API Docs

Discover 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.
hs.bonjour.createSearch() -> HSBonjourSearch
HSBonjourSearch
a new `HSBonjourSearch`
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.
hs.bonjour.removeSearch(search) -> None
Name Type Description
search HSBonjourSearch the search returned by `createSearch()`
None
const s = hs.bonjour.createSearch()
// ... use search ...
hs.bonjour.removeSearch(s)

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()`
None
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)
Promise<string[]>
a Promise resolving to an array of service-type strings such as `"_http._tcp."`
hs.bonjour.networkServices(5).then(types => {
    console.log('Active service types: ' + types.join(', '))
})