{"id":13696718,"url":"https://github.com/rse/mqtt-json-rpc","last_synced_at":"2025-05-01T09:55:45.799Z","repository":{"id":51059014,"uuid":"121006538","full_name":"rse/mqtt-json-rpc","owner":"rse","description":"JSON-RPC protocol over MQTT communication","archived":false,"fork":false,"pushed_at":"2024-08-12T20:48:14.000Z","size":102,"stargazers_count":37,"open_issues_count":0,"forks_count":8,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-01T09:55:38.646Z","etag":null,"topics":["json","json-rpc","mqtt","rpc"],"latest_commit_sha":null,"homepage":"https://npmjs.com/mqtt-json-rpc","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rse.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-02-10T10:37:34.000Z","updated_at":"2024-08-12T20:48:17.000Z","dependencies_parsed_at":"2024-04-08T02:54:32.510Z","dependency_job_id":"3ea368a1-7197-42bb-9d7c-cd3b66ee4ee3","html_url":"https://github.com/rse/mqtt-json-rpc","commit_stats":{"total_commits":111,"total_committers":2,"mean_commits":55.5,"dds":"0.018018018018018056","last_synced_commit":"4b03cda4b21ccf4adf6cea684ad203088f4b33be"},"previous_names":[],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fmqtt-json-rpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fmqtt-json-rpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fmqtt-json-rpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rse%2Fmqtt-json-rpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rse","download_url":"https://codeload.github.com/rse/mqtt-json-rpc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251856993,"owners_count":21655119,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","json-rpc","mqtt","rpc"],"created_at":"2024-08-02T18:00:45.535Z","updated_at":"2025-05-01T09:55:45.775Z","avatar_url":"https://github.com/rse.png","language":"JavaScript","funding_links":[],"categories":["Libraries"],"sub_categories":[],"readme":"\nMQTT-JSON-RPC\n=============\n\n[JSON-RPC](http://www.jsonrpc.org/) protocol over [MQTT](http://mqtt.org/) communication.\n\n\u003cp/\u003e\n\u003cimg src=\"https://nodei.co/npm/mqtt-json-rpc.png?downloads=true\u0026stars=true\" alt=\"\"/\u003e\n\n\u003cp/\u003e\n\u003cimg src=\"https://david-dm.org/rse/mqtt-json-rpc.png\" alt=\"\"/\u003e\n\nInstallation\n------------\n\n```shell\n$ npm install mqtt mqtt-json-rpc\n```\n\nAbout\n-----\n\nThis is an addon API for 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 technically 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\nmqtt.on(\"connect\", () =\u003e {\n    rpc.register(\"example/hello\", (a1, a2) =\u003e {\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\nmqtt.on(\"connect\", () =\u003e {\n    rpc.call(\"example/hello\", \"world\", 42).then((response) =\u003e {\n        console.log(\"example/hello response: \", response)\n        mqtt.end()\n    })\n})\n```\n\nApplication Programming Interface\n---------------------------------\n\nThe MQTT-JSON-RPC API provides the following methods (check out the\ncorresponding [TypeScript definition](mqtt-json-rpc.d.ts)) file):\n\n- `constructor(mqtt: MQTT, options?: { encoding?: string, timeout?: number }): MQTT-JSON-RPC`:\u003cbr/\u003e\n  The `mqtt` is the [MQTT.js](https://www.npmjs.com/package/mqtt) instance.\n  The optional `encoding` option can be either `json` (default), `msgpack` or `cbor`.\n  The optional `timeout` option is the timeout in seconds.\n\n- `MQTT-JSON-RPC#registered(method: string): boolean`:\u003cbr/\u003e\n  Check for the previous registration of a method. The `method` has to\n  be a valid MQTT topic name. The method returns `true` if `method` is\n  already registered, else it returns `false`.\n\n- `MQTT-JSON-RPC#register(method: string, callback: (...args: any[]) =\u003e any): Promise`:\u003cbr/\u003e\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`:\u003cbr/\u003e\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`:\u003cbr/\u003e\n  Notify a method. The remote `MQTT-JSON-RPC#register()` `callback` is called\n  with `params` and its return value is silently ignored.\n\n- `MQTT-JSON-RPC#call(method: string, ...params: any[]): Promise`:\u003cbr/\u003e\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/\u003ccid\u003e` is temporarily subscribed for receiving the\n  response (`\u003ccid\u003e` is a UUID v1 to uniquely identify the MQTT-JSON-RPC\n  caller instance).\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) =\u003e {\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) =\u003e {\n    return `${a1}:${a2}`\n})\n```\n\n...and then its result, in the above `rpc.call` example `\"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 `\u003ccid\u003e:\u003crid\u003e`, where\n`\u003ccid\u003e` is the UUID v1 of the MQTT-JSON-RPC instance and `\u003crid\u003e` is\nthe UUID v1 of the particular method request. The `\u003ccid\u003e` is used for\nsending back the JSON-RPC 2.0 response message to the requestor only.\n\nExample\n-------\n\nFor a real test-drive of MQTT-JSON-RPC, install the\n[Mosquitto](https://mosquitto.org/) MQTT broker with at least a \"MQTT\nover Secure-WebSockets\" lister in the `mosquitto.conf` file like...\n\n```\n[...]\n\npassword_file        mosquitto-pwd.txt\nacl_file             mosquitto-acl.txt\n\n[...]\n\n#   additional listener (wss: MQTT over WebSockets+SSL/TLS)\nlistener             8889 127.0.0.1\nmax_connections      -1\nprotocol             websockets\ncafile               mosquitto-ca.crt.pem\ncertfile             mosquitto-sv.crt.pem\nkeyfile              mosquitto-sv.key.pem\nrequire_certificate  false\n\n[...]\n```\n\n...and an access control list in `mosquitto-acl.txt` like...\n\n```\nuser    example\ntopic   readwrite example/#\n```\n\n...and an `example` user (with password `example`) in `mosquitto-pwd.txt` like:\n\n```\nexample:$6$awYNe6oCAi+xlvo5$mWIUqyy4I0O3nJ99lP1mkRVqsDGymF8en5NChQQxf7KrVJLUp1SzrrVDe94wWWJa3JGIbOXD9wfFGZdi948e6A==\n```\n\nThen test-drive MQTT-JSON-RPC with a complete [sample](sample/sample.js) to see\nMQTT-JSON-RPC in action and tracing its communication:\n\n```js\nconst MQTT = require(\"mqtt\")\nconst RPC  = require(\"mqtt-json-rpc\")\n\nconst mqtt = MQTT.connect(\"wss://127.0.0.1:8889\", {\n    rejectUnauthorized: false,\n    username: \"example\",\n    password: \"example\"\n})\n\nconst rpc = new RPC(mqtt)\n\nmqtt.on(\"error\",     (err)            =\u003e { console.log(\"ERROR\", err) })\nmqtt.on(\"offline\",   ()               =\u003e { console.log(\"OFFLINE\") })\nmqtt.on(\"close\",     ()               =\u003e { console.log(\"CLOSE\") })\nmqtt.on(\"reconnect\", ()               =\u003e { console.log(\"RECONNECT\") })\nmqtt.on(\"message\",   (topic, message) =\u003e { console.log(\"RECEIVED\", topic, message.toString()) })\n\nmqtt.on(\"connect\", () =\u003e {\n    console.log(\"CONNECT\")\n    rpc.register(\"example/hello\", (a1, a2) =\u003e {\n        console.log(\"example/hello: request: \", a1, a2)\n        return `${a1}:${a2}`\n    })\n    rpc.call(\"example/hello\", \"world\", 42).then((result) =\u003e {\n        console.log(\"example/hello sucess: \", result)\n        mqtt.end()\n    }).catch((err) =\u003e {\n        console.log(\"example/hello error: \", err)\n    })\n})\n```\n\nThe output will be:\n\n```\n$ node sample.js\nCONNECT\nRECEIVED example/hello/request {\"jsonrpc\":\"2.0\",\"id\":\"1099cb50-bd2b-11eb-8198-43568ad728c4:10bf7bc0-bd2b-11eb-bac6-439c565b651a\",\"method\":\"example/hello\",\"params\":[\"world\",42]}\nexample/hello: request:  world 42\nRECEIVED example/hello/response/1099cb50-bd2b-11eb-8198-43568ad728c4 {\"jsonrpc\":\"2.0\",\"id\":\"1099cb50-bd2b-11eb-8198-43568ad728c4:10bf7bc0-bd2b-11eb-bac6-439c565b651a\",\"result\":\"world:42\"}\nexample/hello sucess:  world:42\nCLOSE\n```\n\nLicense\n-------\n\nCopyright (c) 2018-2023 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","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frse%2Fmqtt-json-rpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frse%2Fmqtt-json-rpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frse%2Fmqtt-json-rpc/lists"}