TypeScript/JavaScript library for discovery and publishing of services using Multicast DNS (mDNS) and DNS-SD also known as Zeroconf/Bonjour.
To install:
npm install tinkerhub-mdns
Example:
const { MDNSServiceDiscovery } = require('tinkerhub-mdns');
const discovery = new MDNSServiceDiscovery({
type: 'spotify-connect'
});
// Listen for services as they become available
discovery.onAvailable(service => {
// Service available
});
// And for updates to them, such as new network addresses
discovery.onUpdate(service => {
// Service updated
});
// And for when they are no longer available
discovery.onUnavailable(service => {
// Service no longer available
});
// When discovery is no longer needed destroy it
discovery.destroy();
Every service discovered using this discovery will have some data available:
id: string
- the identifier of the service, the hostname as used in DNS-SDname: string
- the parsed nametype: string
- the type of serviceprotocol: 'tcp' | 'udp'
- the protocol used in the registrationsubtypes: string[]
- subtypes found for the serviceaddresses: HostAndPort[]
- addresses where the service can be reacheddata: Map<string, string | boolean>
- decoded data for the servicebinaryData: Buffer[]
- raw binary data
This library supports publishing of services using MDNSServicePublisher
:
import { MDNSServicePublisher } from 'tinkerhub-mdns';
const publisher = new MDNSServicePublisher({
name: 'unique-name-of-service',
type: 'http',
protocol: 'tcp',
port: 8080
});
// To unpublish call destroy()
await publisher.destroy();