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
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.
hs.bonjour.createSearch() -> HSBonjourSearch
HSBonjourSearchconst 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.
hs.bonjour.removeSearch(search) -> None
| Name | Type | Description |
|---|---|---|
| search | HSBonjourSearch | the search returned by createSearch() |
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.
The optional callback receives (event) or (event, errorMessage):
"published"— now advertising"stopped"— advertisement stopped"error"— publication failed; error message in second argument
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.".
hs.bonjour.advertise(name, type, port, domain, callback) -> None
| 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 | number | port number the service listens on |
| domain | string | mDNS domain; defaults to "local." if an empty string is passed |
| callback | function | Optional function called on status changes with event name and optional error message |
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().
hs.bonjour.stopAdvertising(name, type) -> None
| Name | Type | Description |
|---|---|---|
| name | string | the name passed to advertise() |
| type | string | the type passed to advertise() |
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.
hs.bonjour.networkServices(timeout) -> Promise<string[]>
| Name | Type | Description |
|---|---|---|
| timeout | number | maximum seconds to wait (pass 0 to use the default 5 s) |
"_http._tcp."hs.bonjour.networkServices(5).then(types => {
console.log('Active service types: ' + types.join(', '))
})