API Docs

A discovered Bonjour service record. Call resolve() to look up its hostname, port, and addresses.

Instances are delivered by an HSBonjourSearch callback. Call resolve() to discover their hostname, port, and addresses, and optionally monitor() to watch for TXT record changes.

Callback events

Method Event Extra data
resolve() "resolved" (none)
resolve() "stopped" (none)
resolve() "error" error message string
monitor() "txtRecord" updated TXT record dict

Properties

identifier

string

A unique identifier assigned to this service object.

name

string

The service name (e.g. "My Web Server").

type

string

The service type string (e.g. "_http._tcp.").

domain

string

The mDNS domain (almost always "local.").

hostname

string

The resolved hostname, or null before resolve() completes.

port

number

The service port. -1 until resolve() completes.

addresses

string[]

IP address strings (IPv4 and/or IPv6) populated after resolve() completes.

txtRecord

[String: String]

The TXT record as a {key: value} object, or null if none is available. Populated after resolve() completes or when updated via monitor().

includesPeerToPeer

boolean

Whether peer-to-peer Bluetooth/Wi-Fi is included in resolution.

Methods

resolve(timeout, callback) -> HSBonjourService

Resolves the hostname, port, addresses, and TXT record of this service.

The callback is called with (event) or (event, errorMessage):

  • "resolved" — resolution complete; read hostname, port, addresses, txtRecord
  • "stopped" — resolution stopped before completing
  • "error" — resolution failed; error message in second argument
resolve(timeout, callback) -> HSBonjourService
Name Type Description
timeout number seconds before giving up; pass 0 for no timeout
callback function Called on status changes with event name and optional error message
HSBonjourService
self, for chaining
A resolving or monitored service, and its callbacks, stay alive until that finishes or is stopped, even if you don't keep a reference to it
service.resolve(5, (ev, err) => {
    if (ev === 'resolved') console.log(service.hostname, service.port)
    else console.error('Resolve failed:', err)
})

monitor(callback) -> HSBonjourService

Starts monitoring the TXT record for changes. The callback fires whenever the TXT record is updated.

Call stopMonitoring() to unsubscribe.

monitor(callback) -> HSBonjourService
Name Type Description
callback function Called when TXT data changes with the updated record
HSBonjourService
self, for chaining
service.monitor(txt => console.log('TXT updated:', txt))

stop() -> HSBonjourService

Stops any active resolution.

stop() -> HSBonjourService
HSBonjourService
self, for chaining
service.stop()

stopMonitoring() -> HSBonjourService

Stops TXT record monitoring started by monitor().

stopMonitoring() -> HSBonjourService
HSBonjourService
self, for chaining
service.stopMonitoring()

toJSON() -> {[key: string]: any}

Returns a plain object describing this service, for JSON serialisation

JSON.stringify() calls this automatically, so you rarely need to call it yourself.

toJSON() -> {[key: string]: any}
{[key: string]: any}
An object with the service's name, type, domain, hostname, port, addresses, txtRecord and includesPeerToPeer. hostname and txtRecord are null until the service has been resolved
console.log(JSON.stringify(service))