You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.3 KiB

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