Skip to content

About

A Bonjour/Zeroconf protocol implementation in TypeScript

Resources

Stars

1 star

Watchers

0 watching

Forks

 
 

Latest commit

 

History

297 Commits

Folders and files

Repository files navigation

bonjour-service

npm version npm bundle size GitHub Release Date npm weekly downloads npm total downloads GitHub

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

This is a temporary (aka permanent) fork of (https://github.com/onlxltd/bonjour-service) due to some limitations of the original that made it unsuitable for prolonged use. That project is a rewrite of the project Bonjour (https://github.com/watson/bonjour) into modern TypeScript.

Installation

Add to your project dependencies using Yarn or NPM.

Install with Yarn

yarn add @julusian/bonjour-service

Install with NPM

npm install @julusian/bonjour-service

Usage

import { Bonjour } from '@julusian/bonjour-service'

const instance = new Bonjour()

// advertise an HTTP server on port 3000
instance.publish({ name: 'My Web Server', type: 'http', port: 3000 })

// browse for all http services
instance.find({ type: 'http' }, function (service) {
  console.log('Found an HTTP server:', service)
})

API

Initializing

var instance = new Bonjour({ options }, errorCallback)

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

errorCallback is an optional callback used to gracefully handle errors that would otherwise crash the process. This includes fatal errors from the underlying socket, such as it failing to bind (EADDRINUSE/EACCES). While not being strictly required, providing this is highly recommended. Packets which could not be sent (out of any interface) are also passed to it; without it, those are ignored.

Unless an interface is given, multicast packets are sent out of each network interface in turn, rather than only the one the OS picks.

Publishing

var service = bonjour.publish(options)

Publishes a new service.

Options are:

  • name (string)
  • host (string, optional) - the hostname advertised in the A/AAAA and SRV records. Defaults to a unique name derived from the machine hostname plus a short random suffix (e.g. my-mac-a3f9.local), so it never clashes with the name the OS's own mDNS responder defends (avoids the macOS "another computer on the network is using the name..." dialog)
  • port (number)
  • type (string) - at most 15 characters, without the leading underscore (e.g. http). Longer types are rejected per RFC 6763 section 7.2, as some mDNS implementations crash on them
  • 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
  • disableIPv6 (boolean, optional) disble IPv6 addresses
  • allowInvalidType (boolean, optional) - skip the type checks above. Only intended for keeping an existing non-compliant type working; other mDNS implementations may reject or crash on it

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

bonjour.unpublishAll([callback])

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

bonjour.destroy()

Destroy the mdns instance. Closes the udp socket.

Browser

var browser = bonjour.find(options[, onup])

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

var browser = bonjour.findOne(options[, callback])

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.

Event: up

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

Event: down

Emitted every time an existing service emmits a goodbye message.

Event: srv-update

Emitted every time an existing service does a new announcement with an updated SRV record.

Event: txt-update

Emitted every time an existing service does a new announcement with an updated TXT record.

browser.services

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

browser.start()

Start looking for matching services.

browser.stop()

Stop looking for matching services.

browser.update()

Broadcast the query again.

browser.expire()

Check any services for an expired TTL and emit stop events.

Service

Event: up

Emitted when the service is up.

Event: rename

Emitted with the new name, when the name was found to be in use by another host while probing. The service then tries Name (2), Name (3) and so on (RFC 6763 appendix D).

service.stop([callback])

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

service.start()

Publish the service.

service.name

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

service.type

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

service.subtypes

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

service.protocol

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

service.host

The hostname or ip address where the service resides.

service.port

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

service.fqdn

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.

service.txt

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

service.published

A boolean indicating if the service is currently published.

License

MIT

About

A Bonjour/Zeroconf protocol implementation in TypeScript

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages