sfcode An Online Competing and Development Environment

bonjour

A Bonjour/Zeroconf protocol implementation in pure JavaScript. Publish services on the local network or discover existing services using multicast DNS.

# Installation

npm install bonjour

# Usage

var bonjour = require('bonjour')()
// advertise an HTTP server on port 3000
bonjour.publish({ name: 'My Web Server', type: 'http', port: 3000 })
// browse for all http services
bonjour.find({ type: 'http' }, function (service) {
console.log('Found an HTTP server:', service)
})

# API

## Initializing

var bonjour = require('bonjour')([options])

The `options` are optional and will be used when initializing the underlying multicast-dns server. For details see the multicast-dns documentation.

## Publishing

### <tt>var service = bonjour.publish(options)</tt>

Publishes a new service.

Options are:

- `name` (string)
- `host` (string, optional) - defaults to local hostname
- `port` (number)
- `type` (string)
- `subtypes` (array of strings, optional)
- `protocol` (string, optional) - `udp` or `tcp` (default)
- `txt` (object, optional) - a key/value object to broadcast as the TXT record

IANA maintains a list of official service types and port numbers.

### <tt>bonjour.unpublishAll([callback])</tt>

Unpublish all services. The optional `callback` will be called when the services have been unpublished.

### <tt>bonjour.destroy()</tt>

Destroy the mdns instance. Closes the udp socket.

## Browser

### <tt>var browser = bonjour.find(options[, onup])</tt>

Listen for services advertised on the network. An optional callback can be provided as the 2nd argument and will be added as an event listener for the `up` event.

Options (all optional):

- `type` (string)
- `subtypes` (array of strings)
- `protocol` (string) - defaults to `tcp`
- `txt` (object) - passed into dns-txt module contructor. Set to `{ binary: true }` if you want to keep the TXT records in binary

### <tt>var browser = bonjour.findOne(options[, callback])</tt>

Listen for and call the `callback` with the first instance of a service matching the `options`. If no `callback` is given, it's expected that you listen for the `up` event. The returned `browser` will automatically stop it self after the first matching service.

Options are the same as given in the `browser.find` function.

### <tt>Event: up</tt>

Emitted every time a new service is found that matches the browser.

### <tt>Event: down</tt>

Emitted every time an existing service emmits a goodbye message.

### <tt>browser.services</tt>

An array of services known by the browser to be online.

### <tt>browser.start()</tt>

Start looking for matching services.

### <tt>browser.stop()</tt>

Stop looking for matching services.

### <tt>browser.update()</tt>

Broadcast the query again.

## Service

### <tt>Event: up</tt>

Emitted when the service is up.

### <tt>Event: error</tt>

Emitted if an error occurrs while publishing the service.

### <tt>service.stop([callback])</tt>

Unpublish the service. The optional `callback` will be called when the service have been unpublished.

### <tt>service.start()</tt>

Publish the service.

### <tt>service.name</tt>

The name of the service, e.g. `Apple TV`.

### <tt>service.type</tt>

The type of the service, e.g. `http`.

### <tt>service.subtypes</tt>

An array of subtypes. Note that this property might be `null`.

### <tt>service.protocol</tt>

The protocol used by the service, e.g. `tcp`.

### <tt>service.host</tt>

The hostname or ip address where the service resides.

### <tt>service.port</tt>

The port on which the service listens, e.g. `5000`.

### <tt>service.fqdn</tt>

The fully qualified domain name of the service. E.g. if given the name `Foo Bar`, the type `http` and the protocol `tcp`, the `service.fqdn` property will be `Foo Bar._http._tcp.local`.

### <tt>service.txt</tt>

The TXT record advertised by the service (a key/value object). Note that this property might be `null`.

### <tt>service.published</tt>

A boolean indicating if the service is currently published.

# License

MIT