HSBonjourSearch
TypeDiscovers Bonjour services and domains advertised on the local network.
Create via hs.bonjour.newSearch(), then call one of the find… methods.
Each search type uses its own underlying NetServiceBrowser, so service and
domain searches can run concurrently. Restarting any single search type stops
only that browser before beginning the new one.
Service search callback events
| Event | Data | Description |
|---|---|---|
"serviceFound" |
HSBonjourService |
A matching service appeared |
"serviceRemoved" |
HSBonjourService |
A previously found service disappeared |
"error" |
error string | The search failed |
Domain search callback events
| Event | Data | Description |
|---|---|---|
"domainFound" |
domain string | A domain was discovered |
"domainRemoved" |
domain string | A domain disappeared |
"error" |
error string | The search failed |
Example:
const search = hs.bonjour.newSearch()
search.findServices('_ssh._tcp.', 'local.', (event, svc, moreComing) => {
if (event !== 'serviceFound') return
console.log('Discovered: ' + svc.name + ' (' + svc.type + ' ' + svc.domain + ')')
svc.resolve(5, ev => {
if (ev === 'error') { console.error('Resolve failed for' + svc.name); return }
if (ev !== 'resolved') return
console.log(' Host: ' + svc.hostname + ' port ' + svc.port)
console.log(' Addresses: ' + svc.addresses.join(', '))
if (svc.txtRecord) console.log(' TXT record:', JSON.stringify(svc.txtRecord))
})
})
Properties
includesPeerToPeer
boolean
Whether to search over peer-to-peer Bluetooth/Wi-Fi in addition to
standard network interfaces. Defaults to `false`.
Methods
findServices(type, domain, callback) -> HSBonjourSearch
Searches for services of the given type in the given domain.
If a service search is already active it is stopped before starting the
new one. Domain searches are unaffected. The callback receives
`(event, service, moreComing)` — see the type documentation for the
complete event table.
Declaration
findServices(type, domain, callback) -> HSBonjourSearch
Parameters
| Name | Type | Description |
|---|---|---|
| type | string | service type string, e.g. `"_http._tcp."` or `"_ssh._tcp."` |
| domain | string | mDNS domain; `"local."` for the local link, `""` for all domains |
| callback | JSValue | `function(event, service, moreComing)` called for each result |
Returns
HSBonjourSearch
self, for chaining
Example
search.findServices('_http._tcp.', 'local.', (ev, svc, more) => {
if (ev === 'serviceFound') console.log('Found:', svc.name)
})
findBrowsableDomains(callback) -> HSBonjourSearch
Searches for domains visible to this machine (browsable domains).
If a browsable-domain search is already active it is stopped before
starting the new one. Service and registration-domain searches are
unaffected. The callback receives `(event, domain, moreComing)`.
Declaration
findBrowsableDomains(callback) -> HSBonjourSearch
Parameters
| Name | Type | Description |
|---|---|---|
| callback | JSValue | `function(event, domain, moreComing)` called for each result |
Returns
HSBonjourSearch
self, for chaining
Example
search.findBrowsableDomains((ev, domain, more) => {
if (ev === 'domainFound') console.log('Domain:', domain)
})
findRegistrationDomains(callback) -> HSBonjourSearch
Searches for domains on which this machine can register services.
If a registration-domain search is already active it is stopped before
starting the new one. Service and browsable-domain searches are
unaffected. The callback receives `(event, domain, moreComing)`.
Declaration
findRegistrationDomains(callback) -> HSBonjourSearch
Parameters
| Name | Type | Description |
|---|---|---|
| callback | JSValue | `function(event, domain, moreComing)` called for each result |
Returns
HSBonjourSearch
self, for chaining
Example
search.findRegistrationDomains((ev, domain, more) => {
if (ev === 'domainFound') console.log('Can register in:', domain)
})
stop() -> HSBonjourSearch
Stops all active searches. Safe to call when no search is active.
Declaration
stop() -> HSBonjourSearch
Returns
HSBonjourSearch
self, for chaining
Example
search.stop()