mqtt-json-rpc 1.3.7 → 2.1.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 +25 -25
- package/dst/mqtt-json-rpc.cjs.js +5930 -0
- package/dst/mqtt-json-rpc.d.ts +33 -0
- package/dst/mqtt-json-rpc.esm.js +5931 -0
- package/dst/mqtt-json-rpc.js +287 -0
- package/dst/mqtt-json-rpc.umd.js +10 -0
- package/etc/eslint.mts +129 -0
- package/etc/stx.conf +29 -0
- package/etc/tsc.json +27 -0
- package/etc/tsc.tsbuildinfo +1 -0
- package/etc/vite.mts +45 -0
- package/package.json +45 -26
- package/sample/package.json +9 -10
- package/sample/sample.js +20 -26
- package/sample/sample.ts +34 -0
- package/sample/tsc.json +26 -0
- package/sample/tsc.tsbuildinfo +1 -0
- package/sample/vite.mjs +36 -0
- package/src/mqtt-json-rpc.ts +337 -0
- package/eslint.yaml +0 -66
- package/mqtt-json-rpc.js +0 -299
- package/sample/Gruntfile.js +0 -33
package/README.md
CHANGED
|
@@ -20,13 +20,13 @@ $ npm install mqtt mqtt-json-rpc
|
|
|
20
20
|
About
|
|
21
21
|
-----
|
|
22
22
|
|
|
23
|
-
This is
|
|
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
|
-
|
|
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
|
-
|
|
60
|
+
mqtt.on("connect", () => {
|
|
61
61
|
rpc.call("example/hello", "world", 42).then((response) => {
|
|
62
62
|
console.log("example/hello response: ", response)
|
|
63
|
-
|
|
63
|
+
mqtt.end()
|
|
64
64
|
})
|
|
65
65
|
})
|
|
66
66
|
```
|
|
@@ -68,21 +68,20 @@ rpc.on("connect", () => {
|
|
|
68
68
|
Application Programming Interface
|
|
69
69
|
---------------------------------
|
|
70
70
|
|
|
71
|
-
The
|
|
72
|
-
[
|
|
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
|
|
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
|
|
82
81
|
be a valid MQTT topic name. The method returns `true` if `method` is
|
|
83
82
|
already registered, else it returns `false`.
|
|
84
83
|
|
|
85
|
-
- `MQTT-JSON-RPC#register(method: string, callback: (...args: any[]) => any): Promise
|
|
84
|
+
- `MQTT-JSON-RPC#register(method: string, callback: (...args: any[]) => any): Promise<boolean>`:<br/>
|
|
86
85
|
Register a method. The `method` has to be a valid MQTT topic
|
|
87
86
|
name. The `callback` is called with the `params` passed to
|
|
88
87
|
the remote `MQTT-JSON-RPC#notify()` or `MQTT-JSON-RPC#call()`. For
|
|
@@ -92,7 +91,7 @@ wrapper around it with the following additional methods:
|
|
|
92
91
|
Internally, on the MQTT broker the topic `${method}/request` is
|
|
93
92
|
subscribed.
|
|
94
93
|
|
|
95
|
-
- `MQTT-JSON-RPC#unregister(method: string): Promise
|
|
94
|
+
- `MQTT-JSON-RPC#unregister(method: string): Promise<void>`:<br/>
|
|
96
95
|
Unregister a previously registered method.
|
|
97
96
|
Internally, on the MQTT broker the topic `${method}/request` is unsubscribed.
|
|
98
97
|
|
|
@@ -100,7 +99,7 @@ wrapper around it with the following additional methods:
|
|
|
100
99
|
Notify a method. The remote `MQTT-JSON-RPC#register()` `callback` is called
|
|
101
100
|
with `params` and its return value is silently ignored.
|
|
102
101
|
|
|
103
|
-
- `MQTT-JSON-RPC#call(method: string, ...params: any[]): Promise
|
|
102
|
+
- `MQTT-JSON-RPC#call(method: string, ...params: any[]): Promise<any>`:<br/>
|
|
104
103
|
Call a method. The remote `MQTT-JSON-RPC#register()` `callback` is
|
|
105
104
|
called with `params` and its return value resolves the returned
|
|
106
105
|
`Promise`. If the remote `callback` throws an exception, this rejects
|
|
@@ -141,7 +140,7 @@ rpc.register("example/hello", (a1, a2) => {
|
|
|
141
140
|
})
|
|
142
141
|
```
|
|
143
142
|
|
|
144
|
-
...and then its result,
|
|
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`:
|
|
@@ -155,9 +154,10 @@ message to the temporary (client-specific) MQTT topic
|
|
|
155
154
|
```
|
|
156
155
|
|
|
157
156
|
The JSON-RPC 2.0 `id` field always consists of `<cid>:<rid>`, where
|
|
158
|
-
`<cid>` is the UUID v1 of the MQTT-JSON-RPC instance and `<rid>` is
|
|
157
|
+
`<cid>` is the UUID v1 of the MQTT-JSON-RPC client instance and `<rid>` is
|
|
159
158
|
the UUID v1 of the particular method request. The `<cid>` is used for
|
|
160
159
|
sending back the JSON-RPC 2.0 response message to the requestor only.
|
|
160
|
+
The `<rid>` is used for correlating the response to the request only.
|
|
161
161
|
|
|
162
162
|
Example
|
|
163
163
|
-------
|
|
@@ -193,7 +193,7 @@ user example
|
|
|
193
193
|
topic readwrite example/#
|
|
194
194
|
```
|
|
195
195
|
|
|
196
|
-
...and an `example` user in `mosquitto-pwd.txt` like:
|
|
196
|
+
...and an `example` user (with password `example`) in `mosquitto-pwd.txt` like:
|
|
197
197
|
|
|
198
198
|
```
|
|
199
199
|
example:$6$awYNe6oCAi+xlvo5$mWIUqyy4I0O3nJ99lP1mkRVqsDGymF8en5NChQQxf7KrVJLUp1SzrrVDe94wWWJa3JGIbOXD9wfFGZdi948e6A==
|
|
@@ -214,13 +214,13 @@ const mqtt = MQTT.connect("wss://127.0.0.1:8889", {
|
|
|
214
214
|
|
|
215
215
|
const rpc = new RPC(mqtt)
|
|
216
216
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
217
|
+
mqtt.on("error", (err) => { console.log("ERROR", err) })
|
|
218
|
+
mqtt.on("offline", () => { console.log("OFFLINE") })
|
|
219
|
+
mqtt.on("close", () => { console.log("CLOSE") })
|
|
220
|
+
mqtt.on("reconnect", () => { console.log("RECONNECT") })
|
|
221
|
+
mqtt.on("message", (topic, message) => { console.log("RECEIVED", topic, message.toString()) })
|
|
222
222
|
|
|
223
|
-
|
|
223
|
+
mqtt.on("connect", () => {
|
|
224
224
|
console.log("CONNECT")
|
|
225
225
|
rpc.register("example/hello", (a1, a2) => {
|
|
226
226
|
console.log("example/hello: request: ", a1, a2)
|
|
@@ -228,7 +228,7 @@ rpc.on("connect", () => {
|
|
|
228
228
|
})
|
|
229
229
|
rpc.call("example/hello", "world", 42).then((result) => {
|
|
230
230
|
console.log("example/hello sucess: ", result)
|
|
231
|
-
|
|
231
|
+
mqtt.end()
|
|
232
232
|
}).catch((err) => {
|
|
233
233
|
console.log("example/hello error: ", err)
|
|
234
234
|
})
|
|
@@ -250,7 +250,7 @@ CLOSE
|
|
|
250
250
|
License
|
|
251
251
|
-------
|
|
252
252
|
|
|
253
|
-
Copyright (c) 2018-
|
|
253
|
+
Copyright (c) 2018-2025 Dr. Ralf S. Engelschall (http://engelschall.com/)
|
|
254
254
|
|
|
255
255
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
256
256
|
a copy of this software and associated documentation files (the
|