HSBonjourService
TypeA 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
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()`.
Methods
resolve(timeout, callback) -> HSBonjourService
Resolves the hostname, port, addresses, and TXT record of this service.
Declaration
resolve(timeout, callback) -> HSBonjourService
Parameters
| 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 |
Returns
HSBonjourService
self, for chaining
Notes
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
Example
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.
Declaration
monitor(callback) -> HSBonjourService
Parameters
| Name | Type | Description |
|---|---|---|
| callback | function | Called when TXT data changes with the updated record |
Returns
HSBonjourService
self, for chaining
Example
service.monitor(txt => console.log('TXT updated:', txt))
stop() -> HSBonjourService
Stops any active resolution.
Declaration
stop() -> HSBonjourService
Returns
HSBonjourService
self, for chaining
Example
service.stop()
stopMonitoring() -> HSBonjourService
Stops TXT record monitoring started by `monitor()`.
Declaration
stopMonitoring() -> HSBonjourService
Returns
HSBonjourService
self, for chaining
Example
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.
Declaration
toJSON() -> {[key: string]: any}
Returns
{[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
Example
console.log(JSON.stringify(service))