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.

202 lines
5.6 KiB

1 month ago
  1. # bonjour-service
  2. ![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/onlxltd/bonjour-service/publish-release.yml?style=flat-square) ![npm bundle size](https://img.shields.io/bundlephobia/min/bonjour-service?style=flat-square) ![GitHub Release Date](https://img.shields.io/github/release-date/onlxltd/bonjour-service?style=flat-square) ![npm](https://img.shields.io/npm/dw/bonjour-service?color=%23888&style=flat-square) ![GitHub](https://img.shields.io/github/license/onlxltd/bonjour-service?style=flat-square) [![DeepScan grade](https://deepscan.io/api/teams/13435/projects/16430/branches/352351/badge/grade.svg?style=flat-square)](https://deepscan.io/dashboard#view=project&tid=13435&pid=16430&bid=352351)
  3. A Bonjour/Zeroconf protocol implementation in TypeScript. Publish
  4. services on the local network or discover existing services using
  5. multicast DNS.
  6. This is a rewrite of the project Bonjour (https://github.com/watson/bonjour) into modern TypeScript.
  7. 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).
  8. ## Installation
  9. Add to your project dependencies using Yarn or NPM.
  10. #### Install with Yarn
  11. ```
  12. yarn add bonjour-service
  13. ```
  14. #### Install with NPM
  15. ```
  16. npm install bonjour-service
  17. ```
  18. ## Usage
  19. ```js
  20. import { Bonjour } from 'bonjour-service'
  21. const instance = new Bonjour()
  22. // advertise an HTTP server on port 3000
  23. instance.publish({ name: 'My Web Server', type: 'http', port: 3000 })
  24. // browse for all http services
  25. instance.find({ type: 'http' }, function (service) {
  26. console.log('Found an HTTP server:', service)
  27. })
  28. ```
  29. ## API
  30. ### Initializing
  31. ```js
  32. var instance = new Bonjour({ options }, errorCallback)
  33. ```
  34. The `options` are optional and will be used when initializing the
  35. underlying multicast-dns server. For details see [the multicast-dns
  36. documentation](https://github.com/mafintosh/multicast-dns#mdns--multicastdnsoptions).
  37. `errorCallback` is an optional callback used to gracefully handle errors that would otherwise
  38. crash the process. While not being strictly required, providing this is highly recommended
  39. ### Publishing
  40. #### `var service = bonjour.publish(options)`
  41. Publishes a new service.
  42. Options are:
  43. - `name` (string)
  44. - `host` (string, optional) - defaults to local hostname
  45. - `port` (number)
  46. - `type` (string)
  47. - `subtypes` (array of strings, optional)
  48. - `protocol` (string, optional) - `udp` or `tcp` (default)
  49. - `txt` (object, optional) - a key/value object to broadcast as the TXT
  50. record
  51. - `disableIPv6` (boolean, optional) disble IPv6 addresses
  52. IANA maintains a [list of official service types and port
  53. numbers](http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml).
  54. #### `bonjour.unpublishAll([callback])`
  55. Unpublish all services. The optional `callback` will be called when the
  56. services have been unpublished.
  57. #### `bonjour.destroy()`
  58. Destroy the mdns instance. Closes the udp socket.
  59. ### Browser
  60. #### `var browser = bonjour.find(options[, onup])`
  61. Listen for services advertised on the network. An optional callback can
  62. be provided as the 2nd argument and will be added as an event listener
  63. for the `up` event.
  64. Options (all optional):
  65. - `type` (string)
  66. - `subtypes` (array of strings)
  67. - `protocol` (string) - defaults to `tcp`
  68. - `txt` (object) - passed into [dns-txt
  69. module](https://github.com/watson/dns-txt) contructor. Set to `{
  70. binary: true }` if you want to keep the TXT records in binary
  71. #### `var browser = bonjour.findOne(options[, callback])`
  72. Listen for and call the `callback` with the first instance of a service
  73. matching the `options`. If no `callback` is given, it's expected that
  74. you listen for the `up` event. The returned `browser` will automatically
  75. stop it self after the first matching service.
  76. Options are the same as given in the `browser.find` function.
  77. #### `Event: up`
  78. Emitted every time a new service is found that matches the browser.
  79. #### `Event: down`
  80. Emitted every time an existing service emmits a goodbye message.
  81. #### `Event: txt-update`
  82. Emitted every time an existing service does a new announcement with an updated TXT record.
  83. #### `browser.services()`
  84. An array of services known by the browser to be online.
  85. #### `browser.start()`
  86. Start looking for matching services.
  87. #### `browser.stop()`
  88. Stop looking for matching services.
  89. #### `browser.update()`
  90. Broadcast the query again.
  91. ### Service
  92. #### `Event: up`
  93. Emitted when the service is up.
  94. #### `Event: error`
  95. Emitted if an error occurrs while publishing the service.
  96. #### `service.stop([callback])`
  97. Unpublish the service. The optional `callback` will be called when the
  98. service have been unpublished.
  99. #### `service.start()`
  100. Publish the service.
  101. #### `service.name`
  102. The name of the service, e.g. `Apple TV`.
  103. #### `service.type`
  104. The type of the service, e.g. `http`.
  105. #### `service.subtypes`
  106. An array of subtypes. Note that this property might be `null`.
  107. #### `service.protocol`
  108. The protocol used by the service, e.g. `tcp`.
  109. #### `service.host`
  110. The hostname or ip address where the service resides.
  111. #### `service.port`
  112. The port on which the service listens, e.g. `5000`.
  113. #### `service.fqdn`
  114. The fully qualified domain name of the service. E.g. if given the name
  115. `Foo Bar`, the type `http` and the protocol `tcp`, the `service.fqdn`
  116. property will be `Foo Bar._http._tcp.local`.
  117. #### `service.txt`
  118. The TXT record advertised by the service (a key/value object). Note that
  119. this property might be `null`.
  120. #### `service.published`
  121. A boolean indicating if the service is currently published.
  122. ## License
  123. MIT