Low level Multicast DNS implementation in pure javascript.


001.0.63 years ago3 years agoMinified + gzip package size for @alcalzone/mdns-server in KB



Low level Multicast DNS implementation in pure javascript. This is a fork of Bryan Nielsen's version, with a small bug fix.

Based on multicast-dns by Mathias Buus.

Can be configured to work with a single network interface or multiple network interfaces. When no interface is specified the OS package is used to detect all available interfaces.

npm install mdns-server


var mdns = require('mdns-server')({
  reuseAddr: true, // in case other mdns service is running
  loopback: true,  // receive our own mdns messages
  noInit: true     // do not initialize on creation

// listen for response events from server
mdns.on('response', function(response) {
  console.log('got a response packet:')
  var a = []
  if (response.answers) {
    a = a.concat(response.answers)
  if (response.additionals) {
    a = a.concat(response.additionals)

// listen for query events from server
mdns.on('query', function(query) {
  console.log('got a query packet:')
  var q = []
  if (query.questions) {
    q = q.concat(query.questions)

// listen for the server being destroyed
mdns.on('destroyed', function () {
  console.log('Server destroyed.')

// query for 'foo.local' A record when the server is ready
mdns.on('ready', function () {
      name: 'foo.local',
      type: 'A'

// initialize the server now that we are watching for events

// destroy the server after 10 seconds
setTimeout(function () { mdns.destroy() }, 10000)

query packets

In this example a query will be sent on all available IPv4 and IPv6 interfaces. With loopback enabled these queries should be recorded by the server. I.E.

got a query packet:
[ { name: 'foo.local', type: 'A', class: 1 } ]
got a query packet:
[ { name: 'foo.local', type: 'A', class: 1 } ]

Here there were two query packets because the system has both an IPv4 and IPv6 address on one interface.

response packets

Assuming a device on the local network matches the query request for 'foo.local' the server should record a response similar to the following...

got a response packet:
[ { name: 'foo.local',
    type: 'A',
    class: 1,
    ttl: 120,
    flush: true,
    data: '' } ]


A packet has the following format

  questions: [{
  answers: [{
    data:(record type specific data)
  additionals: [
    (same format as answers)
  authorities: [
    (same format as answers)

Currently data from SRV, A, PTR, TXT, AAAA and HINFO records is passed

mdns = mdns-server([options])

Creates a new mdns instance. Options can contain the following

  reuseAddr: (true) || Boolean,
  interfaces: (null) || String || [String, String, ...],
  ttl: (255) || Integer,
  loopback: (true) || Boolean,
  noInit: (false) || Boolean


Determines if socket will be allowed to reuse an address already in use. (requires node >=0.11.13) This is helpful if another process is also listening for mDNS packets.


Specify the interface or interfaces to be used on the mDNS server. If no interfaces are specified then all available network interfaces will be used. To select a single interface set interfaces to the ip address of the selected interface. To select multiple interfaces use an array of ip addresses.

For backward compatability with multicast-dns interface may be used in place of interfaces.

single interface
var mdns = require('mdns-server')({
  interfaces: ""
multiple interfaces
var mdns = require('mdns-server')({
  interfaces: ["", "", "fe80::e03c:16d2:1ba:e62"]


Set the multicast ttl.


Set whether server will report on mDNS packets originating from the same machine.


Control automatic server initialization. If set to false then server will not automatically initialize and the initServer() method must be called when your application is ready.


mdns.on('query', (packet, rinfo))

Emitted when a query packet is received.

mdns.on('query', function(query) {
  if (query.questions[0] && query.questions[0].name === 'brunhilde.local') {
    mdns.respond(someResponse) // see below

mdns.on('response', (packet, rinfo))

Emitted when a response packet is received.

The response might not be a response to a query you send as this is the result of someone multicasting a response.


mdns.query(packet, [cb])

Send a dns query. The callback will be called when the packet was sent.

The following shorthands are equivalent

mdns.query('brunhilde.local', 'A')
mdns.query([{name:'brunhilde.local', type:'A'}])
  questions: [{name:'brunhilde.local', type:'A'}]

mdns.respond(packet, [cb])

Send a dns response. The callback will be called when the packet was sent.

// reply with a SRV and a A record as an answer
  answers: [{
    name: 'my-service',
    type: 'SRV',
    data: {
      weigth: 0,
      priority: 10,
      target: 'my-service.example.com'
  }, {
    name: 'brunhilde.local',
    type: 'A',
    ttl: 300,
    data: ''

The following shorthands are equivalent

mdns.respond([{name:'brunhilde.local', type:'A', data:''}])
  answers: [{name:'brunhilde.local', type:'A', data:''}]


Destroy the mdns instance. Closes the udp socket.


Initialize the server. Only call this method if the constructor was called with the option noInit: true. This is used if your application needs some time to setup events before the server initialization.



If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.