UNPKG

4.38 kBMarkdownView Raw
1# bonjour
2
3A Bonjour/Zeroconf protocol implementation in pure JavaScript. Publish
4services on the local network or discover existing services using
5multicast DNS.
6
7[![Build status](https://travis-ci.org/watson/bonjour.svg?branch=master)](https://travis-ci.org/watson/bonjour)
8[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://github.com/feross/standard)
9
10## Installation
11
12```
13npm install bonjour
14```
15
16## Usage
17
18```js
19var bonjour = require('bonjour')()
20
21// advertise an HTTP server on port 3000
22bonjour.publish({ name: 'My Web Server', type: 'http', port: 3000 })
23
24// browse for all http services
25bonjour.find({ type: 'http' }, function (service) {
26 console.log('Found an HTTP server:', service)
27})
28```
29
30## API
31
32### Initializing
33
34```js
35var bonjour = require('bonjour')([options])
36```
37
38The `options` are optional and will be used when initializing the
39underlying multicast-dns server. For details see [the multicast-dns
40documentation](https://github.com/mafintosh/multicast-dns#mdns--multicastdnsoptions).
41
42### Publishing
43
44#### `var service = bonjour.publish(options)`
45
46Publishes a new service.
47
48Options are:
49
50- `name` (string)
51- `host` (string, optional) - defaults to local hostname
52- `port` (number)
53- `type` (string)
54- `subtypes` (array of strings, optional)
55- `protocol` (string, optional) - `udp` or `tcp` (default)
56- `txt` (object, optional) - a key/value object to broadcast as the TXT
57 record
58
59IANA maintains a [list of official service types and port
60numbers](http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml).
61
62#### `bonjour.unpublishAll([callback])`
63
64Unpublish all services. The optional `callback` will be called when the
65services have been unpublished.
66
67#### `bonjour.destroy()`
68
69Destroy the mdns instance. Closes the udp socket.
70
71### Browser
72
73#### `var browser = bonjour.find(options[, onup])`
74
75Listen for services advertised on the network. An optional callback can
76be provided as the 2nd argument and will be added as an event listener
77for the `up` event.
78
79Options (all optional):
80
81- `type` (string)
82- `subtypes` (array of strings)
83- `protocol` (string) - defaults to `tcp`
84- `txt` (object) - passed into [dns-txt
85 module](https://github.com/watson/dns-txt) contructor. Set to `{
86 binary: true }` if you want to keep the TXT records in binary
87
88#### `var browser = bonjour.findOne(options[, callback])`
89
90Listen for and call the `callback` with the first instance of a service
91matching the `options`. If no `callback` is given, it's expected that
92you listen for the `up` event. The returned `browser` will automatically
93stop it self after the first matching service.
94
95Options are the same as given in the `browser.find` function.
96
97#### `Event: up`
98
99Emitted every time a new service is found that matches the browser.
100
101#### `Event: down`
102
103Emitted every time an existing service emmits a goodbye message.
104
105#### `browser.services`
106
107An array of services known by the browser to be online.
108
109#### `browser.start()`
110
111Start looking for matching services.
112
113#### `browser.stop()`
114
115Stop looking for matching services.
116
117#### `browser.update()`
118
119Broadcast the query again.
120
121### Service
122
123#### `Event: up`
124
125Emitted when the service is up.
126
127#### `Event: error`
128
129Emitted if an error occurrs while publishing the service.
130
131#### `service.stop([callback])`
132
133Unpublish the service. The optional `callback` will be called when the
134service have been unpublished.
135
136#### `service.start()`
137
138Publish the service.
139
140#### `service.name`
141
142The name of the service, e.g. `Apple TV`.
143
144#### `service.type`
145
146The type of the service, e.g. `http`.
147
148#### `service.subtypes`
149
150An array of subtypes. Note that this property might be `null`.
151
152#### `service.protocol`
153
154The protocol used by the service, e.g. `tcp`.
155
156#### `service.host`
157
158The hostname or ip address where the service resides.
159
160#### `service.port`
161
162The port on which the service listens, e.g. `5000`.
163
164#### `service.fqdn`
165
166The fully qualified domain name of the service. E.g. if given the name
167`Foo Bar`, the type `http` and the protocol `tcp`, the `service.fqdn`
168property will be `Foo Bar._http._tcp.local`.
169
170#### `service.txt`
171
172The TXT record advertised by the service (a key/value object). Note that
173this property might be `null`.
174
175#### `service.published`
176
177A boolean indicating if the service is currently published.
178
179## License
180
181MIT