@dawidzawada/bonjour-zeroconf 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +244 -10
- package/package.json +18 -2
package/README.md
CHANGED
|
@@ -1,23 +1,249 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Bonjour Zeroconf 🇫🇷🥖
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
⚡ **High-performance Zeroconf/mDNS service discovery for React Native**
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Discover devices and services on your local network using native Bonjour (iOS) and NSD (Android) APIs. Built with [Nitro Modules](https://nitro.margelo.com/) for maximum performance. Designed for both React Native and Expo. 🧑🚀
|
|
6
|
+
|
|
7
|
+
## ✨ Features
|
|
8
|
+
|
|
9
|
+
- 🏎️ **Racecar performance** – powered by Nitro Modules
|
|
10
|
+
- 🛡️ **Type-safe** – thanks to Nitro & Nitrogen
|
|
11
|
+
- 📡 **Cross-platform** – iOS (Bonjour) and Android (NSD)
|
|
12
|
+
- 📱 **Managing iOS permissions** - no need for extra libraries or custom code, just use `requestLocalNetworkPermission` or `useLocalNetworkPermission` before scanning!
|
|
13
|
+
- 🔄 **Real-time updates** – listen to scan results, state changes, and errors
|
|
14
|
+
- 🧩 **Expo compatible** - (config plugin coming soon)
|
|
15
|
+
|
|
16
|
+
## 📦 Installation
|
|
6
17
|
|
|
7
18
|
```sh
|
|
8
19
|
npm install @dawidzawada/bonjour-zeroconf react-native-nitro-modules
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
> **Note:** `react-native-nitro-modules` is required as a peer dependency.
|
|
23
|
+
|
|
24
|
+
## ⚙️ iOS Setup
|
|
9
25
|
|
|
10
|
-
|
|
26
|
+
On iOS we need to ask for permissions and configure services we want to scan.
|
|
27
|
+
|
|
28
|
+
### Expo:
|
|
29
|
+
|
|
30
|
+
Add this to your `app.json`, `app.config.json` or `app.config.js`:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
{
|
|
34
|
+
ios: {
|
|
35
|
+
infoPlist: {
|
|
36
|
+
NSLocalNetworkUsageDescription:
|
|
37
|
+
'This app needs local network access to discover devices',
|
|
38
|
+
NSBonjourServices: ['_bonjour._tcp', '_lnp._tcp.'],
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
}
|
|
42
|
+
// Add service types you want to scan to NSBonjourServices, first two service types are needed for permissions
|
|
11
43
|
```
|
|
12
44
|
|
|
13
|
-
|
|
45
|
+
Run prebuild command:
|
|
14
46
|
|
|
15
|
-
```
|
|
16
|
-
|
|
47
|
+
```sh
|
|
48
|
+
npx expo prebuild
|
|
49
|
+
```
|
|
17
50
|
|
|
18
|
-
|
|
51
|
+
### React Native:
|
|
19
52
|
|
|
20
|
-
|
|
53
|
+
Add this to your `Info.plist`:
|
|
54
|
+
|
|
55
|
+
```xml
|
|
56
|
+
<key>NSLocalNetworkUsageDescription</key>
|
|
57
|
+
<string>This app needs local network access to discover devices</string>
|
|
58
|
+
<key>NSBonjourServices</key>
|
|
59
|
+
<array>
|
|
60
|
+
<!-- Needed for permissions -->
|
|
61
|
+
<string>_bonjour._tcp</string>
|
|
62
|
+
<string>_lnp._tcp</string>
|
|
63
|
+
<!-- Add other service types you need here -->
|
|
64
|
+
</array>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## 🚀 Quick Start
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
import {
|
|
71
|
+
Scanner,
|
|
72
|
+
useIsScanning,
|
|
73
|
+
type ScanResult,
|
|
74
|
+
} from '@dawidzawada/bonjour-zeroconf';
|
|
75
|
+
import { useEffect, useState } from 'react';
|
|
76
|
+
|
|
77
|
+
function App() {
|
|
78
|
+
const [devices, setDevices] = useState<ScanResult[]>([]);
|
|
79
|
+
|
|
80
|
+
const handleScan = async () => {
|
|
81
|
+
const granted = await requestLocalNetworkPermission();
|
|
82
|
+
if (granted) {
|
|
83
|
+
Scanner.scan('_bonjour._tcp', 'local');
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const handleStop = async () => {
|
|
88
|
+
Scanner.stop();
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const handleCheck = async () => {
|
|
92
|
+
Alert.alert(`Is scanning? ${Scanner.isScanning}`);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
// Listen for discovered devices
|
|
97
|
+
const { remove } = Scanner.listenForScanResults((scan) => {
|
|
98
|
+
setResults(scan);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
return () => {
|
|
102
|
+
remove();
|
|
103
|
+
};
|
|
104
|
+
}, []);
|
|
105
|
+
|
|
106
|
+
return (
|
|
107
|
+
<View>
|
|
108
|
+
<Button title={'Scan'} onPress={handleScan} />
|
|
109
|
+
<Button title={'Stop'} onPress={handleStop} />
|
|
110
|
+
{devices.map((device) => (
|
|
111
|
+
<Text key={device.name}>
|
|
112
|
+
{device.name} - {device.ipv4}:{device.port}
|
|
113
|
+
</Text>
|
|
114
|
+
))}
|
|
115
|
+
</View>
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## 📖 API Reference
|
|
123
|
+
|
|
124
|
+
### **Scanner**
|
|
125
|
+
|
|
126
|
+
#### `scan(type: string, domain: string, options?: ScanOptions)`
|
|
127
|
+
|
|
128
|
+
Start scanning for services.
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
Scanner.scan('_http._tcp', 'local');
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
```ts
|
|
135
|
+
Scanner.scan('_printer._tcp', 'local', {
|
|
136
|
+
addressResolveTimeout: 10000, // ms
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Common service types:**
|
|
141
|
+
|
|
142
|
+
- `_http._tcp` – HTTP servers
|
|
143
|
+
- `_ssh._tcp` – SSH servers
|
|
144
|
+
- `_airplay._tcp` – AirPlay devices
|
|
145
|
+
- `_printer._tcp` – Network printers
|
|
146
|
+
|
|
147
|
+
#### `stop()`
|
|
148
|
+
|
|
149
|
+
Stop scanning and clear cached results.
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
Scanner.stop();
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
#### `listenForScanResults(callback)`
|
|
156
|
+
|
|
157
|
+
Listen for discovered services.
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
const listener = Scanner.listenForScanResults((results: ScanResult[]) => {
|
|
161
|
+
console.log('Found devices:', results);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// Clean up listener
|
|
165
|
+
listener.remove();
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### `listenForScanState(callback)`
|
|
169
|
+
|
|
170
|
+
Listen for scanning state changes.
|
|
171
|
+
|
|
172
|
+
```ts
|
|
173
|
+
const listener = Scanner.listenForScanState((isScanning: boolean) => {
|
|
174
|
+
console.log('Scanning:', isScanning);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// Clean up listener
|
|
178
|
+
listener.remove();
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
#### `listenForScanFail(callback)`
|
|
182
|
+
|
|
183
|
+
Listen for scan failures.
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
const listener = Scanner.listenForScanFail((error: BonjourFail) => {
|
|
187
|
+
console.log('Scan failed:', error);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// Clean up listener
|
|
191
|
+
listener.remove();
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
### **Hooks**
|
|
197
|
+
|
|
198
|
+
#### `useIsScanning()`
|
|
199
|
+
|
|
200
|
+
React hook that returns the current scanning state.
|
|
201
|
+
|
|
202
|
+
```tsx
|
|
203
|
+
const isScanning = useIsScanning();
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
#### `useLocalNetworkPermission()` (iOS only)
|
|
207
|
+
|
|
208
|
+
React hook for managing local network permission.
|
|
209
|
+
|
|
210
|
+
```tsx
|
|
211
|
+
const { status, request } = useLocalNetworkPermission();
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
### **Functions**
|
|
217
|
+
|
|
218
|
+
#### `requestLocalNetworkPermission()`
|
|
219
|
+
|
|
220
|
+
Displays prompt to request local network permission, always returns `true` on Android.
|
|
221
|
+
|
|
222
|
+
```tsx
|
|
223
|
+
const granted = await requestLocalNetworkPermission();
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
---
|
|
227
|
+
|
|
228
|
+
### **Types**
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
interface ScanResult {
|
|
232
|
+
name: string;
|
|
233
|
+
ipv4?: string;
|
|
234
|
+
ipv6?: string;
|
|
235
|
+
hostname?: string;
|
|
236
|
+
port?: number;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
interface ScanOptions {
|
|
240
|
+
addressResolveTimeout?: number; // milliseconds, default: 10000
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
enum BonjourFail {
|
|
244
|
+
DISCOVERY_FAILED = 'DISCOVERY_FAILED',
|
|
245
|
+
RESOLVE_FAILED = 'RESOLVE_FAILED',
|
|
246
|
+
}
|
|
21
247
|
```
|
|
22
248
|
|
|
23
249
|
## Contributing
|
|
@@ -26,10 +252,18 @@ Scanner.scan('_bonjour._tcp', 'local');
|
|
|
26
252
|
- [Sending a pull request](CONTRIBUTING.md#sending-a-pull-request)
|
|
27
253
|
- [Code of conduct](CODE_OF_CONDUCT.md)
|
|
28
254
|
|
|
255
|
+
## Credits
|
|
256
|
+
|
|
257
|
+
- [Nitro Modules](https://nitro.margelo.com/) - High-performance native module framework
|
|
258
|
+
- [mrousavy](https://github.com/mrousavy) - Creator of Nitro Modules
|
|
259
|
+
- [react-native-builder-bob](https://github.com/callstack/react-native-builder-bob) - Library template
|
|
260
|
+
|
|
261
|
+
Solution for handling permissions is based on [react-native-local-network-permission](https://github.com/neurio/react-native-local-network-permission)
|
|
262
|
+
|
|
29
263
|
## License
|
|
30
264
|
|
|
31
265
|
MIT
|
|
32
266
|
|
|
33
267
|
---
|
|
34
268
|
|
|
35
|
-
Made with
|
|
269
|
+
**Made with ❤️ for the React Native community**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dawidzawada/bonjour-zeroconf",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "React Native Zeroconf scanner using Bonjour (iOS) and NSD (Android). Powered by Nitro Modules.",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"types": "./lib/typescript/src/index.d.ts",
|
|
@@ -45,8 +45,24 @@
|
|
|
45
45
|
},
|
|
46
46
|
"keywords": [
|
|
47
47
|
"react-native",
|
|
48
|
+
"bonjour",
|
|
49
|
+
"zeroconf",
|
|
50
|
+
"mdns",
|
|
51
|
+
"nsd",
|
|
52
|
+
"service-discovery",
|
|
53
|
+
"network-discovery",
|
|
54
|
+
"local-network",
|
|
55
|
+
"nitro-modules",
|
|
48
56
|
"ios",
|
|
49
|
-
"android"
|
|
57
|
+
"android",
|
|
58
|
+
"expo",
|
|
59
|
+
"typescript",
|
|
60
|
+
"swift",
|
|
61
|
+
"kotlin",
|
|
62
|
+
"device-discovery",
|
|
63
|
+
"multicast-dns",
|
|
64
|
+
"dns-sd",
|
|
65
|
+
"avahi"
|
|
50
66
|
],
|
|
51
67
|
"repository": {
|
|
52
68
|
"type": "git",
|