# @samtec-ash/bonjour-mdns

> Low level multicast-dns implementation in pure javascript

Latest version **0.0.3** (published 2018-06-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @samtec-ash/bonjour-mdns
pnpm add @samtec-ash/bonjour-mdns
yarn add @samtec-ash/bonjour-mdns
bun add @samtec-ash/bonjour-mdns
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.3 |
| Published | 2018-06-04 |
| First published | 2018-06-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 51.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Nathan Page |
| Maintainers | adamtec, nathanpage, virtchris |
| Keywords | multicast, dns, mdns, multicastdns, dns-sd, service, discovery, bonjour, avahi |

## Links

- npm: https://www.npmjs.com/package/@samtec-ash/bonjour-mdns
- Repository: https://bitbucket.org/samteccmd/bonjour-mdns
- npm.io page: https://npm.io/package/@samtec-ash/bonjour-mdns

## Dependencies (6)

- [dns-txt](https://npm.io/package/dns-txt.md) ^2.0.2
- [dns-equal](https://npm.io/package/dns-equal.md) ^1.0.0
- [deep-equal](https://npm.io/package/deep-equal.md) ^1.0.1
- [dns-packet](https://npm.io/package/dns-packet.md) ^1.0.1
- [array-flatten](https://npm.io/package/array-flatten.md) ^2.1.1
- [multicast-dns-service-types](https://npm.io/package/multicast-dns-service-types.md) ^1.1.0

## Alternatives

- [@mdxeditor/editor](https://npm.io/package/@mdxeditor/editor.md) — 962.4K weekly downloads
- [mmdb-lib](https://npm.io/package/mmdb-lib.md) — 680.9K weekly downloads
- [playcanvas](https://npm.io/package/playcanvas.md) — 36.2K weekly downloads
- [@glw907/cairn-cms](https://npm.io/package/@glw907/cairn-cms.md) — 967 weekly downloads
- [markdown-to-confluence](https://npm.io/package/markdown-to-confluence.md) — 103 weekly downloads

## Recent versions

- 0.0.3 (latest) — 2018-06-04
- 0.0.2 — 2018-06-04
- 0.0.1 — 2018-06-04

## README

# multicast-dns

Low level multicast-dns implementation in pure javascript

```
npm install multicast-dns
```

[![build status](http://img.shields.io/travis/mafintosh/multicast-dns.svg?style=flat)](http://travis-ci.org/mafintosh/multicast-dns)

## Usage

``` js
var mdns = require('multicast-dns')()

mdns.on('response', function(response) {
  console.log('got a response packet:', response)
})

mdns.on('query', function(query) {
  console.log('got a query packet:', query)
})

// lets query for an A record for 'brunhilde.local'
mdns.query({
  questions:[{
    name: 'brunhilde.local',
    type: 'A'
  }]
})
```

Running the above (change `brunhilde.local` to `your-own-hostname.local`) will print an echo of the query packet first

``` js
got a query packet: { type: 'query',
  questions: [ { name: 'brunhilde.local', type: 'A', class: 1 } ],
  answers: [],
  authorities: [],
  additionals: [] }
```

And then a response packet

``` js
got a response packet: { type: 'response',
  questions: [],
  answers:
   [ { name: 'brunhilde.local',
       type: 'A',
       class: 1,
       ttl: 120,
       flush: true,
       data: '192.168.1.5' } ],
  authorities: [],
  additionals:
   [ { name: 'brunhilde.local',
       type: 'A',
       class: 1,
       ttl: 120,
       flush: true,
       data: '192.168.1.5' },
     { name: 'brunhilde.local',
       type: 'AAAA',
       class: 1,
       ttl: 120,
       flush: true,
       data: 'fe80::5ef9:38ff:fe8c:ceaa' } ] }
```


# CLI

```
npm install -g multicast-dns
```

```
multicast-dns brunhilde.local
> 192.168.1.1
```

# API

A packet has the following format

``` js
{
  questions: [{
    name:'brunhilde.local',
    type:'A'
  }],
  answers: [{
    name:'brunhilde.local',
    type:'A',
    ttl:seconds,
    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 = multicastdns([options])`

Creates a new `mdns` instance. Options can contain the following

``` js
{
  multicast: true // use udp multicasting
  interface: '192.168.0.2' // explicitly specify a network interface.
  interfaces: ['192.168.0.2','10.0.0.2'] // explicitly specify a set of network interfaces.
  subnets: ['192.168.0.'], // specify a set of subnet interested, the current value represent 192.168.0.0/24
  port: 5353, // set the udp port
  group_ip: '224.0.0.251', // set the udp multicast group ip
  ttl: 255, // set the multicast ttl
  loopback: true, // receive your own packets
  reuseAddr: true // set the reuseAddr option when creating the socket (requires node >=0.11.13)
  use_group_ip: true // bind the multicasting server socket to the multicast group ip in non-Windows systems
  client_only: true // do not initiate the dns server, use the client component only
}
```

If neighter `interface` nor `interface` is specified, the lib will generate a interface list by first enumerating all network interfaces found on the host. If `subnets` is a non-empty list, the interfaces used will be obtained by filtering the original list using the values in the `subnets`. If `subnets` is not defined or is empty, the original list will be used. If `interface` is defined, it is used as the sole interface used, regardless of the values of `interfaces` or `subnets`.
If `interface` not defined but `interfaces` is defined, it is used as the list of interfaces used, regardless of the values of `subnets`.

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

Emitted when a query packet is received.

``` js
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

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

#### `mdns.respond(packet, [cb])`

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

``` js
// reply with a SRV and a A record as an answer
mdns.respond({
  answers: [{
    name: 'my-service',
    type: 'SRV',
    data: {
      port:9999,
      weigth: 0,
      priority: 10,
      target: 'my-service.example.com'
    }
  }, {
    name: 'brunhilde.local',
    type: 'A',
    ttl: 300,
    data: '192.168.1.5'
  }]
})
```

The following shorthands are equivalent

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

#### `mdns.destroy()`

Destroy the mdns instance. Closes the udp socket.

# Development

To start hacking on this module you can use this example to get started

```
git clone git://github.com/mafintosh/multicast-dns.git
npm install
node example.js
node cli.js $(hostname).local
```

## License

MIT

---
_Source: https://npm.io/package/@samtec-ash/bonjour-mdns · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
