@mysetup/mqtt 1.14.0 → 1.14.2

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.
@@ -7,12 +7,14 @@ interface ConnectionProps {
7
7
  option: MQttOptionProps;
8
8
  topicList: string[];
9
9
  messageCallback?: (topic: string, message: string) => void;
10
+ path?: string;
10
11
  }
11
12
  declare const mqttSub: (topicList: string[], messageCallback?: (topic: string, message: string) => void, client?: MqttClient) => void;
12
13
  declare const mqttUnSub: (topicList: string[], client?: MqttClient) => void;
13
14
  declare const mqttDisconnect: (props: ConnectionProps, reinit: boolean, client?: MqttClient) => void;
14
15
  declare const mqttOnMessage: (messageCallback: (topic: string, message: string) => void, client?: MqttClient) => void;
16
+ declare const mqttPublish: (topic: string, message: object, client?: MqttClient) => void;
15
17
  declare const mqttCreateConnection: (props: ConnectionProps) => mqtt.MqttClient;
16
18
  declare const getMqttClient: () => string;
17
- export { mqttCreateConnection, mqttSub, mqttUnSub, mqttDisconnect, mqttOnMessage, getMqttClient, };
19
+ export { mqttCreateConnection, mqttSub, mqttUnSub, mqttDisconnect, mqttOnMessage, getMqttClient, mqttPublish, };
18
20
  //# sourceMappingURL=mqttClient.d.ts.map
@@ -1 +1 @@
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,YAAY,EAAE,UAAU,EAAE,CAAC;AAE3B,KAAK,eAAe,GAAG,KAAK,GAAG,QAAQ,CAAC;AAExC,UAAU,eAAe;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,eAAe,CAAC;IACxB,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,eAAe,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,CAAC;CAC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AAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,EAC1D,SAAS,UAAU,SAwBtB,CAAC;AAEF,QAAA,MAAM,SAAS,GAAI,WAAW,MAAM,EAAE,EAAE,SAAS,UAAU,SAkB1D,CAAC;AAEF,QAAA,MAAM,cAAc,GAChB,OAAO,eAAe,EACtB,QAAQ,OAAO,EACf,SAAS,UAAU,SAiBtB,CAAC;AAEF,QAAA,MAAM,aAAa,GACf,iBAAiB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,IAAI,EACzD,SAAS,UAAU,SAStB,CAAC;AAEF,QAAA,MAAM,WAAW,GAAI,OAAO,MAAM,EAAE,SAAS,MAAM,EAAE,SAAS,UAAU,SAmBvE,CAAC;AA2BF,QAAA,MAAM,oBAAoB,GAAI,OAAO,eAAe,oBAEnD,CAAC;AAEF,QAAA,MAAM,aAAa,cAElB,CAAC;AAEF,OAAO,EACH,oBAAoB,EACpB,OAAO,EACP,SAAS,EACT,cAAc,EACd,aAAa,EACb,aAAa,EACb,WAAW,GACd,CAAC"}
@@ -14,63 +14,69 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.getMqttClient = exports.mqttOnMessage = exports.mqttDisconnect = exports.mqttUnSub = exports.mqttSub = exports.mqttCreateConnection = void 0;
17
+ exports.mqttPublish = exports.getMqttClient = exports.mqttOnMessage = exports.mqttDisconnect = exports.mqttUnSub = exports.mqttSub = exports.mqttCreateConnection = void 0;
18
18
  var mqtt_1 = __importDefault(require("mqtt"));
19
19
  var logger_1 = require("@mysetup/logger");
20
20
  var cache_1 = require("@mysetup/cache");
21
+ var _a = process.env, NEXT_PUBLIC_ENVIRONMENT = _a.NEXT_PUBLIC_ENVIRONMENT, NEXT_PUBLIC_MQTT_HOST = _a.NEXT_PUBLIC_MQTT_HOST;
21
22
  var serverConnectOptions = {
22
- protocol: "mqtts",
23
- port: 8883,
24
- host: "70e21950e6974840b4ba9bb5817fe2b9.s1.eu.hivemq.cloud",
23
+ protocol: "mqtt",
24
+ port: 1883,
25
+ host: NEXT_PUBLIC_MQTT_HOST,
26
+ path: "",
25
27
  };
26
28
  var webConnectOptions = {
27
- protocol: "wss",
28
- port: 8000,
29
- host: "70e21950e6974840b4ba9bb5817fe2b9.s1.eu.hivemq.cloud",
29
+ protocol: "ws",
30
+ port: 1883,
31
+ host: NEXT_PUBLIC_MQTT_HOST,
32
+ path: "/mqtt",
30
33
  };
31
34
  var mqttClientId = "";
32
35
  var checkInitConnect = function (props) {
33
- var _a = props.option === "web" ? webConnectOptions : serverConnectOptions, protocol = _a.protocol, host = _a.host, port = _a.port;
34
- var connectUrl = "".concat(protocol, "://").concat(host, ":").concat(port);
36
+ var _a = props.option === "web" ? webConnectOptions : serverConnectOptions, protocol = _a.protocol, host = _a.host, port = _a.port, path = _a.path;
37
+ var connectUrl = "".concat(protocol, "://").concat(host, ":").concat(port).concat(path);
35
38
  var client = mqtt_1.default.connect(connectUrl, {
36
39
  clientId: props.value,
37
- clean: true,
40
+ clean: false, /// for retaining previous message purpose
38
41
  rejectUnauthorized: true,
39
42
  resubscribe: true,
40
43
  keepalive: 300,
41
- username: "mj_ems_hivemq",
42
- password: "Mjmqtt123",
44
+ username: "mqttadmin",
45
+ password: "mqttadmin",
43
46
  });
44
47
  mqttClientId = props.value;
45
48
  client.setMaxListeners(100);
49
+ if (props.messageCallback) {
50
+ mqttOnMessage(props.messageCallback, client); // 💡 Always attach
51
+ }
46
52
  client.on("connect", function () {
47
- if (process.env.NEXT_PUBLIC_ENVIRONMENT === "dev") {
53
+ if (NEXT_PUBLIC_ENVIRONMENT === "development") {
48
54
  logger_1.logger.info("MQTT connection successful");
49
55
  }
50
56
  (0, cache_1.setCacheData)("mqttClientStatus", "connect");
51
57
  mqttSub(props.topicList, props.messageCallback, client);
52
58
  });
53
59
  client.on("error", function (err) {
54
- if (process.env.NEXT_PUBLIC_ENVIRONMENT === "dev") {
60
+ if (NEXT_PUBLIC_ENVIRONMENT === "development") {
55
61
  logger_1.logger.error("MQTT Connection error: ".concat(JSON.stringify(err)));
56
62
  }
57
63
  (0, cache_1.setCacheData)("mqttClientStatus", "error");
58
64
  mqttDisconnect(props, true, client);
59
65
  });
60
66
  client.on("reconnect", function () {
61
- if (process.env.NEXT_PUBLIC_ENVIRONMENT === "dev") {
67
+ if (NEXT_PUBLIC_ENVIRONMENT === "development") {
62
68
  logger_1.logger.info("MQTT Reconnecting");
63
69
  }
64
70
  (0, cache_1.setCacheData)("mqttClientStatus", "reconnect");
65
71
  });
66
72
  client.on("close", function () {
67
- if (process.env.NEXT_PUBLIC_ENVIRONMENT === "dev") {
73
+ if (NEXT_PUBLIC_ENVIRONMENT === "development") {
68
74
  logger_1.logger.info("MQTT connection closed");
69
75
  }
70
76
  (0, cache_1.setCacheData)("mqttClientStatus", "close");
71
77
  });
72
78
  client.on("offline", function () {
73
- if (process.env.NEXT_PUBLIC_ENVIRONMENT === "dev") {
79
+ if (NEXT_PUBLIC_ENVIRONMENT === "development") {
74
80
  logger_1.logger.info("MQTT connection offline");
75
81
  }
76
82
  (0, cache_1.setCacheData)("mqttClientStatus", "offline");
@@ -81,13 +87,13 @@ var mqttSub = function (topicList, messageCallback, client) {
81
87
  try {
82
88
  client === null || client === void 0 ? void 0 : client.subscribe(topicList, { qos: 0 }, function (error, granted) {
83
89
  if (error) {
84
- logger_1.logger.error("Subscribe to topics error: ".concat(error.message));
90
+ logger_1.logger.error("MQTT Subscribe to topics error: ".concat(error.message));
85
91
  }
86
92
  var grantedList = [];
87
93
  granted === null || granted === void 0 ? void 0 : granted.forEach(function (element) {
88
94
  grantedList.push(element.topic);
89
- if (process.env.NEXT_PUBLIC_ENVIRONMENT === "dev") {
90
- logger_1.logger.info("Subscribed to topics: ".concat(element.topic));
95
+ if (NEXT_PUBLIC_ENVIRONMENT === "development") {
96
+ logger_1.logger.info("MQTT Subscribed to topics: ".concat(element.topic));
91
97
  }
92
98
  });
93
99
  (0, cache_1.setCacheData)("subscribedTopic", grantedList);
@@ -95,7 +101,7 @@ var mqttSub = function (topicList, messageCallback, client) {
95
101
  });
96
102
  }
97
103
  catch (error) {
98
- logger_1.logger.error("Subscribed error ".concat(JSON.stringify(error)));
104
+ logger_1.logger.error("MQTT Subscribed error ".concat(JSON.stringify(error)));
99
105
  }
100
106
  };
101
107
  exports.mqttSub = mqttSub;
@@ -103,25 +109,25 @@ var mqttUnSub = function (topicList, client) {
103
109
  try {
104
110
  client === null || client === void 0 ? void 0 : client.unsubscribe(topicList, function (error) {
105
111
  if (error) {
106
- logger_1.logger.error("Unsubscribe to topics error: ".concat(error.message));
112
+ logger_1.logger.error("MQTT Unsubscribe to topics error: ".concat(error.message));
107
113
  return;
108
114
  }
109
- if (process.env.NEXT_PUBLIC_ENVIRONMENT === "dev") {
115
+ if (NEXT_PUBLIC_ENVIRONMENT === "development") {
110
116
  topicList.forEach(function (topic) {
111
- logger_1.logger.info("unsubscribed to topic: ".concat(topic));
117
+ logger_1.logger.info("MQTT Unsubscribed to topic: ".concat(topic));
112
118
  });
113
119
  }
114
120
  });
115
121
  }
116
122
  catch (error) {
117
- logger_1.logger.error("unsubscribed error: ".concat(JSON.stringify(error)));
123
+ logger_1.logger.error("MQTT Unsubscribed error: ".concat(JSON.stringify(error)));
118
124
  }
119
125
  };
120
126
  exports.mqttUnSub = mqttUnSub;
121
127
  var mqttDisconnect = function (props, reinit, client) {
122
128
  try {
123
129
  client === null || client === void 0 ? void 0 : client.end(false, function () {
124
- if (process.env.NEXT_PUBLIC_ENVIRONMENT === "dev") {
130
+ if (NEXT_PUBLIC_ENVIRONMENT === "development") {
125
131
  logger_1.logger.info("MQTT disconnected successfully");
126
132
  }
127
133
  if (reinit) {
@@ -130,7 +136,7 @@ var mqttDisconnect = function (props, reinit, client) {
130
136
  });
131
137
  }
132
138
  catch (error) {
133
- logger_1.logger.error("disconnect error: ".concat(JSON.stringify(error)));
139
+ logger_1.logger.error("MQTT Disconnect error: ".concat(JSON.stringify(error)));
134
140
  }
135
141
  };
136
142
  exports.mqttDisconnect = mqttDisconnect;
@@ -141,21 +147,43 @@ var mqttOnMessage = function (messageCallback, client) {
141
147
  });
142
148
  }
143
149
  catch (error) {
144
- logger_1.logger.error("MQTT onmessage error: ".concat(JSON.stringify(error)));
150
+ logger_1.logger.error("MQTT On-message error: ".concat(JSON.stringify(error)));
145
151
  }
146
152
  };
147
153
  exports.mqttOnMessage = mqttOnMessage;
154
+ var mqttPublish = function (topic, message, client) {
155
+ try {
156
+ client === null || client === void 0 ? void 0 : client.publish(topic, JSON.stringify(message), { qos: 1, retain: true }, function (err) {
157
+ if (err) {
158
+ if (NEXT_PUBLIC_ENVIRONMENT === "development") {
159
+ logger_1.logger.error("MQTT Publish error: ".concat(err.message));
160
+ }
161
+ }
162
+ else if (NEXT_PUBLIC_ENVIRONMENT === "development") {
163
+ logger_1.logger.info("MQTT Payload published");
164
+ }
165
+ });
166
+ }
167
+ catch (error) {
168
+ logger_1.logger.error("MQTT Publish exception: ".concat(JSON.stringify(error)));
169
+ }
170
+ };
171
+ exports.mqttPublish = mqttPublish;
148
172
  var memoizedDb = function () {
149
173
  var cache = {};
150
174
  return function (props) {
151
175
  if (props.value in cache) {
152
- if (process.env.NEXT_PUBLIC_ENVIRONMENT === "dev") {
153
- logger_1.logger.info("fetching mqtt cache");
176
+ if (NEXT_PUBLIC_ENVIRONMENT === "development") {
177
+ logger_1.logger.info("MQTT Fetching mqtt cache");
178
+ }
179
+ //to invoke the message callback if it exists,when use cached value
180
+ if (props.messageCallback) {
181
+ mqttOnMessage(props.messageCallback, cache[props.value]);
154
182
  }
155
183
  return cache[props.value];
156
184
  }
157
- if (process.env.NEXT_PUBLIC_ENVIRONMENT === "dev") {
158
- logger_1.logger.info("Creating new mqtt cache");
185
+ if (NEXT_PUBLIC_ENVIRONMENT === "development") {
186
+ logger_1.logger.info("MQTT Creating new mqtt cache");
159
187
  }
160
188
  var result = checkInitConnect(props);
161
189
  cache[props.value] = result;
@@ -6,7 +6,7 @@ var CLIENT_ID = "testClient-".concat(Math.random().toString(16).slice(2, 8));
6
6
  (0, mqttClient_1.mqttCreateConnection)({
7
7
  value: CLIENT_ID,
8
8
  option: "server",
9
- topicList: ["mj/ems/08:D1:F9:A6:BB:84/"],
9
+ topicList: ["mj/ems/test"],
10
10
  messageCallback: function (topic, message) {
11
11
  logger_1.logger.info("Message received \u2192 topic: ".concat(topic, ", message: ").concat(message.toString()));
12
12
  },
package/package.json CHANGED
@@ -1,43 +1,46 @@
1
- {
2
- "name": "@mysetup/mqtt",
3
- "version": "1.14.0",
4
- "license": "MIT",
5
- "main": "dist/index.js",
6
- "files": [
7
- "dist"
8
- ],
9
- "exports": {
10
- ".": {
11
- "types": "./dist/index.d.ts",
12
- "import": "./dist/index.js",
13
- "require": "./dist/index.js"
14
- },
15
- "./package.json": "./package.json"
16
- },
17
- "scripts": {
18
- "lint": "eslint .",
19
- "typecheck": "tsc --noEmit",
20
- "build": "rm -rf ./dist && tsc",
21
- "format": "prettier --write \"**/*.{ts,tsx,md,js}\"",
22
- "checks": "pnpm typecheck && pnpm lint",
23
- "clean": "rm -rf node_modules .swc dist pnpm-lock.yaml && echo \"✅ Successfully removed \""
24
- },
25
- "dependencies": {
26
- "mqtt": "^5.10.3"
27
- },
28
- "devDependencies": {
29
- "@mysetup/cache": "latest",
30
- "@mysetup/eslint-config": "latest",
31
- "@mysetup/logger": "latest",
32
- "@mysetup/prettier-config": "latest",
33
- "@mysetup/tsconfig": "latest",
34
- "@types/node": "^22.16.4",
35
- "eslint": "8.57.0",
36
- "typescript": "^5.7.3"
37
- },
38
- "prettier": "@mysetup/prettier-config",
39
- "engines": {
40
- "node": ">=18.0.0"
41
- },
42
- "packageManager": "pnpm@9.9.0"
43
- }
1
+ {
2
+ "name": "@mysetup/mqtt",
3
+ "version": "1.14.2",
4
+ "license": "MIT",
5
+ "main": "dist/index.js",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.js"
14
+ },
15
+ "./package.json": "./package.json"
16
+ },
17
+ "scripts": {
18
+ "dev": "nodemon",
19
+ "lint": "eslint .",
20
+ "typecheck": "tsc --noEmit",
21
+ "build": "rm -rf ./dist && tsc",
22
+ "format": "prettier --write \"**/*.{ts,tsx,md,js}\"",
23
+ "checks": "pnpm typecheck && pnpm lint",
24
+ "clean": "rm -rf node_modules .swc dist pnpm-lock.yaml && echo \"✅ Successfully removed \""
25
+ },
26
+ "dependencies": {
27
+ "mqtt": "^5.10.3"
28
+ },
29
+ "devDependencies": {
30
+ "@mysetup/cache": "latest",
31
+ "@mysetup/eslint-config": "latest",
32
+ "@mysetup/logger": "latest",
33
+ "@mysetup/prettier-config": "latest",
34
+ "@mysetup/tsconfig": "latest",
35
+ "@types/node": "^22.16.4",
36
+ "eslint": "8.57.0",
37
+ "nodemon": "3.1.7",
38
+ "ts-node": "^10.9.2",
39
+ "typescript": "^5.7.3"
40
+ },
41
+ "prettier": "@mysetup/prettier-config",
42
+ "engines": {
43
+ "node": ">=18.0.0"
44
+ },
45
+ "packageManager": "pnpm@9.9.0"
46
+ }