Fully featured mDNS and DNS-SD implementation in Typescript
npm install @astronautlabs/mdnsv1.x.x) but will be receiving substantial API upgrades and retooling to use
@astronautlabs/* frameworks in v2.x, so expect fairly large compatibility changes when those
npm install @astronautlabs/mdns
`
Usage
Advertise an HTTP server on port 4321:
`ts
import { Advertisement } from '@astronautlabs/mdns';
const ad = new Advertisement('_http._tcp', 4321)
.start();
`
Find all Google Cast compatible devices:
`ts
import { Browser } from '@astronautlabs/mdns';
new Browser('_googlecast._tcp')
.on('serviceUp', service => console.log("Device up: ", service))
.on('serviceDown', service => console.log("Device down: ", service))
.start();
`
You can also use this library to query multicast DNS as you would unicast DNS using the MulticastDNS class.
`ts
import { MulticastDNS } from '@astronautlabs/mdns';
let ipAddress: string = await MulticastDNS.A('myService._http._tcp');
`
You can also use MulticastDNS.query() to retrieve the records themselves.
`ts
import { MulticastDNS, SRVRecord } from '@astronautlabs/mdns';
let { answer, related } = await MulticastDNS.query('myService._http._tcp', 'SRV');
// answer: SRVRecord, related: ResourceRecord[]
`
Validation
Service type names and TXT records have some specific restrictions.
Service names:
* must start with an underscore _
* less than 16 chars including the leading _
* must start with a letter or digit
* only letters / digits / hyphens (but not consecutively: --)
TXT records:
* Keys <= 9 chars
* Keys must be ascii and can't use '='
* Values must be a string, buffer, number, or boolean
* Each key/value pair must be < 255 bytes
* Total TXT object is < 1300 bytes
Credits
This package is based on Gravity Software's dnssd.js package, which itself is based on David Siegel's mdns` package.