The Browser class is an EventEmitter used to discover services on a local network via mDNS. It listens for PTR records of a specific type and protocol (e.g., _http._tcp.local) and maintains an internal list of online services.
Lifecycle and Events
- Discovery: When a new service is discovered, it is added to the
services array and an up event is emitted. - Removal: When a service is no longer available (detected via 'goodbye' announcements with a TTL of 0), it is removed from the list and a
down event is emitted. - Wildcard Search: If no
type is provided in the options, the browser performs a wildcard search (_services._dns-sd._udp.local), discovering all available services on the network.
Service Object Structure
When a service is discovered, the emitted object contains:
name: The service name.fqdn: The fully qualified domain name.host: The target hostname.port: The service port.type: The service type.protocol: The service protocol (e.g., tcp).addresses: An array of IP addresses (A or AAAA records) associated with the host.txt: A decoded object of the service's TXT records.rawTxt: The raw buffer of the TXT record.
const Browser = require('bonjour').Browser;
// Note: Browser requires an mdns instance as the first argument
const browser = new Browser(mdnsInstance, { type: 'http', protocol: 'tcp' });
browser.on('up', (service) => {
console.log('Service discovered:', service.name, service.addresses);
});
browser.on('down', (service) => {
console.log('Service went down:', service.name);
});