Skip to content

Commit

Permalink
docs: add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
robrechtme committed May 3, 2024
1 parent e10b425 commit 1403b68
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 3 deletions.
111 changes: 109 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,120 @@
[![npm](https://img.shields.io/npm/v/@inthepocket/react-native-service-discovery)](https://www.npmjs.com/package/@inthepocket/react-native-service-discovery)
![License: MIT](https://img.shields.io/badge/License-MIT-brightgreen.svg)

This library allows you to discover services on your local network using mDNS (DNS-SD, Bonjour, Zeroconf, Avahi, ...). Devices that support service discovery include printers, webcams, HTTPS servers, and other mobile devices. The library uses [NSNetServiceBrowser](https://developer.apple.com/documentation/foundation/nsnetservicebrowser) on iOS and [NsdManager](https://developer.android.com/develop/connectivity/wifi/use-nsd) on Android.

## Installation

```sh
npm install @inthepocket/react-native-service-discovery
```

Add the services you want to search for in `Info.plist`:

```xml
<key>NSBonjourServices</key>
<array>
<!-- Change to your services -->
<string>_ssh._tcp</string>
<string>_http._tcp</string>
</array>
```

Add a custom Local Network prompt message in `Info.plist`:

```xml
<key>NSLocalNetworkUsageDescription</key>
<string>${PRODUCT_NAME} uses the local network to discover and connect to devices on your Wi-Fi network.</string>
```

## Usage

TODO
```ts
import * as ServiceDiscovery from '@inthepocket/react-native-service-discovery';

const foundListener = ServiceDiscovery.addEventListener('serviceFound', (service) => {
console.log('Service found:', service);
});

const lostListener = ServiceDiscovery.addEventListener('serviceLost', (service) => {
console.log('Service lost:', service);
});

// Start searching for _http._tcp. services
await ServiceDiscovery.startSearch('http');

// ...
await ServiceDiscovery.stopSearch('http');
foundListener.remove();
lostListener.remove();
```

### startSearch

Start searching for services of a specific type. Searching for multiple services can be done in parallel.

| Parameter | Type | Description |
| --------- | ------ | ----------- |
| type | string | The service type to search for, e.g. 'http'. The device will search for services of type `_serviceType._tcp.`. |

#### Returns

`Promise<void>` - Resolves when the search has started. Rejects when the search could not be started.

### stopSearch

Stop searching for a specific service. This will not stop searching for any other services that are being searched for.

| Parameter | Type | Description |
| --------- | ------ | ----------- |
| type | string | The service type to stop searching for, e.g. 'http'. |

#### Returns

`Promise<void>` - Resolves when the search has stopped. Rejects when the search could not be stopped. Does not reject when the search was already stopped or was not started in the first place.

### addEventListener

| Parameter | Type | Description |
| --------- | ------ | ----------- |
| event | `'serviceFound' \| 'serviceLost'` | Start listening for found or lost services. |
| listener | `(service: Service) => void` | The listener that will be called when a service is found or lost. |

#### Returns

An `EmitterSubscription` that can be used to remove the listener, by calling `remove()`.

### Service

| Attribute | Type | Description |
| --------- | ------ | ----------- |
| name | `string` | Service name. |
| type | `string` | Service type, e.g. `_http._tcp.`. |
| domain | `string` | Domain, can be `local.` or any other domain. |
| hostName | `string` | The domain name of the IP advertising the service. On local networks this is usually the host name. |
| addresses | `string[]` | The IP addresses of the service. This is an array of IPv4 and IPv6 addresses. |
| port | `number` | The port number where the service can be reached. |
| txt | `Record<string, string>` | The TXT record of the service. The TXT record gives additional information about the service. |


### Listening for multiple service types

You can listen for multiple service types with the same listener:

```ts
import * as ServiceDiscovery from '@inthepocket/react-native-service-discovery';

ServiceDiscovery.addEventListener('serviceFound', (service) => {
if(service.type === '_http._tcp.') {
console.log('HTTP service found:', service);
} else if(service.type === '_ssh._tcp.') {
console.log('SSH service found:', service);
}
});

await ServiceDiscovery.startSearch('http');
await ServiceDiscovery.startSearch('ssh');
```

## Contributing

Expand All @@ -24,5 +129,7 @@ See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the
MIT

---
Many thanks to

Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
- [react-native-dnssd](https://github.com/kopera/react-native-dnssd)
- [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
66 changes: 65 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,44 @@ const ServiceDiscovery = ServiceDiscoveryModule
}
);

/**
* A service that is found or lost on the network.
*/
export interface Service {
/**
* Service name.
*/
readonly name: string;
/**
* Service type, e.g. `_http._tcp.`.
*/
readonly type: string;
/**
* Domain, can be `local.` or any other domain.
*/
readonly domain: string;
/**
* The domain name of the IP advertising the service.
* On local networks this is usually the host name.
*/
readonly hostName: string;
/**
* The IP addresses of the service.
* This is an array of IPv4 and IPv6 addresses.
*/
readonly addresses: string[];
/**
* The port number where the service can be reached.
*/
readonly port: number;
/**
* The TXT record of the service. The TXT record gives additional information
* about the service.
*/
readonly txt: Record<string, string>;
}

export type ServiceEvent = 'serviceFound' | 'serviceLost' | 'error';
export type ServiceEvent = 'serviceFound' | 'serviceLost';

export type ServiceCallback = (service: Service) => void;

Expand All @@ -44,13 +71,50 @@ export interface Subscription {

const EventEmitter = new NativeEventEmitter(ServiceDiscovery);

/**
*
* @example
* ```ts
* import * as ServiceDiscovery from '@inthepocket/react-native-service-discovery';
*
* ServiceDiscovery.addEventListener('serviceFound', (service) => console.log('Service found:', service));
*
* // Start searching for _http._tcp. services
* await ServiceDiscovery.startSearch('http');
* ```
*/
export const addEventListener = (
event: ServiceEvent,
listener: ServiceCallback
): Subscription => EventEmitter.addListener(event, listener);

/**
* Start searching for services of a specific type. Searching for
* multiple services can be done in parallel.
*
* @param serviceType The service type to search for, e.g. 'http'. The device
* will search for services of type `_serviceType._tcp.`.
*
* @example
* ```ts
* import * as ServiceDiscovery from '@inthepocket/react-native-service-discovery';
*
* ServiceDiscovery.addEventListener('serviceFound', (service) => console.log('Service found:', service));
*
* // Start searching for _http._tcp. services
* await ServiceDiscovery.startSearch('http');
* ```
*/
export const startSearch = (serviceType: string): Promise<void> =>
ServiceDiscovery.startSearch(serviceType);

/**
* Stop searching for a specific service. This will not stop searching for any
* other services that are being searched for.
*
* @returns A promise that resolves when the search has been stopped, and
* rejects if the search cannot be stopped. Does not reject if the search has
* already been stopped.
*/
export const stopSearch = (serviceType: string): Promise<void> =>
ServiceDiscovery.stopSearch(serviceType);

0 comments on commit 1403b68

Please sign in to comment.