hs.bonjour
ModuleDiscover and publish Bonjour (mDNS / Zeroconf) network services.
Use createSearch() to search the network for services advertised by other
devices, and advertise() to advertise your own. The networkServices()
convenience function returns a snapshot of all service types currently
active on the local network.
Common service type strings
The hs.bonjour.serviceTypes object maps short names to their mDNS strings,
e.g. hs.bonjour.serviceTypes.ssh → "_ssh._tcp.".
Searching for a service
// Find all SSH services on the local network and resolve each one
const search = hs.bonjour.createSearch()
search.findServices('_ssh._tcp.', 'local.', (event, svc, moreComing) => {
if (event === 'serviceFound') {
svc.resolve(5, ev => {
if (ev === 'resolved') console.log(svc.hostname + ':' + svc.port)
})
}
})
Advertising a service
hs.bonjour.advertise('My Web Server', '_http._tcp.', 8080, ev => {
if (ev === 'published') console.log('Now advertising!')
else if (ev === 'error') console.error('Advertising failed')
})
// Later, to stop:
hs.bonjour.stopAdvertising('My Web Server', '_http._tcp.')
Listing all active service types
hs.bonjour.networkServices(5).then(types => {
console.log('Active service types: ' + types.join(', '))
})
Types
This module provides the following types:
Properties
hs.bonjour.serviceTypes
{[key: string]: string}
A frozen object mapping short service-type names to their mDNS strings.
Populated by the JavaScript enhancement layer.
Methods
hs.bonjour.createSearch() -> HSBonjourSearch
Creates a new Bonjour search for discovering services or domains.
Call one of the `find…` methods on the returned search to start
discovering. Remove it with `removeSearch()` when finished.
Declaration
hs.bonjour.createSearch() -> HSBonjourSearch
Returns
HSBonjourSearch
a new `HSBonjourSearch`
Example
const search = hs.bonjour.createSearch()
search.findServices('_http._tcp.', 'local.', (ev, svc, more) => {
if (ev === 'serviceFound') console.log('Found:', svc.name)
})
hs.bonjour.removeSearch(search) -> None
Stops and removes a previously created search.
Declaration
hs.bonjour.removeSearch(search) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| search | HSBonjourSearch | the search returned by `createSearch()` |
Returns
None
Example
const s = hs.bonjour.createSearch()
// ... use search ...
hs.bonjour.removeSearch(s)
hs.bonjour.advertise(name, type, port, domain, callback) -> None
Starts advertising a local service on the network.
If `domain` is omitted or not a string, it defaults to `"local."`.
If the 4th argument is a function, it is used as the callback and domain
defaults to `"local."`.
Declaration
hs.bonjour.advertise(name, type, port, domain, callback) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| name | string | human-readable name shown to browsers (e.g. `"My Web Server"`) |
| type | string | service type in `"_proto._tcp."` or `"_proto._udp."` form |
| port | Int32 | port number the service listens on |
| domain | JSValue | mDNS domain; defaults to `"local."` if omitted |
| callback | JSValue | optional `function(event, data?)` called on status changes |
Returns
None
Example
hs.bonjour.advertise('My Server', '_http._tcp.', 8080, ev => {
if (ev === 'published') console.log('Now advertising!')
})
hs.bonjour.stopAdvertising(name, type) -> None
Stops advertising a service previously started with `advertise()`.
Declaration
hs.bonjour.stopAdvertising(name, type) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| name | string | the name passed to `advertise()` |
| type | string | the type passed to `advertise()` |
Returns
None
Example
hs.bonjour.stopAdvertising('My Server', '_http._tcp.')
hs.bonjour.networkServices(timeout) -> Promise<string[]>
Returns a Promise that resolves to an array of service-type strings
currently advertised on the local network.
Internally searches for `_services._dns-sd._udp.` services, collects
results for up to `timeout` seconds (or until the browser signals no more
results), then resolves.
Declaration
hs.bonjour.networkServices(timeout) -> Promise<string[]>
Parameters
| Name | Type | Description |
|---|---|---|
| timeout | number | maximum seconds to wait (pass `0` to use the default 5 s) |
Returns
Promise<string[]>
a Promise resolving to an array of service-type strings such as `"_http._tcp."`
Example
hs.bonjour.networkServices(5).then(types => {
console.log('Active service types: ' + types.join(', '))
})