@burgrp/mqtt-mtl 1.0.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.
- package/README.md +2 -0
- package/mqtt-mtl.js +50 -0
- package/package.json +22 -0
package/README.md
ADDED
package/mqtt-mtl.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const mqtt = require("mqtt");
|
|
2
|
+
|
|
3
|
+
let brokers = {};
|
|
4
|
+
|
|
5
|
+
module.exports = brokerUrl => {
|
|
6
|
+
|
|
7
|
+
if (!brokers[brokerUrl]) {
|
|
8
|
+
let broker = {
|
|
9
|
+
|
|
10
|
+
connection: mqtt.connect(brokerUrl),
|
|
11
|
+
|
|
12
|
+
listeners: [],
|
|
13
|
+
|
|
14
|
+
publish(topic, message) {
|
|
15
|
+
this.connection.publish(topic, message);
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
subscribe(topic, listener) {
|
|
19
|
+
this.connection.subscribe(topic);
|
|
20
|
+
|
|
21
|
+
let parsedTopic = topic.split("/");
|
|
22
|
+
this.listeners.push((actTopic, actMessage) => {
|
|
23
|
+
let parsedActTopic = actTopic.split("/");
|
|
24
|
+
let matches = true;
|
|
25
|
+
for (let i in parsedTopic) {
|
|
26
|
+
if (parsedTopic[i] === parsedActTopic[i] || parsedTopic[i] === "+") {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
if (parsedTopic[i] === "#") {
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
matches = false;
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
if (matches) {
|
|
36
|
+
listener(actTopic, actMessage);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
brokers[brokerUrl] = broker;
|
|
43
|
+
|
|
44
|
+
broker.connection.on("message", (topic, message) => {
|
|
45
|
+
broker.listeners.forEach(l => l(topic, message));
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return brokers[brokerUrl];
|
|
50
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@burgrp/mqtt-mtl",
|
|
3
|
+
"version": "1.0.2",
|
|
4
|
+
"description": "Selective multi-topic listener extension to mqtt library",
|
|
5
|
+
"main": "mqtt-mtl.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/device-farm/mqtt-mtl.git"
|
|
12
|
+
},
|
|
13
|
+
"author": "",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/device-farm/mqtt-mtl/issues"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/device-farm/mqtt-mtl#readme",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"mqtt": "^3.0.0"
|
|
21
|
+
}
|
|
22
|
+
}
|