Bonjour/Avahi-like service discovery in pure JavaScript
npm install dnssddnssd lets you find (and advertise) services on your network like chromecasts, printers, and airplay speakers.
npm install dnssd
`
[6762]: https://tools.ietf.org/html/rfc6762
[6763]: https://tools.ietf.org/html/rfc6763
[flood]: https://www.engadget.com/2018/01/16/google-chromecast-messing-wifi-connections/
Usage
`js
const dnssd = require('dnssd');
// advertise a http server on port 4321
const ad = new dnssd.Advertisement(dnssd.tcp('http'), 4321);
ad.start();
// find all chromecasts
const browser = dnssd.createBrowser(dnssd.tcp('googlecast'))
.on('serviceUp', service => console.log("Device up: ", service))
.on('serviceDown', service => console.log("Device down: ", service))
.start();
`
Documentation
dnssd aims to have a API compatible with the mdns package + some extras.
- Class: Advertisement
+ [new dnssd.Advertisement(serviceType, port [, options])](#user-content-new-advertisement)
+ .start()
+ [.stop([forceImmediately [, callback]])](#user-content-advertisement-stop)
+ .on(event, listener)
+ .updateTXT(txt)
- Class: Browser
+ [new dnssd.Browser(serviceType [, options])](#user-content-new-browser)
+ .start()
+ .stop()
+ .on(event, listener)
+ .list()
- new dnssd.ServiceType(...args)
- dnssd.tcp(...args)
- dnssd.udp(...args)
- dnssd.all()
- [resolve(name, type [, options])](#user-content-resolve)
+ [resolveA(name [, options])](#user-content-resolve-a)
+ [resolveAAAA(name [, options])](#user-content-resolve-aaaa)
+ [resolveSRV(name [, options])](#user-content-resolve-srv)
+ [resolveTXT(name [, options])](#user-content-resolve-txt)
+ [resolveService(name [, options])](#user-content-resolve-service)
$3
`js
// advertising a http server on port 4321:
const ad = new dnssd.Advertisement(dnssd.tcp('http'), 4321);
ad.start();
`
options.name - instance name
options.host - hostname to use
options.txt - TXT record
options.subtypes - subtypes to register
options.interface - interface name or address to use ('eth0' or '1.2.3.4')
#### .start()
Starts the advertisement.
If there is a conflict with the instance name it will automatically get renamed. (Name -> Name (2))
#### .stop([forceImmediately [, callback]])
Stops the advertisement.
Can do either a clean stop or a forced stop. A clean stop will send goodbye records out so others will know the service is going down. This takes ~1s. Forced goodbyes shut everything down immediately.
#### .on(event, listener)
error
stopped when the advertisement is stopped
instanceRenamed when the service instance has to be renamed
hostRenamed when the hostname has to be renamed
#### .updateTXT(txt)
Updates the advertisements TXT record
$3
`js
// find all chromecasts
const browser = dnssd.createBrowser(dnssd.tcp('googlecast'))
.on('serviceUp', service => console.log("Device up: ", service))
.on('serviceDown', service => console.log("Device down: ", service))
.start();
`
A resolved service looks like:
`js
service = {
fullname: 'InstanceName._googlecast._tcp.local.',
name: 'InstanceName',
type: { name: 'googlecast', protocol: 'tcp' },
domain: 'local',
host: 'Hostname.local.',
port: 8009,
addresses: ['192.168.1.15'],
txt: { id: 'strings' },
txtRaw: { id: },
};
`
Browser search is a multi-step process. First it finds an instance name, then it resolves all the necessary properties of the service, like the address and the port. It keeps that data up to date by sending more queries out as needed. If you want less steps, there's some options:
options.maintain: Set to false if don't want to maintain a service's info. This will give you a 'serviceUp' event but no 'serviceDown' or 'serviceUpdated'
options.resolve: Set to false if you only want the instance name and nothing else.
options.interface: Sets the interface to use ('eth0' or '1.2.3.4')
#### .start()
Starts the browser.
#### .stop()
Stops the browser.
#### .on(event, listener)
error
serviceUp when a new service is found
serviceChanged when a service's data has changed
serviceDown when a service goes down
#### .list()
Lists all current services that have been found.
$3
Used to turn some input into a reliable service type for advertisements and browsers. Name and protocol are always required, subtypes are optional. Multiple forms available:
String _(single argument)_
`js
'_http._tcp'
'_http._tcp,mysubtype,anothersub'
`
Object _(single argument)_
`js
{
name: '_http',
protocol: '_tcp',
subtypes: ['mysubtype', 'anothersub'],
}
`
Array _(single argument)_
`js
['_http', '_tcp', ['mysubtype', 'anothersub']]
['_http', '_tcp', 'mysubtype', 'anothersub']
`
Strings _(multiple arguments)_
`js
'_http', '_tcp'
'_http', '_tcp', 'mysubtype', 'anothersub'
`
$3
Creates a new ServiceType with tcp protocol
`js
ServiceType.tcp('_http')
ServiceType.tcp('_http', 'sub1', 'sub2')
ServiceType.tcp(['_http', 'sub1', 'sub2'])
`
$3
Creates a new ServiceType with udp protocol
`js
new ServiceType('_services._dns-sd._udp');
`
$3
`js
// browse all the things
const browser = dnssd.createBrowser(dnssd.all())
`
$3
Async functions for resolving specific records / record types. Returns a promise with result.
`js
dnssd.resolve(name, rrtype).then(function(result) {})
result = {
answer: {}
related: [{}, {}]
}
`
#### dnssd.resolveA(name [, options])
`js
dnssd.resolveA('something.local.').then((address) => {
address === '192.168.1.10'
});
`
#### dnssd.resolveAAAA(name [, options])
`js
dnssd.resolveAAAA('computer.local.').then((address) => {
address === '2001:0db8:85a3:0000:0000:8a2e:0370:7334'
});
`
#### dnssd.resolveSRV(name [, options])
`js
dnssd.resolveSRV(name).then((srv) => {
srv === {
target: 'machine.local.',
port: 8000,
}
});
`
#### dnssd.resolveTXT(name [, options])
`js
dnssd.resolveTXT(name).then((txt) => {
txt === { some: 'thing' }
});
`
#### dnssd.resolveService(name [, options])
`js
dnssd.resolveService(name).then((service) => {
service === like the browser results
});
``