thehellmaker-react-native-zeroconf
Version:
A Zeroconf discovery utility for react-native
111 lines (71 loc) • 3.29 kB
Markdown
> Basic Zeroconf implementation for React-native
Get running services advertizing themselves using Zeroconf implementations like Avahi, Bonjour or NSD.
yarn add react-native-zeroconf
react-native link
(cd ios && pod install)
You can look at [the wiki](https://github.com/Apercu/react-native-zeroconf/wiki) if you prefer a manual install.
TXT records will be available on iOS and Android >= 7.
For Android please ensure your manifest is requesting all necessary permissions.
```xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
```
Take a look at the [example folder](./example). Install the dependencies, run `node server.js` and launch the project.
```javascript
import Zeroconf from 'react-native-zeroconf'
const zeroconf = new Zeroconf()
```
This will initialize the scan from the `Zeroconf` instance. Will stop another scan if any is running.
If any scan is running, stop it. Otherwise do nothing.
Will return all names of services that have been resolved.
Allow you to clean the listeners, avoiding potential memory leaks ([#33](https://github.com/Apercu/react-native-zeroconf/issues/33)).
###### `addDeviceListeners()` Add listeners
If you cleaned the listeners and need to get them back on.
###### `publishService(type, protocol, domain, name, port, txt)` Publish a service
This adds a service for the current device to the discoverable services on the network.
`domain` should be the domain the service is sitting on, dot suffixed, for example `'local.'`
`type` should be both type and protocol, underscore prefixed, for example `'_http._tcp'`
`name` should be unique to the device, often the device name
`port` should be an integer
`txt` should be a hash, for example `{"foo": "bar"}`
This removes a service from those discoverable on the network.
`name` should be the name used when publishing the service
```javascript
zeroconf.on('start', () => console.log('The scan has started.'))
```
Broadcast a service name as soon as it is found.
Broadcast a service object once it is fully resolved
```json
{
"host": "XeroxPrinter.local.",
"addresses": [
"192.168.1.23",
"fe80::aebc:123:ffff:abcd"
],
"name": "Xerox Printer",
"fullName": "XeroxPrinter.local._http._tcp.",
"port": 8080
}
```
Broadcast a service name removed from the network.