balena-io-modules / resin-discoverable-services

An API to allow registration and discovery of local zeroconf services
Apache License 2.0
6 stars 1 forks source link

resin-discoverable-services

Discover all resin registered services from the local network.

npm version [dependencies Build Status ![Gitter](https://badges.gitter.im/Join Chat.svg)

Role

The intention of this module is to provide a way to discover zeroconf services advertised by Resin.io components and devices on a local network.

Installation

Install resin-discoverable-services by running:

$ npm install --save resin-discoverable-services

API

This module exports three methods:

setRegistryPath(path)

This informs the module where to search for valid services to register with itself. By default this is the services directory within it. Service definitions are defined as a set of nested directories, and take the form:

`[<subtype>/]<type>/<protocol>`

A tags.json file may exist in the lowest level directory in a service definition and consists of an array of strings, each of which is a 'shortened name' for the service defined, eg.:

`[ 'serviceShortName', 'reallyShort' ]`

enumerateServices([callback(error, services)])

Returns the list of currently registered service definitions. Service definitions are returned in the standard service form:

`{ service: `[_<subtype>._sub]._<type>._protocol`, tags: [ [tagNames] ] }`

Notice Should a callback not be provided, then the method will return a resolvable Promise.

Only registered services returned by the enumerateServices() method may be used to find services on the local network.

Should a callback not be provided, then the method will return a resolvable Promise.

findServices(services, [[timeout], [callback(error, services)])

Searches for any occurrences of the specified services on the local network. Services are passed in the standard service form, or as a tag associated with that service.

Notice Should a callback not be provided, then the method will return a resolvable Promise.

The timeout value is the length of time in milliseconds that the module will search for before returning any results. If no value is passed, it defaults to 2000ms.

The method returns an array of services that conform to those registered. Each service information object consists of:

publishServices(services, [options, [callback(error, services)]])

Publishes any valid service to the network. If a call to this method is made then a corresponding call to unpublishServices() must be made before process exit to ensure any existing network sockets are finalised.

Notice Should a callback not be provided, then the method will return a resolvable Promise.

services is an array of objects, each of which contains:

options is an object allowing publishing options to be passed:

unpublishServices([callback])

Unpublishes all services currently published to the network. This method must be called prior to process exit should any calls to publishServices() have been made.

Notice Should a callback not be provided, then the method will return a resolvable Promise.

Support

If you're having any problem, please raise an issue on GitHub and the Resin.io team will be happy to help.

Tests

Run the test suite by doing:

$ gulp test

Contribute

Before submitting a PR, please make sure that you include tests, and that coffeelint runs without any warning:

$ gulp lint

License

The project is licensed under the Apache 2.0 license.