bonjour-hap
v3.11.1
Published
A Bonjour/Zeroconf implementation in pure JavaScript (for HAP)
Readme
bonjour-hap
A Bonjour/Zeroconf protocol implementation in pure JavaScript. Publish services on the local network or discover existing services using multicast DNS.
Notice
bonjour-hap is maintained by the Homebridge team for
HAP-NodeJS, where it is one of the advertisers that can be chosen.
It was the only one in versions prior to v0.8.0.
For new projects, the ciao library is preferred. It is the default advertiser
in HAP-NodeJS since v0.8.0, and closely follows RFC 6762 (mDNS) and RFC 6763 (DNS-SD).
bonjour-hap does not fully implement those RFCs: for example it has no tiebreaking when two devices ask for the
same name at the same moment, and it does not pick a new name when the one it was given is taken. Bugs are fixed, but the code should not be
taken as a reference for how mDNS should work.
Installation
npm install bonjour-hapUsage
var bonjour = require('bonjour-hap')()
// 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-hap')([options])The options are optional and will be used when initializing the
underlying multicast-dns server. For details see the multicast-dns
documentation.
Publishing
var service = bonjour.publish(options)
Publishes a new service.
Options are:
name(string) - at most 63 bytes. A name with a dot in it is sent as two names, so Apple devices do not list the servicehost(string, optional) - defaults to the local hostname, with.localadded when it has no domainport(number)type(string)subtypes(array of strings, optional) - for example['printer']. Each one is advertised, so a browser asking for only that subtype (_printer._sub._http._tcp.local) finds the serviceprotocol(string, optional) -udportcp(default)txt(object, optional) - a key/value object to broadcast as the TXT record. Eachkey=valueentry can be at most 255 bytesprobe(boolean, optional) - check that the name is free on the network before announcing it. Defaults totruerestrictedAddresses(array of strings, optional) - IP addresses and/or network interface names. Only these addresses are advertised. An interface name stands for every address on that interfacedisabledIpv6(boolean, optional) - do not advertise IPv6 addressesaddUnsafeServiceEnumerationRecord(boolean, optional) - also advertise the service type under_services._dns-sd._udp.local, the name that tools listing every type of service ask for. Despite the name it is safe with several services: those of the same type share the record, and it is withdrawn when the last of them stops
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([callback])
Destroy the mdns instance. Stops every browser, says goodbye for every
published service, then closes the udp socket. The optional callback
will be called when that is done. After this, publish(), find() and
findOne() throw.
Event: error
Emitted on the bonjour instance when the udp socket reports an error,
or a reply could not be sent. With no listener attached, the error is
logged with console.warn instead of being thrown.
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) - leave out to look for every type of serviceprotocol(string) - defaults totcpname(string) - look for one service of that name onlytxt(object) - set to{ binary: true }if you want to keep the values of 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: update
Emitted every time an update is received for an existing service that matches the browser, including a TXT or SRV record announced on its own.
Event: down
Emitted every time an existing service emits a goodbye message, or stops answering: the browser asks for it again as its record nears expiry, and reports it down if nothing answers before it expires.
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.
Service
Event: up
Emitted once each time the service is started, after its first
announcement. Updating the txt record with service.updateTxt(object)
does not emit it again.
Event: error
Emitted if an error occurs while publishing the service, e.g. when its name is
already in use on the network. With no listener attached, the error is logged
with console.warn instead of being thrown.
service.stop([callback])
Unpublish the service. The optional callback will be called when the
service have been unpublished.
service.start()
Publish the service.
service.updateTxt(object[, silent])
Update the txt record of the service. The new record is announced to
the network straight away, unless silent is true: then it is given
out when asked for, and with the next re-announcement that was already
planned.
service.destroy()
Stop the service's timers and remove its listeners, without saying
goodbye. Call service.stop() first to withdraw it from the network.
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
