| [9af201e] | 1 | # bonjour-service
|
|---|
| 2 |
|
|---|
| 3 |      [](https://deepscan.io/dashboard#view=project&tid=13435&pid=16430&bid=352351)
|
|---|
| 4 |
|
|---|
| 5 | A Bonjour/Zeroconf protocol implementation in TypeScript. Publish
|
|---|
| 6 | services on the local network or discover existing services using
|
|---|
| 7 | multicast DNS.
|
|---|
| 8 |
|
|---|
| 9 | This is a rewrite of the project Bonjour (https://github.com/watson/bonjour) into modern TypeScript.
|
|---|
| 10 |
|
|---|
| 11 | bonjour-service is supported by [ON LX Limited](https://onlx.ltd/?src=bonjour-service). Check out our projects such as [Ctrl Suite](https://onlx.ltd/ctrl-suite?src=bonjour-service) and [Ctrl for iPad](https://onlx.ltd/ctrl-for-ipad?src=bonjour-service).
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 | ## Installation
|
|---|
| 16 | Add to your project dependencies using Yarn or NPM.
|
|---|
| 17 |
|
|---|
| 18 | #### Install with Yarn
|
|---|
| 19 | ```
|
|---|
| 20 | yarn add bonjour-service
|
|---|
| 21 | ```
|
|---|
| 22 | #### Install with NPM
|
|---|
| 23 | ```
|
|---|
| 24 | npm install bonjour-service
|
|---|
| 25 | ```
|
|---|
| 26 |
|
|---|
| 27 | ## Usage
|
|---|
| 28 |
|
|---|
| 29 | ```js
|
|---|
| 30 | import { Bonjour } from 'bonjour-service'
|
|---|
| 31 |
|
|---|
| 32 | const instance = new Bonjour()
|
|---|
| 33 |
|
|---|
| 34 | // advertise an HTTP server on port 3000
|
|---|
| 35 | instance.publish({ name: 'My Web Server', type: 'http', port: 3000 })
|
|---|
| 36 |
|
|---|
| 37 | // browse for all http services
|
|---|
| 38 | instance.find({ type: 'http' }, function (service) {
|
|---|
| 39 | console.log('Found an HTTP server:', service)
|
|---|
| 40 | })
|
|---|
| 41 | ```
|
|---|
| 42 |
|
|---|
| 43 | ## API
|
|---|
| 44 |
|
|---|
| 45 | ### Initializing
|
|---|
| 46 |
|
|---|
| 47 | ```js
|
|---|
| 48 | var instance = new Bonjour({ options }, errorCallback)
|
|---|
| 49 | ```
|
|---|
| 50 |
|
|---|
| 51 | The `options` are optional and will be used when initializing the
|
|---|
| 52 | underlying multicast-dns server. For details see [the multicast-dns
|
|---|
| 53 | documentation](https://github.com/mafintosh/multicast-dns#mdns--multicastdnsoptions).
|
|---|
| 54 |
|
|---|
| 55 | `errorCallback` is an optional callback used to gracefully handle errors that would otherwise
|
|---|
| 56 | crash the process. While not being strictly required, providing this is highly recommended
|
|---|
| 57 |
|
|---|
| 58 | ### Publishing
|
|---|
| 59 |
|
|---|
| 60 | #### `var service = bonjour.publish(options)`
|
|---|
| 61 |
|
|---|
| 62 | Publishes a new service.
|
|---|
| 63 |
|
|---|
| 64 | Options are:
|
|---|
| 65 |
|
|---|
| 66 | - `name` (string)
|
|---|
| 67 | - `host` (string, optional) - defaults to local hostname
|
|---|
| 68 | - `port` (number)
|
|---|
| 69 | - `type` (string)
|
|---|
| 70 | - `subtypes` (array of strings, optional)
|
|---|
| 71 | - `protocol` (string, optional) - `udp` or `tcp` (default)
|
|---|
| 72 | - `txt` (object, optional) - a key/value object to broadcast as the TXT
|
|---|
| 73 | record
|
|---|
| 74 | - `disableIPv6` (boolean, optional) disble IPv6 addresses
|
|---|
| 75 |
|
|---|
| 76 | IANA maintains a [list of official service types and port
|
|---|
| 77 | numbers](http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml).
|
|---|
| 78 |
|
|---|
| 79 | #### `bonjour.unpublishAll([callback])`
|
|---|
| 80 |
|
|---|
| 81 | Unpublish all services. The optional `callback` will be called when the
|
|---|
| 82 | services have been unpublished.
|
|---|
| 83 |
|
|---|
| 84 | #### `bonjour.destroy()`
|
|---|
| 85 |
|
|---|
| 86 | Destroy the mdns instance. Closes the udp socket.
|
|---|
| 87 |
|
|---|
| 88 | ### Browser
|
|---|
| 89 |
|
|---|
| 90 | #### `var browser = bonjour.find(options[, onup])`
|
|---|
| 91 |
|
|---|
| 92 | Listen for services advertised on the network. An optional callback can
|
|---|
| 93 | be provided as the 2nd argument and will be added as an event listener
|
|---|
| 94 | for the `up` event.
|
|---|
| 95 |
|
|---|
| 96 | Options (all optional):
|
|---|
| 97 |
|
|---|
| 98 | - `type` (string)
|
|---|
| 99 | - `subtypes` (array of strings)
|
|---|
| 100 | - `protocol` (string) - defaults to `tcp`
|
|---|
| 101 | - `txt` (object) - passed into [dns-txt
|
|---|
| 102 | module](https://github.com/watson/dns-txt) contructor. Set to `{
|
|---|
| 103 | binary: true }` if you want to keep the TXT records in binary
|
|---|
| 104 |
|
|---|
| 105 | #### `var browser = bonjour.findOne(options[, callback])`
|
|---|
| 106 |
|
|---|
| 107 | Listen for and call the `callback` with the first instance of a service
|
|---|
| 108 | matching the `options`. If no `callback` is given, it's expected that
|
|---|
| 109 | you listen for the `up` event. The returned `browser` will automatically
|
|---|
| 110 | stop it self after the first matching service.
|
|---|
| 111 |
|
|---|
| 112 | Options are the same as given in the `browser.find` function.
|
|---|
| 113 |
|
|---|
| 114 | #### `Event: up`
|
|---|
| 115 |
|
|---|
| 116 | Emitted every time a new service is found that matches the browser.
|
|---|
| 117 |
|
|---|
| 118 | #### `Event: down`
|
|---|
| 119 |
|
|---|
| 120 | Emitted every time an existing service emmits a goodbye message.
|
|---|
| 121 |
|
|---|
| 122 | #### `Event: txt-update`
|
|---|
| 123 |
|
|---|
| 124 | Emitted every time an existing service does a new announcement with an updated TXT record.
|
|---|
| 125 |
|
|---|
| 126 | #### `browser.services()`
|
|---|
| 127 |
|
|---|
| 128 | An array of services known by the browser to be online.
|
|---|
| 129 |
|
|---|
| 130 | #### `browser.start()`
|
|---|
| 131 |
|
|---|
| 132 | Start looking for matching services.
|
|---|
| 133 |
|
|---|
| 134 | #### `browser.stop()`
|
|---|
| 135 |
|
|---|
| 136 | Stop looking for matching services.
|
|---|
| 137 |
|
|---|
| 138 | #### `browser.update()`
|
|---|
| 139 |
|
|---|
| 140 | Broadcast the query again.
|
|---|
| 141 |
|
|---|
| 142 | ### Service
|
|---|
| 143 |
|
|---|
| 144 | #### `Event: up`
|
|---|
| 145 |
|
|---|
| 146 | Emitted when the service is up.
|
|---|
| 147 |
|
|---|
| 148 | #### `Event: error`
|
|---|
| 149 |
|
|---|
| 150 | Emitted if an error occurrs while publishing the service.
|
|---|
| 151 |
|
|---|
| 152 | #### `service.stop([callback])`
|
|---|
| 153 |
|
|---|
| 154 | Unpublish the service. The optional `callback` will be called when the
|
|---|
| 155 | service have been unpublished.
|
|---|
| 156 |
|
|---|
| 157 | #### `service.start()`
|
|---|
| 158 |
|
|---|
| 159 | Publish the service.
|
|---|
| 160 |
|
|---|
| 161 | #### `service.name`
|
|---|
| 162 |
|
|---|
| 163 | The name of the service, e.g. `Apple TV`.
|
|---|
| 164 |
|
|---|
| 165 | #### `service.type`
|
|---|
| 166 |
|
|---|
| 167 | The type of the service, e.g. `http`.
|
|---|
| 168 |
|
|---|
| 169 | #### `service.subtypes`
|
|---|
| 170 |
|
|---|
| 171 | An array of subtypes. Note that this property might be `null`.
|
|---|
| 172 |
|
|---|
| 173 | #### `service.protocol`
|
|---|
| 174 |
|
|---|
| 175 | The protocol used by the service, e.g. `tcp`.
|
|---|
| 176 |
|
|---|
| 177 | #### `service.host`
|
|---|
| 178 |
|
|---|
| 179 | The hostname or ip address where the service resides.
|
|---|
| 180 |
|
|---|
| 181 | #### `service.port`
|
|---|
| 182 |
|
|---|
| 183 | The port on which the service listens, e.g. `5000`.
|
|---|
| 184 |
|
|---|
| 185 | #### `service.fqdn`
|
|---|
| 186 |
|
|---|
| 187 | The fully qualified domain name of the service. E.g. if given the name
|
|---|
| 188 | `Foo Bar`, the type `http` and the protocol `tcp`, the `service.fqdn`
|
|---|
| 189 | property will be `Foo Bar._http._tcp.local`.
|
|---|
| 190 |
|
|---|
| 191 | #### `service.txt`
|
|---|
| 192 |
|
|---|
| 193 | The TXT record advertised by the service (a key/value object). Note that
|
|---|
| 194 | this property might be `null`.
|
|---|
| 195 |
|
|---|
| 196 | #### `service.published`
|
|---|
| 197 |
|
|---|
| 198 | A boolean indicating if the service is currently published.
|
|---|
| 199 |
|
|---|
| 200 | ## License
|
|---|
| 201 |
|
|---|
| 202 | MIT
|
|---|