@mysetup/mqtt 2.0.1 → 2.0.4
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 +43 -57
- package/dist/index.d.ts +0 -1
- package/dist/mqttClient.d.ts +0 -1
- package/package.json +56 -43
- package/dist/index.d.ts.map +0 -1
- package/dist/mqttClient.d.ts.map +0 -1
- package/dist/test/test-mqtt.d.ts +0 -2
- package/dist/test/test-mqtt.d.ts.map +0 -1
- package/dist/test/test-mqtt.js +0 -22
package/README.md
CHANGED
|
@@ -1,82 +1,68 @@
|
|
|
1
|
-
# @mysetup/mqtt
|
|
1
|
+
# @mysetup/mqtt
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- Connect to MQTT brokers with custom configuration
|
|
8
|
-
- Subscribe and unsubscribe to topics
|
|
9
|
-
- Publish messages to topics
|
|
10
|
-
- Handle connection events (connect, error, reconnect, close, offline)
|
|
11
|
-
- Cache and reuse MQTT clients by `clientId`
|
|
12
|
-
- Custom message callbacks
|
|
13
|
-
- Integration with a logger and cache system
|
|
3
|
+
MQTT helpers for creating connections, subscribing, publishing, and reusing clients.
|
|
14
4
|
|
|
15
5
|
## Installation
|
|
16
6
|
|
|
17
|
-
```
|
|
18
|
-
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @mysetup/mqtt
|
|
19
9
|
```
|
|
20
10
|
|
|
11
|
+
## Supported libraries and runtimes
|
|
12
|
+
|
|
13
|
+
| Supported | Notes |
|
|
14
|
+
| ---------------------- | ------------------------------- |
|
|
15
|
+
| Node.js | Full support |
|
|
16
|
+
| Next.js server runtime | Supported in backend-only usage |
|
|
17
|
+
| Browser apps | Not supported |
|
|
18
|
+
|
|
21
19
|
## Usage
|
|
22
20
|
|
|
23
|
-
```
|
|
24
|
-
import {
|
|
21
|
+
```ts
|
|
22
|
+
import {
|
|
23
|
+
mqttCreateConnection,
|
|
24
|
+
mqttDisconnect,
|
|
25
|
+
mqttPublish,
|
|
26
|
+
mqttSub,
|
|
27
|
+
} from "@mysetup/mqtt";
|
|
25
28
|
|
|
26
|
-
const clientId = "
|
|
29
|
+
const clientId = "app-client";
|
|
27
30
|
const config = {
|
|
28
31
|
protocol: "mqtt",
|
|
29
32
|
host: "localhost",
|
|
30
33
|
port: 1883,
|
|
31
34
|
path: "",
|
|
32
|
-
username: "user",
|
|
33
|
-
password: "pass",
|
|
34
35
|
};
|
|
35
36
|
|
|
36
|
-
const topicList = ["my/topic"];
|
|
37
|
-
|
|
38
37
|
mqttCreateConnection({
|
|
39
38
|
clientId,
|
|
40
39
|
config,
|
|
41
|
-
topicList,
|
|
42
|
-
messageCallback: (topic,
|
|
43
|
-
console.log(
|
|
40
|
+
topicList: ["updates/topic"],
|
|
41
|
+
messageCallback: (topic, payload) => {
|
|
42
|
+
console.log(topic, payload);
|
|
44
43
|
},
|
|
45
|
-
environment: "development",
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
// Subscribe to topics
|
|
49
|
-
mqttSub(topicList, clientId, (topic, message) => {
|
|
50
|
-
console.log(`Received: ${message} on ${topic}`);
|
|
44
|
+
environment: "development",
|
|
51
45
|
});
|
|
52
46
|
|
|
53
|
-
|
|
54
|
-
mqttPublish("
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
47
|
+
mqttSub(["updates/topic"], clientId);
|
|
48
|
+
mqttPublish("updates/topic", { hello: "world" }, clientId);
|
|
49
|
+
mqttDisconnect(
|
|
50
|
+
{
|
|
51
|
+
clientId,
|
|
52
|
+
config,
|
|
53
|
+
topicList: ["updates/topic"],
|
|
54
|
+
messageCallback: () => {},
|
|
55
|
+
},
|
|
56
|
+
false,
|
|
57
|
+
);
|
|
58
58
|
```
|
|
59
59
|
|
|
60
|
-
##
|
|
61
|
-
|
|
62
|
-
- mqttCreateConnection(props: ConnectionProps): MqttClient
|
|
63
|
-
Creates or retrieves a cached MQTT client and connects to the broker.
|
|
64
|
-
|
|
65
|
-
- mqttSub(topicList, clientId, messageCallback?, environment?)
|
|
66
|
-
Subscribes to the given topics for the specified client.
|
|
67
|
-
|
|
68
|
-
- mqttUnSub(topicList, clientId, environment?)
|
|
69
|
-
Unsubscribes from the given topics for the specified client.
|
|
70
|
-
|
|
71
|
-
- mqttPublish(topic, message, clientId, environment?)
|
|
72
|
-
Publishes a message to a topic.
|
|
73
|
-
|
|
74
|
-
- mqttDisconnect(props, reinit, environment?)
|
|
75
|
-
Disconnects the client and optionally reinitializes the connection.
|
|
76
|
-
|
|
77
|
-
- getMqttClient(clientId)
|
|
78
|
-
Retrieves the cached MQTT client by ID.
|
|
79
|
-
|
|
80
|
-
## License
|
|
60
|
+
## Exports
|
|
81
61
|
|
|
82
|
-
|
|
62
|
+
- `mqttCreateConnection`
|
|
63
|
+
- `mqttSub`
|
|
64
|
+
- `mqttUnSub`
|
|
65
|
+
- `mqttPublish`
|
|
66
|
+
- `mqttDisconnect`
|
|
67
|
+
- `getMqttClient`
|
|
68
|
+
- `removeMqttClient`
|
package/dist/index.d.ts
CHANGED
package/dist/mqttClient.d.ts
CHANGED
|
@@ -25,4 +25,3 @@ declare const getMqttClient: (id: string) => mqtt.MqttClient | null;
|
|
|
25
25
|
declare const removeMqttClient: (id: string) => void;
|
|
26
26
|
export { mqttCreateConnection, mqttSub, mqttUnSub, mqttDisconnect, mqttPublish, getMqttClient, removeMqttClient, };
|
|
27
27
|
export type { ConnectionProps, MqttConfigProps, MqttClient };
|
|
28
|
-
//# sourceMappingURL=mqttClient.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,45 +1,58 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
2
|
+
"name": "@mysetup/mqtt",
|
|
3
|
+
"version": "2.0.4",
|
|
4
|
+
"description": "MQTT client helpers for Node.js applications.",
|
|
5
|
+
"author": "krishnaraj <krishnaraj.webdev@gmail.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"mqtt",
|
|
9
|
+
"iot",
|
|
10
|
+
"nodejs",
|
|
11
|
+
"typescript",
|
|
12
|
+
"messaging"
|
|
13
|
+
],
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"main": "dist/index.js",
|
|
18
|
+
"types": "dist/index.d.ts",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"import": "./dist/index.js",
|
|
27
|
+
"require": "./dist/index.js"
|
|
16
28
|
},
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
"
|
|
45
|
-
}
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"mqtt": "^5.10.3",
|
|
33
|
+
"@mysetup/logger": "^2.0.4",
|
|
34
|
+
"@mysetup/cache": "^2.0.4"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/node": "^22.16.4",
|
|
38
|
+
"nodemon": "3.1.7",
|
|
39
|
+
"ts-node": "^10.9.2",
|
|
40
|
+
"typescript": "5.5.4",
|
|
41
|
+
"@mysetup/eslint-config": "^2.0.5",
|
|
42
|
+
"@mysetup/tsconfig": "^2.0.4",
|
|
43
|
+
"@mysetup/prettier-config": "^2.0.4"
|
|
44
|
+
},
|
|
45
|
+
"prettier": "@mysetup/prettier-config",
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=20.15.1"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"dev": "nodemon",
|
|
51
|
+
"lint": "node ../scripts/run-eslint.cjs .",
|
|
52
|
+
"typecheck": "tsc --noEmit",
|
|
53
|
+
"build": "node ../scripts/package-fs.cjs remove dist && tsc -p tsconfig.build.json",
|
|
54
|
+
"format": "prettier --write \"**/*.{ts,tsx,md,js}\"",
|
|
55
|
+
"checks": "pnpm typecheck && pnpm lint && pnpm build",
|
|
56
|
+
"clean": "node ../scripts/package-fs.cjs remove node_modules .swc dist pnpm-lock.yaml && echo \"Cleaned\""
|
|
57
|
+
}
|
|
58
|
+
}
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC"}
|
package/dist/mqttClient.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"mqttClient.d.ts","sourceRoot":"","sources":["../mqttClient.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAGvC,KAAK,YAAY,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,KAAK,CAAC;AAEpD,UAAU,eAAe;IACrB,QAAQ,EAAE,YAAY,CAAC;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,UAAU,eAAe;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,eAAe,CAAC;IACxB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,eAAe,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;IAC1D,WAAW,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CACpC;AAkED,QAAA,MAAM,OAAO,cACE,MAAM,EAAE,YACT,MAAM,gBACF,MAAM,SAwBvB,CAAC;AAEF,QAAA,MAAM,SAAS,cACA,MAAM,EAAE,YACT,MAAM,gBACF,MAAM,SAoBvB,CAAC;AAEF,QAAA,MAAM,cAAc,UACT,eAAe,UACd,OAAO,gBACD,MAAM,SAuBvB,CAAC;AAEF,QAAA,MAAM,WAAW,UACN,MAAM,WACJ,MAAM,YACL,MAAM,gBACF,MAAM,SAqBvB,CAAC;AAsBF,QAAA,MAAM,oBAAoB,UAAW,eAAe,2BAEnD,CAAC;AAEF,QAAA,MAAM,aAAa,OAAQ,MAAM,2BAEhC,CAAC;AAEF,QAAA,MAAM,gBAAgB,OAAQ,MAAM,SAGnC,CAAC;AAEF,OAAO,EACH,oBAAoB,EACpB,OAAO,EACP,SAAS,EACT,cAAc,EACd,WAAW,EACX,aAAa,EACb,gBAAgB,GACnB,CAAC;AAEF,YAAY,EAAE,eAAe,EAAE,eAAe,EAAE,UAAU,EAAE,CAAC"}
|
package/dist/test/test-mqtt.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"test-mqtt.d.ts","sourceRoot":"","sources":["../../test/test-mqtt.ts"],"names":[],"mappings":""}
|
package/dist/test/test-mqtt.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
var logger_1 = require("@mysetup/logger");
|
|
4
|
-
var mqttClient_1 = require("../mqttClient");
|
|
5
|
-
var _a = process.env, ENVIRONMENT = _a.ENVIRONMENT, MQTT_HOST = _a.MQTT_HOST, MQTT_USERNAME = _a.MQTT_USERNAME, MQTT_PASSWORD = _a.MQTT_PASSWORD, MQTT_PORT = _a.MQTT_PORT;
|
|
6
|
-
var CLIENT_ID = "testClient-".concat(Math.random().toString(16).slice(2, 8));
|
|
7
|
-
(0, mqttClient_1.mqttCreateConnection)({
|
|
8
|
-
clientId: CLIENT_ID,
|
|
9
|
-
config: {
|
|
10
|
-
host: String(MQTT_HOST),
|
|
11
|
-
port: Number(MQTT_PORT),
|
|
12
|
-
protocol: "mqtts",
|
|
13
|
-
path: "",
|
|
14
|
-
username: MQTT_USERNAME,
|
|
15
|
-
password: MQTT_PASSWORD,
|
|
16
|
-
},
|
|
17
|
-
environment: String(ENVIRONMENT),
|
|
18
|
-
topicList: ["#"],
|
|
19
|
-
messageCallback: function (topic, message) {
|
|
20
|
-
logger_1.logger.info("Message received \u2192 topic: ".concat(topic, ", message: ").concat(message.toString()));
|
|
21
|
-
},
|
|
22
|
-
});
|