The `bonjour` package provides a pure JavaScript implementation of the Bonjour (also known as Zeroconf or mDNS/DNS-SD) protocol for Node.js environments. It allows applications to publish services on the local network, making them discoverable by other Bonjour-compatible devices, and to discover services advertised by others. The library is currently at version 3.5.1 and has an infrequent, as-needed release cadence. Its key differentiator is its full implementation in JavaScript, relying on the `multicast-dns` package for the underlying DNS operations. Unlike some alternatives, it offers both service advertisement and discovery capabilities within a single, lightweight API, making it suitable for local network peer-to-peer communication without central servers.
npm install bonjourVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize `bonjour`, publish an HTTP service, and then discover existing HTTP services on the local network, with proper cleanup.
Always ensure `bonjour.destroy()` is called on application shutdown, for instance, by listening to `process.on('exit')` or `process.on('SIGINT')`.Verify network settings, ensure UDP ports are open (typically 5353 for mDNS), and test within the same local subnet. Avoid using VPNs during development if Bonjour discovery is critical.
Implement robust handling for service presence, assuming services might appear or disappear with some delay. Consider a debounce or timeout mechanism for reacting to service discovery events.
For new projects, consider `npm install bonjour-service` and consult its documentation for usage. For existing projects, be aware that `bonjour` may not receive significant new features or compliance updates.
Ensure no other Bonjour/mDNS service (or another instance of your application) is running. On some systems, restarting the machine can resolve this. You can also pass custom `port` options to the `multicast-dns` server when initializing `bonjour` if necessary, though this might hinder interoperability.
Double-check that both the publishing and browsing applications are on the same local network segment and are using identical service `type` strings. Temporarily disable firewalls for testing. Use network debugging tools (like Wireshark) to inspect mDNS traffic on port 5353.
Use `const bonjour = require('bonjour')();` for CommonJS or `import bonjourFactory from 'bonjour'; const bonjour = bonjourFactory();` for ESM to correctly initialize the Bonjour instance.