{"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install multicast-dns"],"cli":null},"imports":["const mdns = require('multicast-dns')();","import createMdns from 'multicast-dns';\nconst mdns = createMdns();","import type { Packet, Question, Answer } from 'multicast-dns';"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}