@mysetup/mqtt 1.14.7 → 1.14.9

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.
Files changed (2) hide show
  1. package/README.md +73 -18
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,27 +1,82 @@
1
- ## Getting Started
1
+ # @mysetup/mqtt-client
2
2
 
3
- ```bash
4
- import { logger } from "@mysetup/logger";
5
- import { mqttCreateConnection } from "../mqttClient";
3
+ A utility for managing MQTT connections, subscriptions, publishing, and client caching, built on top of the [mqtt](https://www.npmjs.com/package/mqtt) library.
6
4
 
7
- const CLIENT_ID = `testClient-${Math.random().toString(16).slice(2, 8)}`;
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
14
+
15
+ ## Installation
16
+
17
+ ```sh
18
+ npm install @mysetup/mqtt-client
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ```sh
24
+ import { mqttCreateConnection, mqttSub, mqttPublish, mqttDisconnect } from "@mysetup/mqtt-client";
25
+
26
+ const clientId = "myClientId";
27
+ const config = {
28
+ protocol: "mqtt",
29
+ host: "localhost",
30
+ port: 1883,
31
+ path: "",
32
+ username: "user",
33
+ password: "pass",
34
+ };
35
+
36
+ const topicList = ["my/topic"];
8
37
 
9
38
  mqttCreateConnection({
10
- clientId: CLIENT_ID,
11
- config: {
12
- host: "localhost",
13
- port: 1883,
14
- protocol: "mqtt",
15
- path: "",
16
- username: process.env.MQTT_USERNAME,
17
- password: process.env.MQTT_PASSWORD,
18
- },
19
- topicList: ["mj/ems/test"],
39
+ clientId,
40
+ config,
41
+ topicList,
20
42
  messageCallback: (topic, message) => {
21
- logger.info(
22
- `Message received → topic: ${topic}, message: ${message.toString()}`,
23
- );
43
+ console.log(`Received on ${topic}: ${message}`);
24
44
  },
45
+ environment: "development", // Optional: enables verbose logging
46
+ });
47
+
48
+ // Subscribe to topics
49
+ mqttSub(topicList, clientId, (topic, message) => {
50
+ console.log(`Received: ${message} on ${topic}`);
25
51
  });
26
52
 
53
+ // Publish a message
54
+ mqttPublish("my/topic", { hello: "world" }, clientId);
55
+
56
+ // Disconnect
57
+ mqttDisconnect({ clientId, config, topicList }, false);
27
58
  ```
59
+
60
+ ## Functions
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
81
+
82
+ MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mysetup/mqtt",
3
- "version": "1.14.7",
3
+ "version": "1.14.9",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "files": [