{"_id":"@art-of-coding/bus","_rev":"3-05959487c38ea3c7c7a7dac03efbde49","name":"@art-of-coding/bus","description":"Fancy MQTT message bus manager ","dist-tags":{"latest":"0.0.1-alpha.3"},"versions":{"0.0.1-alpha.1":{"name":"@art-of-coding/bus","version":"0.0.1-alpha.1","description":"MQTT message bus","author":{"name":"Michiel van der Velde","email":"michiel@michielvdvelde.nl"},"license":"MIT","main":"dist/index.js","typings":"dist/index.d.ts","scripts":{},"engines":{"node":"=>7.10.0"},"repository":{"type":"git","url":"git+https://github.com/Art-of-Coding/bus.git"},"bugs":{"url":"https://github.com/Art-of-Coding/bus/issues"},"homepage":"https://github.com/Art-of-Coding/bus#readme","dependencies":{"@types/node":"^7.0.27","mqtt":"^2.8.1"},"devDependencies":{"typescript":"^2.3.4"},"gitHead":"576e090ef9341e6e0f9009bd89766b4addcc8eae","_id":"@art-of-coding/bus@0.0.1-alpha.1","_npmVersion":"5.0.2","_nodeVersion":"8.0.0","_npmUser":{"name":"michielvdvelde","email":"michiel@michielvdvelde.nl"},"dist":{"integrity":"sha512-WhUX0AQrfqR2aB60Sj1gCkrQMfISwWFh+y8d/l/ykCrDNZO/mh9MEJLYk2Igf7tdls6dG5bYnhVyc56QQyuq5A==","shasum":"86a9c15699296b979bcf56c2a2213e2d4d14754f","tarball":"https://registry.npmjs.org/@art-of-coding/bus/-/bus-0.0.1-alpha.1.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCHeIUE41P7kcgmYbW1uIcJKlN+28KG80C/xUwCcPbvNgIgJN1LaB6NMd9G6Z5kOP3VA44C9mtAuzatUvVVX11rouU="}]},"maintainers":[{"name":"michielvdvelde","email":"michiel@michielvdvelde.nl"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/bus-0.0.1-alpha.1.tgz_1496604662277_0.6398453724104911"}},"0.0.1-alpha.2":{"name":"@art-of-coding/bus","version":"0.0.1-alpha.2","description":"Fancy MQTT message bus manager ","author":{"name":"Michiel van der Velde","email":"michiel@michielvdvelde.nl"},"license":"MIT","main":"dist/index.js","typings":"dist/index.d.ts","engines":{"node":"=>7.10.0"},"repository":{"type":"git","url":"git+https://github.com/Art-of-Coding/bus.git"},"bugs":{"url":"https://github.com/Art-of-Coding/bus/issues"},"homepage":"https://github.com/Art-of-Coding/bus#readme","dependencies":{"@types/node":"^7.0.27","mqtt":"^2.8.1"},"devDependencies":{"typescript":"^2.3.4"},"gitHead":"ae34b1adfb5fb64699b7976687166004f4a282cc","_id":"@art-of-coding/bus@0.0.1-alpha.2","_npmVersion":"5.0.2","_nodeVersion":"8.0.0","_npmUser":{"name":"michielvdvelde","email":"michiel@michielvdvelde.nl"},"dist":{"integrity":"sha512-yb8DksgyUmpumFanxVcusbcKz1XKJTvzk30eHkC6sN71PDK8N4ZjiQR3mFIA1sNThT/QLw9AFCWAzIRtI9VaPA==","shasum":"baeac0270fa499a4cd42c934ef3daf39b4e75a4b","tarball":"https://registry.npmjs.org/@art-of-coding/bus/-/bus-0.0.1-alpha.2.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIFwOvfp/unxIvwHPergrKY6/xXBwbH4HfgINq/gBo5BZAiEAjX9s2sFPcGdKpFf4LwQX/QXV9rEiUazRXhUDRwUnWY0="}]},"maintainers":[{"name":"michielvdvelde","email":"michiel@michielvdvelde.nl"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/bus-0.0.1-alpha.2.tgz_1496693131548_0.703397429548204"}},"0.0.1-alpha.3":{"name":"@art-of-coding/bus","version":"0.0.1-alpha.3","description":"Fancy MQTT message bus manager ","author":{"name":"Michiel van der Velde","email":"michiel@michielvdvelde.nl"},"license":"MIT","main":"dist/index.js","typings":"dist/index.d.ts","engines":{"node":"=>7.10.0"},"repository":{"type":"git","url":"git+https://github.com/Art-of-Coding/bus.git"},"bugs":{"url":"https://github.com/Art-of-Coding/bus/issues"},"homepage":"https://github.com/Art-of-Coding/bus#readme","dependencies":{"@types/node":"^7.0.28","mqtt":"^2.8.2"},"devDependencies":{"typescript":"^2.3.4"},"gitHead":"f23a9aa67bc60f56b865b596b08d2a827d39e94d","_id":"@art-of-coding/bus@0.0.1-alpha.3","_npmVersion":"5.0.0","_nodeVersion":"8.0.0","_npmUser":{"name":"michielvdvelde","email":"michiel@michielvdvelde.nl"},"dist":{"integrity":"sha512-gYuUQ7i8O9Z+fjexXmFzig/BGi43R0KnTdh34I7cjqSnsST/zQoCzuRL1HY0x/wGxGH3RnnF4Js011I4S7dJRw==","shasum":"8ae5695347b3efc8f0ee311e7908e99fa11309a7","tarball":"https://registry.npmjs.org/@art-of-coding/bus/-/bus-0.0.1-alpha.3.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCkrmM58kLbhlCSmFQL/HPANYWiSCmvDk0ziT07Ik1/TAIhAK8Ov09yksIJo+j6dX4on1K5zCmls8gG4cxmnZsQyWij"}]},"maintainers":[{"name":"michielvdvelde","email":"michiel@michielvdvelde.nl"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/bus-0.0.1-alpha.3.tgz_1499435997568_0.3203495368361473"}}},"readme":"# bus &nbsp;&nbsp; [![TypeScript](https://badges.frapsoft.com/typescript/code/typescript.svg?v=101)](https://github.com/ellerbrock/typescript-badges/) [![npm](https://img.shields.io/npm/v/@art-of-coding/bus.svg)](https://www.npmjs.com/package/@art-of-coding/bus) [![npm](https://img.shields.io/npm/l/@art-of-coding/bus.svg)](https://www.npmjs.com/package/@art-of-coding/bus)\n\n\n**Note:** bus is **deprecated** and **no longer maintained!** Use [Eshu](https://github.com/Art-of-Coding/eshu) instead.\n\n------------------------------------------------\n\n**bus** provides a high-level way to work with MQTT. Under the hood it uses\nthe excellent [MQTT.js](https://github.com/mqttjs/MQTT.js) module.\n\nThe module is written in [TypeScript](https://www.typescriptlang.org/) and has the\nrequired declaration files included. It can also be used in vanilla JS.\n\n> bus is currently in active development and breaking changes may be introduced\n> without warning! This will remain true at least as long as the module is in\n> alpha (e.g. 0.0.1-alpha.1)\n\n* [Install](#install)\n* [Example](#example)\n* [API](#api)\n* [Inspired by](#inspiredby)\n* [License](#license)\n\n------------------------------------------------\n\n<a name=\"install\"></a>\n## Install\n\nbus can be installed using npm:\n\n```\nnpm install @art-of-coding/bus\n```\n\n------------------------------------------------\n\n<a name=\"example\"></a>\n## Example\n\nThis basic example shows how to use the basic functionality.\n\n```ts\nimport { Bus } from '@art-of-coding/bus'\n\n// Create a new Bus instance with the given client ID and broker URL\nconst bus = Bus.create('my-client-id', 'mqtt://localhost')\n\n// Set up a topic pattern identified by the label `configUpdated`\n// `+deviceId` and '#keys' are parameterized wildcards\nbus.setPattern('configUpdated', 'devices/+deviceId/config/#keys')\n\n// Add a listener for `configUpdated`\n// This function is called each time a publish packet is received on the\n// topic identified by the label\nbus.on('configUpdated', function (packet) {\n  // We can use packet.params to access the pattern parameters\n  console.log(`Device ${packet.params.deviceId} config updated:\n  ${packet.params.keys.join('/')} is now ${packet.payload})`)\n})\n\n// Now, connect to the broker\nbus.connect().then(() => {\n  console.log('connected to broker')\n\n  // Subscribe to the topic with label `configUpdated`\n  return bus.subscribe('configUpdated')\n}).then(() => {\n  console.log('subscribed to configUpdated')\n\n  // Publish a config update on the same pattern\n  // This will publish the word 'localhost' on topic devices/my-device/config/http/host\n  return bus.publish('configUpdated', { deviceId: 'my-device', keys: [ 'http', 'host' ] }, 'localhost')\n}).then(() => {\n  console.log('update published')\n\n  // We're done, so we'll close the connection\n  return bus.end()\n}).then(() => {\n  console.log('disconnected from broker')\n}).catch(err => {\n  // One of the steps above threw an error\n  console.error('Got an error:')\n  console.error(err)\n})\n```\n\n------------------------------------------------\n\n<a name=\"api\"></a>\n## API\n\n* [Bus.create()](#create)\n* [bus.getId()](#getid)\n* [bus.isAvailable()](#isavailable)\n* [bus.getStatus()](#getstatus)\n* [bus.getStatusError()](#getstatuserror)\n* [bus.onStatusChange()](#onstatuschange)\n* [bus.setPattern()](#setpattern)\n* [bus.removePattern()](#removepattern)\n* [bus.on()](#on)\n* [bus.once()](#once)\n* [bus.removeListener()](#removelistener)\n* [bus.removeAllListeners()](#removealllisteners)\n* [bus.connect()](#connect)\n* [bus.end()](#end)\n* [bus.subscribe()](#subscribe)\n* [bus.unsubscribe()](#unsubscribe)\n* [bus.publish()](#publish)\n\n------------------------------------------------\n\n<a name=\"create\"></a>\n### Bus.create (clientId: string, url: string, opts?: IBusOptions): Bus\n\nCreates a new Bus instance.\n\n* `clientId`: The instance's client ID\n* `url`: The broker URL (e.g. `mqtt://localhost:3306`)\n* `opts`: MQTT.js options (see [here](https://github.com/mqttjs/MQTT.js#client))\n  * `clientId` is **always** overwritten by the Id given as the first parameter\n\n```ts\nimport { Bus } from '@art-of-coding/bus'\n// or\nconst Bus = require('@art-of-coding/bus').Bus\r\n\n// create a Bus instance\nconst bus = Bus.create('my-client-id', 'mqtt://localhost')\n```\n\n------------------------------------------------\n\n<a name=\"getid\"></a>\n### bus.getId (): string\n\nGet the client ID for the instance.\n\n------------------------------------------------\n\n<a name=\"isavailable\"></a>\n### bus.isAvailable (): boolean\n\nCheck to see if the bus is available and can be used.\n\n------------------------------------------------\n\n<a name=\"getstatus\"></a>\n### bus.getStatus (): Status\n\nGet the instance status. The return value is an integer and corresponds to the\n`Status` enum:\n\n| Status       | Code |\n|--------------|------|\n| READY        | 0    |\n| CONNECTING   | 1    |\n| CONNECTED    | 2    |\n| RECONNECTING | 3    |\n| OFFLINE      | 4    |\n| CLOSED       | 5    |\n| ERROR        | 6    |\n\n------------------------------------------------\n\n<a name=\"getstatuserror\"></a>\n### bus.getStatusError (): Error|null\n\nGet the last error, or `null` if no error occured yet.\nIs usually only available when `bus.getStatus()` returns `ERROR`.\n\n------------------------------------------------\n\n<a name=\"onstatuschange\"></a>\n### bus.onStatusChange (fn: (status: Status, error: Error) => void): void\n\nSet a method which will be called each time the bus's status changes.\n\n* `fn`: The method to call\n\n```ts\nbus.onStatusChange(function (status: Status, error: Error) {\n  const withOrWithout = error ? 'with' : 'without'\n  console.log(`Status changed to ${status} (${withOrWithout} error)`)\n})\n```\n\n------------------------------------------------\n\n<a name=\"setpattern\"></a>\n### bus.setPattern (label: string, pattern: string): Bus\n\nSet a topic pattern with the given label. When set, a `pattern` can be referenced\nby using its `label`. For more on topic patterns, [see the mqtt-regex readme](https://github.com/RangerMauve/mqtt-regex/blob/master/README.md#api).\n\n* `label`: The pattern's label\n* `pattern`: The topic pattern\n\nNotes:\n* This method will throw if the label already exists or the pattern is invalid\n* This method returns the bus instance, so calls can be chained\n\n```ts\n// set a topic with a single named wildcard - `one`\nbus.setPattern('myLabel1', 'my/topic/+one')\n\n// set a topic with a multi named wildcard - `keys`\nbus.setPattern('myLabel2', 'my/topic/#keys')\n```\n\n------------------------------------------------\n\n<a name=\"removepattern\"></a>\n### bus.removePattern (label: string): Bus\n\nRemoves a topic pattern with the given label.\n\n* `label`: The label to remove\n\nNotes:\n* This method will throw if the label doesn't exist\n* This method returns the bus instance, so calls can be chained\n\n------------------------------------------------\n\n<a name=\"on\"></a>\n### bus.on (label: string, fn: (packet: IPacket) => void): Bus\n\nAdd a listener for the given `label`. This listener is called each time a message\nis received on a topic that matches the pattern accompanying the label.\n\n* `label`: The label\n* `fn`: The method to call\n\nNotes:\n* bus is **not an EventEmitter**! Therefore, not all methods for regular\nevent emitters will work!\n* This method will throw if the label doesn't exist\n* This method returns the bus instance, so calls can be chained\n\n------------------------------------------------\n\n<a name=\"once\"></a>\n### bus.once (label: string, fn: (packet: IPacket) => void): Bus\n\nAdds a once listener for the given `label`. The listener is called the next time\na message that matches the topic pattern is received, then it is removed.\n\n* `label`: The label\n* `fn`: The method to call\n\nNotes:\n* bus is **not an EventEmitter**! Therefore, not all methods for regular\nevent emitters will work!\n* This method will throw if the label doesn't exist\n* This method returns the bus instance, so calls can be chained\n\n------------------------------------------------\n\n<a name=\"removelistener\"></a>\n### bus.removeListener (label: string, fn: (packet: IPacket) => void): Bus\n\nRemoves the listener method for the given `label`. The method will no longer be\ncalled.\n\n* `label`: The label\n* `fn`: The method to call\n\nNotes:\n* bus is **not an EventEmitter**! Therefore, not all methods for regular\nevent emitters will work!\n* This method will throw if the label doesn't exist\n* This method returns the bus instance, so calls can be chained\n\n------------------------------------------------\n\n<a name=\"removealllisteners\"></a>\n### bus.removeAllListeners (label?: string): Bus\n\nRemoves all listeners. When `label` is set, only that label's listeners will be\nremoved. Otherwise, all listeners (for all labels) will be removed.\n\n* `label`: The label\n\nNotes:\n* bus is **not an EventEmitter**! Therefore, not all methods for regular\nevent emitters will work!\n* This method will throw if the label doesn't exist\n* This method returns the bus instance, so calls can be chained\n\n------------------------------------------------\n\n<a name=\"connect\"></a>\n### bus.connect(): Promise<IConnackPacket>\n\nConnect to the broker.\n\n```ts\nbus.connect().then(() => {\n  console.log('connected!')\n}).catch(err => {\n  console.error(err)\n})\n```\n\n------------------------------------------------\n\n<a name=\"end\"></a>\n### bus.end (force: boolean = false): Promise<null>\n\nCloses the connection to the broker. If `force` is `true`, the connection will be\nclosed without waiting for in-flight packages fo be acked.\n\n* `force`: Don't wait for in-flight messages to be acked (default `false`)\n\n------------------------------------------------\n\n<a name=\"subscribe\"></a>\n### bus.subscribe (label: string, opts?: IClientSubscribeOptions): Promise<ISubscriptionGrant>\n\nSubscribe to the topic pattern identified by `label`.\n\n* `label`: The label to subscribe to\n* `opts`:\n  * `qos`: qos subscription level, (default `0`)\n\n------------------------------------------------\n\n<a name=\"unsubscribe\"></a>\n### bus.unsubscribe (label: string, removeListeners: boolean = false): Promise<null>\n\nUnsubscribes from the topic pattern identified by `label`. If `removeListeners`\r\nis `true`, all added listeners for the label will be removed as well.\n\n* `label`: The label to unsubscribe from\n* `removeListeners`: Remove all listeners for the label (default `false`)\n\n------------------------------------------------\n\n<a name=\"publish\"></a>\n### bus.publish (label: string, params: any, payload: any, opts?: IClientPublishOptions): Promise<null>\n\nPublished the `payload` on the topic pattern identified by `label`, with the\nparameters `params`. If you have no parameters, use an empty object (`{}`) instead.\n\n* `label`: The label to publish\r\n* `params`: The topic parameter values\r\n* `payload`: The payload to publish\r\n* `opts`:\n  * `qos`: QoS level (default `0`)\n  * `retain`: retain flag (default `false`)\n  * `dup`: mark as duplicate (default `false`)\n\r\nNotes:\r\n * `params` will usually be an object with keys corresponding to wildcard names\r\n * `payload` automatically stringifies JSON and converts numbers to string\r\n\r\n```ts\n// Set a pattern `myLabel` with one parameter (`+name`)\nbus.setPattern('myLabel', 'topics/+name')\n\n// Make the parameters object\nconst params = { name: 'my-topic' }\n// Set the payload\nconst payload = 'Hello, topic!'\n\n// Publish the payload on the label with the given parameters\nbus.publish('myLabel', params, payload)\n```\n\n------------------------------------------------\n\n<a name=\"inspiredby\"></a>\n## Inspired by\n\n- [MQTT.js](https://github.com/mqttjs/MQTT.js), which this module uses under the hood\n- [mqtt-regex](https://github.com/RangerMauve/mqtt-regex), whose support for parameterized\ntopics is integrated\n\n------------------------------------------------\n\n<a name=\"license\"></a>\n## License\n\nCopyright 2017 [Michiel van der Velde](http://www.michielvdvelde.nl).\n\nThis software is licensed under the [MIT License](LICENSE).\n","maintainers":[{"name":"michielvdvelde","email":"michiel@michielvdvelde.nl"}],"time":{"modified":"2022-04-04T15:35:52.996Z","created":"2017-06-04T19:31:03.235Z","0.0.1-alpha.1":"2017-06-04T19:31:03.235Z","0.0.1-alpha.2":"2017-06-05T20:05:32.417Z","0.0.1-alpha.3":"2017-07-07T13:59:58.616Z"},"homepage":"https://github.com/Art-of-Coding/bus#readme","repository":{"type":"git","url":"git+https://github.com/Art-of-Coding/bus.git"},"author":{"name":"Michiel van der Velde","email":"michiel@michielvdvelde.nl"},"bugs":{"url":"https://github.com/Art-of-Coding/bus/issues"},"license":"MIT","readmeFilename":"README.md"}