bare-mdns-discovery
bare-mdns-discovery
bare-mdns-discovery — bare-mdns-discovery.
npm i bare-mdns-discoveryUsage
Low-level mDNS queries
const { MDNS } = require('bare-mdns-discovery')
const mdns = new MDNS({ debug: true })
await mdns.ready()
mdns.on('records', (records, rinfo) => {
console.log('from:', rinfo.address)
for (const r of records) {
console.log(r.type, r.name, r.data)
}
})
mdns.query('_services._dns-sd._udp.local')
mdns.query('_http._tcp.local')
mdns.query('_googlecast._tcp.local')
setTimeout(() => mdns.close(), 10000)Service discovery
const { Discovery } = require('bare-mdns-discovery')
const discovery = new Discovery({ service: 'googlecast' })
await discovery.ready()
discovery.on('service', (service) => {
console.log('Found:', service.name, service.address, service.port)
})
const services = await discovery.discover(10) // 10 second timeout
console.log('All services:', services)
await discovery.close()Extending for specific services
const { Discovery } = require('bare-mdns-discovery')
class MyServiceDiscovery extends Discovery {
constructor(opts = {}) {
super({ ...opts, service: 'myservice' })
}
_parseService(records, rinfo) {
const service = super._parseService(records, rinfo)
if (!service) return null
// Add custom filtering or fields
return {
...service,
customField: service.txt.someKey
}
}
}API
MDNS
new MDNS(opts?: MDNSOptions)
Source
Send and receive raw mDNS queries and records over UDP multicast. Extends ReadyResource; open it with ready() and release its socket with close().
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
opts? | MDNSOptions | — | Options; see MDNSOptions. |
debug: boolean
Source
Whether internal activity is logged to the console.
emit(event: 'records', records: Record[], rinfo: RecordInfo): boolean
Source
Overloads:
emit(event: 'records', records: Record[], rinfo: RecordInfo): boolean
emit(event: 'error', err: Error): boolean
emit(event: 'ready'): boolean
emit(event: 'close'): booleanParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
event | 'records' | — | — |
records | Record[] | — | — |
rinfo | RecordInfo | — | — |
off(event: 'records', listener: (records: Record[], rinfo: RecordInfo) => void): this
Source
Overloads:
off(event: 'records', listener: (records: Record[], rinfo: RecordInfo) => void): this
off(event: 'error', listener: (err: Error) => void): this
off(event: 'ready', listener: () => void): this
off(event: 'close', listener: () => void): thisParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
event | 'records' | — | — |
listener | (records: Record[], rinfo: RecordInfo) => void | — | — |
on(event: 'records', listener: (records: Record[], rinfo: RecordInfo) => void): this
Source
Overloads:
on(event: 'records', listener: (records: Record[], rinfo: RecordInfo) => void): this
on(event: 'error', listener: (err: Error) => void): this
on(event: 'ready', listener: () => void): this
on(event: 'close', listener: () => void): thisParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
event | 'records' | — | — |
listener | (records: Record[], rinfo: RecordInfo) => void | — | — |
once(event: 'records', listener: (records: Record[], rinfo: RecordInfo) => void): this
Source
Overloads:
once(event: 'records', listener: (records: Record[], rinfo: RecordInfo) => void): this
once(event: 'error', listener: (err: Error) => void): this
once(event: 'ready', listener: () => void): this
once(event: 'close', listener: () => void): thisParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
event | 'records' | — | — |
listener | (records: Record[], rinfo: RecordInfo) => void | — | — |
query(name: string, type?: number): void
Source
Sends an mDNS query for name.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
name | string | — | The DNS name to query for, e.g. '_http._tcp.local'. |
type? | number | — | The DNS record type to request (default TYPE.PTR). |
socket: object | null
Source
The underlying UDP socket, or null before the resource has opened.
Discovery
new Discovery(opts?: DiscoveryOptions)
Source
Discovers instances of a specific mDNS service (such as 'googlecast' or 'http') by periodically querying for it and parsing SRV, TXT, and address records into Service objects. Extends MDNS; subclass and override _parseService to customize how records are turned into a Service for a given service type.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
opts? | DiscoveryOptions | — | Options; see DiscoveryOptions. |
discover(opts?: { first?: boolean; timeout?: number }): Promise<Service[]>
Source
Queries for the configured service every 2 seconds until timeout milliseconds elapse (default 10000), or resolves early when opts.first is set and a service has been found.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
opts? | { first?: boolean; timeout?: number } | — | Options: timeout is how long, in milliseconds, to keep querying before resolving (default 10000); set first to resolve as soon as the first service is found instead of waiting out the timeout. |
Returns Promise<Service[]> — The discovered Service objects — a single-element array when opts.first resolved early.
emit(event: 'service', service: Service): boolean
Source
Overloads:
emit(event: 'service', service: Service): boolean
emit(event: 'records', records: Record[], rinfo: RecordInfo): boolean
emit(event: 'error', err: Error): boolean
emit(event: 'ready'): boolean
emit(event: 'close'): booleanParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
event | 'service' | — | — |
service | Service | — | — |
off(event: 'service', listener: (service: Service) => void): this
Source
Overloads:
off(event: 'service', listener: (service: Service) => void): this
off(event: 'records', listener: (records: Record[], rinfo: RecordInfo) => void): this
off(event: 'error', listener: (err: Error) => void): this
off(event: 'ready', listener: () => void): this
off(event: 'close', listener: () => void): thisParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
event | 'service' | — | — |
listener | (service: Service) => void | — | — |
on(event: 'service', listener: (service: Service) => void): this
Source
Overloads:
on(event: 'service', listener: (service: Service) => void): this
on(event: 'records', listener: (records: Record[], rinfo: RecordInfo) => void): this
on(event: 'error', listener: (err: Error) => void): this
on(event: 'ready', listener: () => void): this
on(event: 'close', listener: () => void): thisParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
event | 'service' | — | — |
listener | (service: Service) => void | — | — |
once(event: 'service', listener: (service: Service) => void): this
Source
Overloads:
once(event: 'service', listener: (service: Service) => void): this
once(event: 'records', listener: (records: Record[], rinfo: RecordInfo) => void): this
once(event: 'error', listener: (err: Error) => void): this
once(event: 'ready', listener: () => void): this
once(event: 'close', listener: () => void): thisParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
event | 'service' | — | — |
listener | (service: Service) => void | — | — |
service: string
Source
The mDNS service name being discovered.
services: Map<string, Service>
Source
A map of discovered services, keyed by service uid, accumulated since the last discover() call.
Constants and variables
TYPE
TYPE: {
readonly A: 1
readonly PTR: 12
readonly TXT: 16
readonly AAAA: 28
readonly SRV: 33
}DNS record type numbers used in mDNS queries and records, per RFC 6762: A, PTR, TXT, AAAA, and SRV.
MDNS_ADDR: '224.0.0.251'
Source
The IPv4 multicast address used for mDNS, 224.0.0.251, per RFC 6762.
MDNS_PORT: 5353
Source
The UDP port used for mDNS, 5353, per RFC 6762.
Types
RecordInfo
interface RecordInfo {
address: string
port: number
family: 'IPv4' | 'IPv6'
size: number
}The remote address information for a received mDNS packet: address, port, family, and the packet size in bytes.
SRVData
interface SRVData {
priority: number
weight: number
port: number
target: string
}The parsed data of an SRV record: priority, weight, port, and target hostname.
TXTData
interface TXTData {
[key: string]: string | boolean
}The parsed key/value pairs of a TXT record. A key with no = in its entry is stored with the value true.
Record
interface Record {
name: string
type: number
class: number
flush: boolean
ttl: number
data: string | TXTData | SRVData | Buffer
}A single parsed DNS resource record from an mDNS response: name, type, class, the flush cache-flush bit, ttl, and the type-specific data.
Service
interface Service {
uid: string
name: string
address: string
addresses: {
ipv4: string | null
ipv6: string[]
}
port: number
target: string
txt: TXTData
}A discovered service: its unique id, name, resolved address, all known addresses (IPv4 and IPv6), port, SRV target hostname, and txt record data.
MDNSOptions
interface MDNSOptions {
debug?: boolean
/** IPv4 address of the network interface to bind multicast membership on.
* Required on Android: pass the WiFi interface IP so the kernel delivers
* mDNS responses on the correct interface. */
iface?: string
}Options for MDNS. debug logs internal activity to the console. iface is the IPv4 address of the network interface to bind multicast membership on; required on Android, where it must be the WiFi interface IP so the kernel delivers mDNS responses on the correct interface.
DiscoveryOptions
interface DiscoveryOptions extends MDNSOptions {
service?: string
}Options for Discovery, extending MDNSOptions with service, the mDNS service name to query for (e.g. 'googlecast').
See also
- Bare modules — the full
bare-*catalog. - Bare runtime API — the runtime these modules extend.