mqtt-json-rpc 1.3.6 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -20,13 +20,13 @@ $ npm install mqtt mqtt-json-rpc
20
20
  About
21
21
  -----
22
22
 
23
- This is a small wrapper around the
23
+ This is an addon API for the
24
24
  [MQTT.js](https://www.npmjs.com/package/mqtt) API of
25
25
  [Node.js](https://nodejs.org/), for
26
26
  [Remote Procedure Call](https://en.wikipedia.org/wiki/Remote_procedure_call) (RPC)
27
27
  communication based on the [JSON-RPC](http://www.jsonrpc.org/)
28
28
  protocol. This allows a bi-directional request/response-style communication over
29
- the uni-directional message protocol [MQTT](http://mqtt.org).
29
+ the technically uni-directional message protocol [MQTT](http://mqtt.org).
30
30
 
31
31
  Usage
32
32
  -----
@@ -40,7 +40,7 @@ const RPC = require("mqtt-json-rpc")
40
40
  const mqtt = MQTT.connect("wss://127.0.0.1:8889", { ... })
41
41
  const rpc = new RPC(mqtt)
42
42
 
43
- rpc.on("connect", () => {
43
+ mqtt.on("connect", () => {
44
44
  rpc.register("example/hello", (a1, a2) => {
45
45
  console.log("example/hello: request: ", a1, a2)
46
46
  return `${a1}:${a2}`
@@ -57,10 +57,10 @@ const RPC = require("mqtt-json-rpc")
57
57
  const mqtt = MQTT.connect("wss://127.0.0.1:8889", { ... })
58
58
  const rpc = new RPC(mqtt)
59
59
 
60
- rpc.on("connect", () => {
60
+ mqtt.on("connect", () => {
61
61
  rpc.call("example/hello", "world", 42).then((response) => {
62
62
  console.log("example/hello response: ", response)
63
- rpc.end()
63
+ mqtt.end()
64
64
  })
65
65
  })
66
66
  ```
@@ -68,14 +68,13 @@ rpc.on("connect", () => {
68
68
  Application Programming Interface
69
69
  ---------------------------------
70
70
 
71
- The API of MQTT-JSON-RPC is a superset of the original
72
- [MQTT.js](https://www.npmjs.com/package/mqtt) API because it is just a
73
- wrapper around it with the following additional methods:
71
+ The MQTT-JSON-RPC API provides the following methods (check out the
72
+ corresponding [TypeScript definition](mqtt-json-rpc.d.ts)) file):
74
73
 
75
- - `constructor(mqtt: MQTT, encoding?: string]): MQTT-JSON-RPC`:<br/>
76
- Create the [MQTT.js](https://www.npmjs.com/package/mqtt) API wrapper.
74
+ - `constructor(mqtt: MQTT, options?: { encoding?: string, timeout?: number }): MQTT-JSON-RPC`:<br/>
77
75
  The `mqtt` is the [MQTT.js](https://www.npmjs.com/package/mqtt) instance.
78
- The optional `encoding` can be either `json` (default), `msgpack` or `cbor`.
76
+ The optional `encoding` option can be either `json` (default), `msgpack` or `cbor`.
77
+ The optional `timeout` option is the timeout in seconds.
79
78
 
80
79
  - `MQTT-JSON-RPC#registered(method: string): boolean`:<br/>
81
80
  Check for the previous registration of a method. The `method` has to
@@ -141,7 +140,7 @@ rpc.register("example/hello", (a1, a2) => {
141
140
  })
142
141
  ```
143
142
 
144
- ...and then its result, here `"world:42"`, is then
143
+ ...and then its result, in the above `rpc.call` example `"world:42"`, is then
145
144
  sent back as the following JSON-RPC 2.0 success response
146
145
  message to the temporary (client-specific) MQTT topic
147
146
  `example/hello/response/d1acc980-0e4e-11e8-98f0-ab5030b47df4`:
@@ -193,7 +192,7 @@ user example
193
192
  topic readwrite example/#
194
193
  ```
195
194
 
196
- ...and an `example` user in `mosquitto-pwd.txt` like:
195
+ ...and an `example` user (with password `example`) in `mosquitto-pwd.txt` like:
197
196
 
198
197
  ```
199
198
  example:$6$awYNe6oCAi+xlvo5$mWIUqyy4I0O3nJ99lP1mkRVqsDGymF8en5NChQQxf7KrVJLUp1SzrrVDe94wWWJa3JGIbOXD9wfFGZdi948e6A==
@@ -214,13 +213,13 @@ const mqtt = MQTT.connect("wss://127.0.0.1:8889", {
214
213
 
215
214
  const rpc = new RPC(mqtt)
216
215
 
217
- rpc.on("error", (err) => { console.log("ERROR", err) })
218
- rpc.on("offline", () => { console.log("OFFLINE") })
219
- rpc.on("close", () => { console.log("CLOSE") })
220
- rpc.on("reconnect", () => { console.log("RECONNECT") })
221
- rpc.on("message", (topic, message) => { console.log("RECEIVED", topic, message.toString()) })
216
+ mqtt.on("error", (err) => { console.log("ERROR", err) })
217
+ mqtt.on("offline", () => { console.log("OFFLINE") })
218
+ mqtt.on("close", () => { console.log("CLOSE") })
219
+ mqtt.on("reconnect", () => { console.log("RECONNECT") })
220
+ mqtt.on("message", (topic, message) => { console.log("RECEIVED", topic, message.toString()) })
222
221
 
223
- rpc.on("connect", () => {
222
+ mqtt.on("connect", () => {
224
223
  console.log("CONNECT")
225
224
  rpc.register("example/hello", (a1, a2) => {
226
225
  console.log("example/hello: request: ", a1, a2)
@@ -228,7 +227,7 @@ rpc.on("connect", () => {
228
227
  })
229
228
  rpc.call("example/hello", "world", 42).then((result) => {
230
229
  console.log("example/hello sucess: ", result)
231
- rpc.end()
230
+ mqtt.end()
232
231
  }).catch((err) => {
233
232
  console.log("example/hello error: ", err)
234
233
  })
@@ -250,7 +249,7 @@ CLOSE
250
249
  License
251
250
  -------
252
251
 
253
- Copyright (c) 2018-2022 Dr. Ralf S. Engelschall (http://engelschall.com/)
252
+ Copyright (c) 2018-2023 Dr. Ralf S. Engelschall (http://engelschall.com/)
254
253
 
255
254
  Permission is hereby granted, free of charge, to any person obtaining
256
255
  a copy of this software and associated documentation files (the
package/eslint.yaml CHANGED
@@ -1,6 +1,6 @@
1
1
  ##
2
2
  ## MQTT-JSON-RPC -- JSON-RPC protocol over MQTT communication
3
- ## Copyright (c) 2018-2022 Dr. Ralf S. Engelschall <rse@engelschall.com>
3
+ ## Copyright (c) 2018-2023 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
4
  ##
5
5
  ## Permission is hereby granted, free of charge, to any person obtaining
6
6
  ## a copy of this software and associated documentation files (the
@@ -0,0 +1,54 @@
1
+ /*
2
+ ** MQTT-JSON-RPC -- JSON-RPC protocol over MQTT communication
3
+ ** Copyright (c) 2018-2023 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
+ **
5
+ ** Permission is hereby granted, free of charge, to any person obtaining
6
+ ** a copy of this software and associated documentation files (the
7
+ ** "Software"), to deal in the Software without restriction, including
8
+ ** without limitation the rights to use, copy, modify, merge, publish,
9
+ ** distribute, sublicense, and/or sell copies of the Software, and to
10
+ ** permit persons to whom the Software is furnished to do so, subject to
11
+ ** the following conditions:
12
+ **
13
+ ** The above copyright notice and this permission notice shall be included
14
+ ** in all copies or substantial portions of the Software.
15
+ **
16
+ ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+ declare module "mqtt-json-rpc" {
26
+ export class API {
27
+ constructor(
28
+ mqtt: any,
29
+ options: {
30
+ encoding?: string,
31
+ timeout?: number
32
+ }
33
+ )
34
+ registered(
35
+ method: string
36
+ ): boolean
37
+ register(
38
+ method: string,
39
+ callback: (...params: any[]) => any
40
+ ): Promise<boolean>
41
+ unregister(
42
+ method: string
43
+ ): Promise<any>
44
+ notify (
45
+ method: string,
46
+ ...params: any[]
47
+ ): void
48
+ call (
49
+ method: string,
50
+ ...params: any[]
51
+ ): Promise<any>
52
+ }
53
+ }
54
+
package/mqtt-json-rpc.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*
2
2
  ** MQTT-JSON-RPC -- JSON-RPC protocol over MQTT communication
3
- ** Copyright (c) 2018-2022 Dr. Ralf S. Engelschall <rse@engelschall.com>
3
+ ** Copyright (c) 2018-2023 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
4
  **
5
5
  ** Permission is hereby granted, free of charge, to any person obtaining
6
6
  ** a copy of this software and associated documentation files (the
@@ -57,23 +57,6 @@ class API {
57
57
  })
58
58
  }
59
59
 
60
- /* just pass-through the entire MQTT Client API */
61
- on (...args) { return this.mqtt.on(...args) }
62
- addListener (...args) { return this.mqtt.addListener(...args) }
63
- removeListener (...args) { return this.mqtt.removeListener(...args) }
64
- publish (...args) { return this.mqtt.publish(...args) }
65
- subscribe (...args) { return this.mqtt.subscribe(...args) }
66
- unsubscribe (...args) { return this.mqtt.unsubscribe(...args) }
67
- end (...args) { return this.mqtt.end(...args) }
68
- removeOutgoingMessage (...args) { return this.mqtt.removeOutgoingMessage(...args) }
69
- reconnect (...args) { return this.mqtt.reconnect(...args) }
70
- handleMessage (...args) { return this.mqtt.handleMessage(...args) }
71
- get connected () { return this.mqtt.connected }
72
- set connected (value) { this.mqtt.connected = value }
73
- getLastMessageId (...args) { return this.mqtt.getLastMessageId(...args) }
74
- get reconnecting () { return this.mqtt.reconnecting }
75
- set reconnecting (value) { this.mqtt.reconnecting = value }
76
-
77
60
  /*
78
61
  * RPC server/response side
79
62
  */
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "mqtt-json-rpc",
3
- "version": "1.3.6",
3
+ "version": "2.0.0",
4
4
  "description": "JSON-RPC protocol over MQTT communication",
5
5
  "keywords": [ "json-rpc", "json", "rpc", "mqtt" ],
6
6
  "main": "./mqtt-json-rpc.js",
7
+ "types": "./mqtt-json-rpc.d.ts",
7
8
  "license": "MIT",
8
9
  "repository": {
9
10
  "type": "git",
@@ -17,9 +18,9 @@
17
18
  "homepage": "https://github.com/rse/mqtt-json-rpc",
18
19
  "bugs": "https://github.com/rse/mqtt-json-rpc/issues",
19
20
  "devDependencies": {
20
- "eslint": "8.13.0",
21
- "eslint-config-standard": "17.0.0",
22
- "eslint-plugin-import": "2.26.0",
21
+ "eslint": "8.44.0",
22
+ "eslint-config-standard": "17.1.0",
23
+ "eslint-plugin-import": "2.27.5",
23
24
  "eslint-plugin-node": "11.1.0",
24
25
  "mqtt": "4.3.7"
25
26
  },
@@ -27,8 +28,8 @@
27
28
  "mqtt": ">=4.0.0"
28
29
  },
29
30
  "dependencies": {
30
- "pure-uuid": "1.6.2",
31
- "encodr": "1.3.2",
31
+ "pure-uuid": "1.6.4",
32
+ "encodr": "1.3.5",
32
33
  "jsonrpc-lite": "2.2.0"
33
34
  },
34
35
  "engines": {
@@ -3,17 +3,17 @@
3
3
  "version": "0.0.0",
4
4
  "description": "",
5
5
  "dependencies": {
6
- "@babel/core": "7.17.9",
6
+ "@babel/core": "7.22.8",
7
7
  "mqtt": "4.3.7",
8
8
  "mqtt-json-rpc": ".."
9
9
  },
10
10
  "devDependencies": {
11
- "grunt": "1.5.2",
11
+ "grunt": "1.6.1",
12
12
  "grunt-cli": "1.4.3",
13
13
  "grunt-browserify": "6.0.0",
14
14
  "browserify": "17.0.0",
15
15
  "babelify": "10.0.0",
16
- "@babel/preset-env": "7.16.11"
16
+ "@babel/preset-env": "7.22.7"
17
17
  },
18
18
  "scripts": {
19
19
  "build": "grunt default",
package/sample/sample.js CHANGED
@@ -10,13 +10,13 @@ const mqtt = MQTT.connect("wss://127.0.0.1:8889", {
10
10
 
11
11
  const rpc = new RPC(mqtt)
12
12
 
13
- rpc.on("error", (err) => { console.log("ERROR", err) })
14
- rpc.on("offline", () => { console.log("OFFLINE") })
15
- rpc.on("close", () => { console.log("CLOSE") })
16
- rpc.on("reconnect", () => { console.log("RECONNECT") })
17
- rpc.on("message", (topic, message) => { console.log("RECEIVED", topic, message.toString()) })
13
+ mqtt.on("error", (err) => { console.log("ERROR", err) })
14
+ mqtt.on("offline", () => { console.log("OFFLINE") })
15
+ mqtt.on("close", () => { console.log("CLOSE") })
16
+ mqtt.on("reconnect", () => { console.log("RECONNECT") })
17
+ mqtt.on("message", (topic, message) => { console.log("RECEIVED", topic, message.toString()) })
18
18
 
19
- rpc.on("connect", () => {
19
+ mqtt.on("connect", () => {
20
20
  console.log("CONNECT")
21
21
  rpc.register("example/hello", (a1, a2) => {
22
22
  console.log("example/hello: request: ", a1, a2)
@@ -24,7 +24,7 @@ rpc.on("connect", () => {
24
24
  })
25
25
  rpc.call("example/hello", "world", 42).then((result) => {
26
26
  console.log("example/hello sucess: ", result)
27
- rpc.end()
27
+ mqtt.end()
28
28
  }).catch((err) => {
29
29
  console.log("example/hello error: ", err)
30
30
  })