{"_id":"@akiroz/mqtt-json-rpc","_rev":"1-51a4395bbec8bca0204045a038ffa3d3","name":"@akiroz/mqtt-json-rpc","dist-tags":{"latest":"1.1.1"},"versions":{"1.1.1":{"name":"@akiroz/mqtt-json-rpc","version":"1.1.1","description":"JSON-RPC protocol over MQTT communication","keywords":["json-rpc","json","rpc","mqtt"],"main":"./mqtt-json-rpc.js","license":"MIT","repository":{"type":"git","url":"https://github.com/rse/mqtt-json-rpc.git"},"author":{"name":"Dr. Ralf S. Engelschall","email":"rse@engelschall.com","url":"http://engelschall.com"},"homepage":"https://github.com/rse/mqtt-json-rpc","bugs":{"url":"https://github.com/rse/mqtt-json-rpc/issues"},"devDependencies":{"babel-eslint":"10.0.2","eslint":"6.0.1","eslint-config-standard":"13.0.1","eslint-plugin-import":"2.18.0","eslint-plugin-node":"9.1.0","eslint-plugin-promise":"4.2.1","eslint-plugin-standard":"4.0.0","mqtt":"3.0.0"},"peerDependencies":{"mqtt":"^3.0.0"},"dependencies":{"encodr":"1.2.2","jsonrpc-lite":"2.1.0","pure-uuid":"1.5.7"},"engines":{"node":">=8.0.0"},"scripts":{"prepublishOnly":"eslint --config eslint.yaml mqtt-json-rpc.js sample.js"},"_id":"@akiroz/mqtt-json-rpc@1.1.1","dist":{"shasum":"715c29b638dda9cf9f48c91bfa442466d00cdb89","integrity":"sha512-Fmr1sjJbh/hPmwKH2B2S4wDSexppeOSNSBiQYSdbR02dL1Tx7b5OnVJQEiIaNKH1agvHuYNKv5ShprXe4CTgqQ==","tarball":"https://registry.npmjs.org/@akiroz/mqtt-json-rpc/-/mqtt-json-rpc-1.1.1.tgz","fileCount":6,"unpackedSize":21952,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJeViayCRA9TVsSAnZWagAA468P/R/SA101mHs2V9WY8ll5\nKo/VzLchucwibeTBg0jRavbcfBHxyqgQXUDFBaTxe18BsXtSsMQXEHHO3w8J\n3genSt+h6b6hu+4y6/iQs5gnGcJsgZoHWbH7XJMSz0AOkykM9d/SKqp0L1YS\nIIO4Nf5ZGsK4GalJLYlKfkimkUGFJW4iQ5koZbqixVI+6+DoU59OL4fNjn4a\nQlv5UaKqGcxaR/FGjEiXFnWElnSb0CYBSSwPROTtTmVA4RQjmNJ0ZfNCovic\n48dRAjRqy8m9KW5G0RkbW24S+QDgMq3OZw38Rx++Y056Kn/OW8Ia0zYvd1IZ\n5TO/BnU5XsXwyIF8yLwPa5JAzJA48YBf8XXE6V8IPiPx7Vk4UpIWfnDjsMiy\n8OvalbNn8RpHo1nqnDzGSAG+OX28J02+SDaP4jYKpLe+TgR8IOzEmmmK12kH\nLsX4ZGz0dCczys5HF9CPmlU6goiHBEnlr5OVjYXPx9IMHdtcL2FTWE9Sr+0h\nrrg8DqJehN41GfpT0/mesawFiQs1faDzXcpcZn48OImrTdjp5yt0Y+HzgrdN\n8EEyC0JJUkwPzR8bTg/v3TiKLT73xV2Sk9aGldngyHXH1x1I/p8evBTmsmPN\n5/Ycs0s2BW1U6tvgQQyPFWwkfhaEOy99Su3wRT5yG9Bcpv0dIdcJijw2km/I\nfyLP\r\n=a1cR\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDITaYaZDlmSn89wl25BFD9fX6S0KQeTXDj5VoPOA7xdAIgGMUZlXtsENXeIIeWbMWuE8dw8pbelVG4om8fHOiJIiQ="}]},"maintainers":[{"name":"akiroz","email":"akiroz.vectis@gmail.com"}],"_npmUser":{"name":"akiroz","email":"akiroz.vectis@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/mqtt-json-rpc_1.1.1_1582704306412_0.8653779224376981"},"_hasShrinkwrap":false}},"time":{"created":"2020-02-26T08:05:06.387Z","1.1.1":"2020-02-26T08:05:06.610Z","modified":"2022-04-04T12:29:45.286Z"},"maintainers":[{"name":"akiroz","email":"akiroz.vectis@gmail.com"}],"description":"JSON-RPC protocol over MQTT communication","homepage":"https://github.com/rse/mqtt-json-rpc","keywords":["json-rpc","json","rpc","mqtt"],"repository":{"type":"git","url":"https://github.com/rse/mqtt-json-rpc.git"},"author":{"name":"Dr. Ralf S. Engelschall","email":"rse@engelschall.com","url":"http://engelschall.com"},"bugs":{"url":"https://github.com/rse/mqtt-json-rpc/issues"},"license":"MIT","readme":"\nMQTT-JSON-RPC\n=============\n\n[JSON-RPC](http://www.jsonrpc.org/) protocol over [MQTT](http://mqtt.org/) communication.\n\n<p/>\n<img src=\"https://nodei.co/npm/mqtt-json-rpc.png?downloads=true&stars=true\" alt=\"\"/>\n\n<p/>\n<img src=\"https://david-dm.org/rse/mqtt-json-rpc.png\" alt=\"\"/>\n\nInstallation\n------------\n\n```shell\n$ npm install mqtt mqtt-json-rpc\n```\n\nAbout\n-----\n\nThis is a small wrapper around the\n[MQTT.js](https://www.npmjs.com/package/mqtt) API of\n[Node.js](https://nodejs.org/), for\n[Remote Procedure Call](https://en.wikipedia.org/wiki/Remote_procedure_call) (RPC)\ncommunication based on the [JSON-RPC](http://www.jsonrpc.org/)\nprotocol. This allows a bi-directional request/response-style communication over\nthe uni-directional message protocol [MQTT](http://mqtt.org).\n\nUsage\n-----\n\n#### Server:\n\n```js\nconst MQTT = require(\"mqtt\")\nconst RPC  = require(\"mqtt-json-rpc\")\n\nconst mqtt = MQTT.connect(\"wss://127.0.0.1:8889\", { ... })\nconst rpc  = new RPC(mqtt)\n\nrpc.on(\"connect\", () => {\n    rpc.register(\"example/hello\", (a1, a2) => {\n        console.log(\"example/hello: request: \", a1, a2)\n        return `${a1}:${a2}`\n    })\n})\n```\n\n#### Client:\n\n```js\nconst MQTT = require(\"mqtt\")\nconst RPC  = require(\"mqtt-json-rpc\")\n\nconst mqtt = MQTT.connect(\"wss://127.0.0.1:8889\", { ... })\nconst rpc  = new RPC(mqtt)\n\nrpc.on(\"connect\", () => {\n    rpc.call(\"example/hello\", \"world\", 42).then((response) => {\n        console.log(\"example/hello response: \", response)\n        rpc.end()\n    })\n})\n```\n\nApplication Programming Interface\n---------------------------------\n\nThe API of MQTT-JSON-RPC is a superset of the original\n[MQTT.js](https://www.npmjs.com/package/mqtt) API because it is just a\nwrapper around it with the following additional methods:\n\n- `constructor(mqtt: MQTT, encoding?: string]): MQTT-JSON-RPC`:<br/>\n  Create the [MQTT.js](https://www.npmjs.com/package/mqtt) API wrapper.\n  The `mqtt` is the [MQTT.js](https://www.npmjs.com/package/mqtt) instance.\n  The optional `encoding` can be either `json` (default), `msgpack` or `cbor`.\n\n- `MQTT-JSON-RPC#register(method: string, callback: (...args: any[]) => any): Promise`:<br/>\n  Register a method. The `method` has to be a valid MQTT topic\n  name. The `callback` is called with the `params` passed to\n  the remote `MQTT-JSON-RPC#notify()` or `MQTT-JSON-RPC#call()`. For\n  a remote `MQTT-JSON-RPC#notify()`, the return value of `callback` will be\n  ignored. For a remote `MQTT-JSON-RPC#call()`, the return value of `callback`\n  will resolve the promise returned by the remote `MQTT-JSON-RPC#call()`.\n  Internally, on the MQTT broker the topic `${method}/request` is\n  subscribed.\n\n- `MQTT-JSON-RPC#unregister(method: string): Promise`:<br/>\n  Unregister a previously registered method.\n  Internally, on the MQTT broker the topic `${method}/request` is unsubscribed.\n\n- `MQTT-JSON-RPC#notify(method: string, ...params: any[]): void`:<br/>\n  Notify a method. The remote `MQTT-JSON-RPC#register()` `callback` is called\n  with `params` and its return value silently ignored.\n\n- `MQTT-JSON-RPC#call(method: string, ...params: any[]): Promise`:<br/>\n  Call a method. The remote `MQTT-JSON-RPC#register()` `callback` is\n  called with `params` and its return value resolves the returned\n  `Promise`. If the remote `callback` throws an exception, this rejects\n  the returned `Promise`. Internally, on the MQTT broker the topic\n  `${method}/response/<cid>` is temporarily subscribed for receiving the\n  response.\n\nInternals\n---------\n\nInternally, remote methods are assigned to MQTT topics. When calling a\nremote method named `example/hello` with parameters \"world\" and 42 via...\n\n```js\nrpc.call(\"example/hello\", \"world\", 42).then((result) => {\n    ...\n})\n```\n\n..the following JSON-RPC 2.0 request message is sent to the permanent MQTT\ntopic `example/hello/request`:\n\n```json\n{\n    \"jsonrpc\": \"2.0\",\n    \"id\":      \"d1acc980-0e4e-11e8-98f0-ab5030b47df4:d1db7aa0-0e4e-11e8-b1d9-5f0ab230c0d9\",\n    \"method\":  \"example/hello\",\n    \"params\":  [ \"world\", 42 ]\n}\n```\n\nBeforehand, this `example/hello` method should have been registered with...\n\n```js\nrpc.register(\"example/hello\", (a1, a2) => {\n    return `${a1}:${a2}`\n})\n```\n\n...and then its result, here `\"world:42\"`, is then\nsent back as the following JSON-RPC 2.0 success response\nmessage to the temporary (client-specific) MQTT topic\n`example/hello/response/d1acc980-0e4e-11e8-98f0-ab5030b47df4`:\n\n```json\n{\n    \"jsonrpc\": \"2.0\",\n    \"id\":      \"d1acc980-0e4e-11e8-98f0-ab5030b47df4:d1db7aa0-0e4e-11e8-b1d9-5f0ab230c0d9\",\n    \"result\":  \"world:42\"\n}\n```\n\nThe JSON-RPC 2.0 `id` field always consists of `<cid>:<rid>`, where\n`<cid>` is the UUID v1 of the MQTT-JSON-RPC instance and `<rid>` is\nthe UUID v1 of the particular method request. The `<cid>` is used for\nsending back the JSON-RPC 2.0 response message to the requestor only.\n\nLicense\n-------\n\nCopyright (c) 2018-2019 Dr. Ralf S. Engelschall (http://engelschall.com/)\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n\"Software\"), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be included\nin all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n","readmeFilename":"README.md"}