{"id":14715,"library":"multicast-dns","title":"Multicast DNS (mDNS) Implementation","description":"multicast-dns is a low-level, pure JavaScript implementation of the Multicast DNS (mDNS) protocol, widely used for zero-configuration service discovery, commonly known as Apple's Bonjour or Avahi. It facilitates device and service discovery on a local network by sending DNS queries over UDP multicast to the standard address `224.0.0.251:5353` and listening for corresponding responses. The current stable version, 7.2.5, provides an event-driven API for both querying for and responding to mDNS packets. It supports various DNS record types including A, AAAA, PTR, SRV, TXT, and HINFO, allowing applications to discover network services and resolve local hostnames. The library offers fine-grained control over network interfaces, ports, and other UDP socket options, making it a foundational component for peer-to-peer and local area network applications that require automatic service registration and discovery without relying on a central DNS server. It maintains a focus on low-level network interactions rather than abstracting into a high-level service browser.","status":"active","version":"7.2.5","language":"javascript","source_language":"en","source_url":"https://github.com/mafintosh/multicast-dns","tags":["javascript","multicast","dns","mdns","multicastdns","dns-sd","service","discovery","bonjour"],"install":[{"cmd":"npm install multicast-dns","lang":"bash","label":"npm"},{"cmd":"yarn add multicast-dns","lang":"bash","label":"yarn"},{"cmd":"pnpm add multicast-dns","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The `multicast-dns` package exports a factory function that must be immediately invoked to create an mDNS instance.","wrong":"const mdns = require('multicast-dns');","symbol":"mdns instance (CommonJS)","correct":"const mdns = require('multicast-dns')();"},{"note":"For ESM, import the default export (the factory function) and then call it to get a new mDNS instance. The package's primary documentation showcases CommonJS usage.","wrong":"import { mdns } from 'multicast-dns';","symbol":"mdns instance (ESM)","correct":"import createMdns from 'multicast-dns';\nconst mdns = createMdns();"},{"note":"While the library is JavaScript, TypeScript type definitions are often available via `@types/multicast-dns` for enhanced development experience. Common types include `Packet`, `Question`, and `Answer` for handling mDNS data structures.","wrong":"import { Packet } from 'multicast-dns';","symbol":"Packet Types (TypeScript)","correct":"import type { Packet, Question, Answer } from 'multicast-dns';"}],"quickstart":{"code":"import createMdns from 'multicast-dns';\n\nconst mdns = createMdns();\n\nmdns.on('response', function(response) {\n  console.log('Got a mDNS response packet:', JSON.stringify(response, null, 2));\n});\n\nmdns.on('query', function(query) {\n  console.log('Got a mDNS query packet:', JSON.stringify(query, null, 2));\n  // Example: Respond to a query for 'my-service.local'\n  if (query.questions[0] && query.questions[0].name === 'my-service.local') {\n    mdns.respond({\n      answers: [{\n        name: 'my-service.local',\n        type: 'A',\n        ttl: 120,\n        data: '127.0.0.1' // Replace with actual IP\n      }]\n    });\n    console.log('Responded to query for my-service.local');\n  }\n});\n\n// Query for an A record for a local hostname (e.g., your-hostname.local)\nmdns.query({\n  questions:[\n    {\n      name: 'your-hostname.local',\n      type: 'A'\n    },\n    {\n      name: 'my-service.local',\n      type: 'SRV'\n    }\n  ]\n});\n\nconsole.log('Sent mDNS query for your-hostname.local and my-service.local');\n\n// Destroy the instance after a timeout to prevent resource leaks in short-lived scripts\nsetTimeout(() => {\n  mdns.destroy();\n  console.log('mDNS instance destroyed.');\n}, 10000);","lang":"typescript","description":"This quickstart initializes a `multicast-dns` instance, logs incoming query and response packets, demonstrates how to query for specific records, and how to respond to queries for a known service, then destroys the instance."},"warnings":[{"fix":"Ensure your Node.js runtime is updated to a modern version (LTS recommended) or avoid using the `reuseAddr: true` option.","message":"The `reuseAddr` option for the underlying UDP socket requires Node.js version 0.11.13 or newer. Using it on older versions will result in an error or unexpected behavior.","severity":"gotcha","affected_versions":"<0.11.13"},{"fix":"Ensure port 5353 is open in your firewall for UDP traffic, or configure a different `port` option (though this would break standard mDNS discovery). If another service is listening, stop it or configure `multicast-dns` to use a different `interface` if available.","message":"The mDNS protocol operates over UDP port 5353, which is a well-known port. Firewalls or other services (like Bonjour/Avahi daemons) might block access or already be using this port, leading to `EADDRINUSE` or `EACCES` errors when binding the socket.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"For ESM, use `import createMdns from 'multicast-dns'; const mdns = createMdns();`. Avoid named imports like `{ mdns }` unless explicitly documented, as the default export is a function.","message":"The README and examples primarily demonstrate CommonJS (`require()`) usage. While modern Node.js supports ESM, careful consideration is needed for `import` statements, especially when the package exports a factory function that needs to be invoked immediately.","severity":"gotcha","affected_versions":">=6.0.0 (Node.js versions with ESM support)"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Stop the conflicting process, or configure your `multicast-dns` instance to bind to a specific `interface` if available, or a different `port` (which will prevent standard mDNS discovery for that instance).","cause":"Another process (e.g., a system mDNS responder like Bonjour or Avahi, or another instance of `multicast-dns`) is already listening on the default mDNS port and multicast address.","error":"Error: bind EADDRINUSE 224.0.0.251:5353"},{"fix":"Verify the IP address configured for the `interface` option is correct and corresponds to an active network interface on your machine. Remove the `interface` option to let `multicast-dns` bind to all available interfaces (default behavior).","cause":"The specified network `interface` in the options (e.g., `interface: '192.168.0.2'`) does not exist or is not available on the current system.","error":"Error: bind EADDRNOTAVAIL"},{"fix":"Ensure you are calling the imported module as a function: `const mdns = require('multicast-dns')();` for CommonJS, or `const mdns = createMdns();` after `import createMdns from 'multicast-dns';` for ESM.","cause":"The imported/required `multicast-dns` module was not invoked as a function to create an instance. For example, `const mdns = require('multicast-dns');` instead of `const mdns = require('multicast-dns')();`.","error":"TypeError: multicastdns is not a function"}],"ecosystem":"npm"}