bonjour-service is a TypeScript implementation of the Bonjour/Zeroconf protocol, enabling service publishing and discovery on local networks using multicast DNS. It functions as a modern rewrite of the popular `watson/bonjour` package, bringing contemporary TypeScript practices and improved maintainability. Currently stable at version 1.3.0, the package sees periodic updates, often including dependency bumps and minor feature enhancements, as indicated by recent changelogs. Its primary differentiator is providing a reliable, actively maintained, and type-safe solution for mDNS/DNS-SD within the Node.js ecosystem, suitable for applications requiring local network service announcement and detection, such as IoT devices or local development tooling. It offers functionalities to advertise services (e.g., HTTP servers) and browse for existing ones, handling the underlying UDP socket communication and DNS packet parsing.
npm install bonjour-serviceVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to publish an HTTP service and simultaneously discover other HTTP services on the local network using `bonjour-service`.
Adjust firewall settings to allow UDP traffic on port 5353 for the Node.js process. On Linux, consider `sudo ufw allow 5353/udp`.
Initialize Bonjour with an error callback: `const bonjour = new Bonjour({}, (err) => console.error('Bonjour error:', err));`Dynamically generate or append a unique identifier (e.g., hostname, UUID) to service names, especially in environments where multiple instances of the same service might run concurrently.
Always call `bonjour.destroy()` when your application is shutting down or no longer needs service discovery/publishing. For individual services/browsers, use `service.stop()` and `browser.stop()`.
Ensure only one instance of `bonjour-service` (or any other mDNS client/server) is running at a time. If running multiple, ensure each `Bonjour` instance has its own unique port configuration if necessary, though mDNS typically operates on 5353. For system-wide conflicts, identify and stop the conflicting process.
This is often transient. Ensure the target device is online and accessible on the local network. Implement retries or a more robust service tracking logic in your application to handle temporary resolution failures.
If using CommonJS, try `const { Bonjour } = require('bonjour-service');`. If using ES modules, ensure your environment supports `import` syntax and you are using `import { Bonjour } from 'bonjour-service';`.