@burgrp/mqtt-mtl 1.1.1 → 1.1.3

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/mqtt-mtl.js +27 -26
  2. package/package.json +1 -1
package/mqtt-mtl.js CHANGED
@@ -2,6 +2,26 @@ const mqtt = require("mqtt");
2
2
 
3
3
  let brokers = {};
4
4
 
5
+ function matchesTopicWildcards(topic, filter) {
6
+ let topicParts = topic.split("/");
7
+ let filterParts = filter.split("/");
8
+
9
+ for (let i = 0; i < filterParts.length; i++) {
10
+ let filterPart = filterParts[i];
11
+ let topicPart = topicParts[i];
12
+
13
+ if (filterPart === "#") {
14
+ return true;
15
+ } else if (filterPart === "+") {
16
+ continue;
17
+ } else if (filterPart !== topicPart) {
18
+ return false;
19
+ }
20
+ }
21
+
22
+ return topicParts.length === filterParts.length;
23
+ }
24
+
5
25
  module.exports = brokerUrl => {
6
26
 
7
27
  if (!brokers[brokerUrl]) {
@@ -11,29 +31,18 @@ module.exports = brokerUrl => {
11
31
  protocolVersion: 5
12
32
  }),
13
33
 
14
- listeners: {},
15
- nextListenerId: 1,
34
+ listeners: [],
16
35
 
17
36
  publish(topic, message) {
18
37
  this.connection.publish(topic, message);
19
38
  },
20
39
 
21
40
  subscribe(topic, listener) {
22
-
23
- let id = this.nextListenerId++;
24
-
25
- this.listeners[id] = listener;
26
-
27
- this.connection.subscribe(topic, {
28
- properties: {
29
- subscriptionIdentifier: id
30
- }
31
- });
32
-
33
- return id;
41
+ this.listeners.push({topic, listener});
42
+ this.connection.subscribe(topic);
34
43
  },
35
44
 
36
- unsubscribe(listenerId) {
45
+ unsubscribe(listener) {
37
46
  //TODO: unsubscribe from MQTT if it's last subscription in this.subscriptions[]
38
47
  //delete this.listeners[listenerId];
39
48
  throw new Error("Not implemented yet");
@@ -43,23 +52,15 @@ module.exports = brokerUrl => {
43
52
  brokers[brokerUrl] = broker;
44
53
 
45
54
  broker.connection.on("message", (topic, message, packet) => {
46
-
47
- let ids = (packet.properties && packet.properties.subscriptionIdentifier) || [];
48
- if (!(ids instanceof Array)) {
49
- ids = [ids];
50
- }
51
-
52
- for (let id of ids) {
53
- let listener = broker.listeners[id];
54
- if (listener) {
55
+ for (let {topic: filter, listener} of broker.listeners) {
56
+ if (matchesTopicWildcards(topic, filter)) {
55
57
  try {
56
- listener(topic, message);
58
+ listener(topic, message.toString());
57
59
  } catch (e) {
58
60
  console.error("Unhandled MQTT listener exception:", e);
59
61
  }
60
62
  }
61
63
  }
62
-
63
64
  });
64
65
  }
65
66
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@burgrp/mqtt-mtl",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Selective multi-topic listener extension to mqtt library",
5
5
  "main": "mqtt-mtl.js",
6
6
  "scripts": {