@machhub-dev/sdk-ts 1.0.2 → 1.0.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.
@@ -7,4 +7,5 @@ export declare class Tag {
7
7
  getAllTags(): Promise<string[]>;
8
8
  publish(topic: string, data: any): Promise<void>;
9
9
  subscribe(topic: string, callback: (data: any, topic?: string) => void): Promise<void>;
10
+ unsubscribe(topic: string): Promise<void>;
10
11
  }
@@ -21,5 +21,11 @@ class Tag {
21
21
  }
22
22
  this.mqttService.addTopicHandler(topic, callback);
23
23
  }
24
+ async unsubscribe(topic) {
25
+ if (!this.mqttService) {
26
+ throw new Error("MQTT service not connected");
27
+ }
28
+ this.mqttService.removeTopicHandler(topic);
29
+ }
24
30
  }
25
31
  exports.Tag = Tag;
@@ -8,6 +8,7 @@ export declare class MQTTService {
8
8
  static getInstance(url?: string, developerKey?: string): Promise<MQTTService>;
9
9
  static resetInstance(): void;
10
10
  addTopicHandler(topic: string, handler: (message: unknown, topic?: string) => void): void;
11
+ removeTopicHandler(topic: string): void;
11
12
  clearTopics(): void;
12
13
  publish(topic: string, message: unknown): boolean;
13
14
  private attachMessageListener;
@@ -39,6 +39,12 @@ class MQTTService {
39
39
  // addTopicHandler Adds a topic and handler to the subscribed list
40
40
  addTopicHandler(topic, handler) {
41
41
  try {
42
+ // Check if already subscribed to this topic
43
+ const existingSubscription = this.subscribedTopics.find(sub => sub.topic === topic);
44
+ if (existingSubscription) {
45
+ // If already subscribed, unsubscribe first to get retained message again
46
+ this.removeTopicHandler(topic);
47
+ }
42
48
  this.subscribedTopics.push({ topic, handler });
43
49
  if (topic == "")
44
50
  return;
@@ -53,6 +59,24 @@ class MQTTService {
53
59
  console.error(`Failed to subscribe to topic ${topic}:`, e);
54
60
  }
55
61
  }
62
+ // removeTopicHandler removes a specific topic handler and unsubscribes from the topic
63
+ removeTopicHandler(topic) {
64
+ try {
65
+ // Remove all handlers for this topic
66
+ this.subscribedTopics = this.subscribedTopics.filter(sub => sub.topic !== topic);
67
+ // Unsubscribe from the MQTT topic
68
+ if (topic && topic !== "") {
69
+ this.client.unsubscribe(topic, (err) => {
70
+ if (err) {
71
+ console.error(`Failed to unsubscribe from topic ${topic}:`, err);
72
+ }
73
+ });
74
+ }
75
+ }
76
+ catch (e) {
77
+ console.error(`Failed to unsubscribe from topic ${topic}:`, e);
78
+ }
79
+ }
56
80
  // clearTopics clears all the topics subscribed to
57
81
  clearTopics() {
58
82
  this.subscribedTopics = [];
@@ -7,4 +7,5 @@ export declare class Tag {
7
7
  getAllTags(): Promise<string[]>;
8
8
  publish(topic: string, data: any): Promise<void>;
9
9
  subscribe(topic: string, callback: (data: any, topic?: string) => void): Promise<void>;
10
+ unsubscribe(topic: string): Promise<void>;
10
11
  }
@@ -18,4 +18,10 @@ export class Tag {
18
18
  }
19
19
  this.mqttService.addTopicHandler(topic, callback);
20
20
  }
21
+ async unsubscribe(topic) {
22
+ if (!this.mqttService) {
23
+ throw new Error("MQTT service not connected");
24
+ }
25
+ this.mqttService.removeTopicHandler(topic);
26
+ }
21
27
  }
@@ -8,6 +8,7 @@ export declare class MQTTService {
8
8
  static getInstance(url?: string, developerKey?: string): Promise<MQTTService>;
9
9
  static resetInstance(): void;
10
10
  addTopicHandler(topic: string, handler: (message: unknown, topic?: string) => void): void;
11
+ removeTopicHandler(topic: string): void;
11
12
  clearTopics(): void;
12
13
  publish(topic: string, message: unknown): boolean;
13
14
  private attachMessageListener;
@@ -33,6 +33,12 @@ export class MQTTService {
33
33
  // addTopicHandler Adds a topic and handler to the subscribed list
34
34
  addTopicHandler(topic, handler) {
35
35
  try {
36
+ // Check if already subscribed to this topic
37
+ const existingSubscription = this.subscribedTopics.find(sub => sub.topic === topic);
38
+ if (existingSubscription) {
39
+ // If already subscribed, unsubscribe first to get retained message again
40
+ this.removeTopicHandler(topic);
41
+ }
36
42
  this.subscribedTopics.push({ topic, handler });
37
43
  if (topic == "")
38
44
  return;
@@ -47,6 +53,24 @@ export class MQTTService {
47
53
  console.error(`Failed to subscribe to topic ${topic}:`, e);
48
54
  }
49
55
  }
56
+ // removeTopicHandler removes a specific topic handler and unsubscribes from the topic
57
+ removeTopicHandler(topic) {
58
+ try {
59
+ // Remove all handlers for this topic
60
+ this.subscribedTopics = this.subscribedTopics.filter(sub => sub.topic !== topic);
61
+ // Unsubscribe from the MQTT topic
62
+ if (topic && topic !== "") {
63
+ this.client.unsubscribe(topic, (err) => {
64
+ if (err) {
65
+ console.error(`Failed to unsubscribe from topic ${topic}:`, err);
66
+ }
67
+ });
68
+ }
69
+ }
70
+ catch (e) {
71
+ console.error(`Failed to unsubscribe from topic ${topic}:`, e);
72
+ }
73
+ }
50
74
  // clearTopics clears all the topics subscribed to
51
75
  clearTopics() {
52
76
  this.subscribedTopics = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@machhub-dev/sdk-ts",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "MACHHUB TYPESCRIPT SDK",
5
5
  "keywords": [
6
6
  "machhub",
@@ -27,4 +27,11 @@ export class Tag {
27
27
  }
28
28
  this.mqttService.addTopicHandler(topic, callback);
29
29
  }
30
+
31
+ async unsubscribe(topic: string): Promise<void> {
32
+ if (!this.mqttService) {
33
+ throw new Error("MQTT service not connected");
34
+ }
35
+ this.mqttService.removeTopicHandler(topic);
36
+ }
30
37
  }
@@ -48,6 +48,14 @@ export class MQTTService {
48
48
  // addTopicHandler Adds a topic and handler to the subscribed list
49
49
  public addTopicHandler(topic: string, handler: (message: unknown, topic?: string) => void): void {
50
50
  try {
51
+ // Check if already subscribed to this topic
52
+ const existingSubscription = this.subscribedTopics.find(sub => sub.topic === topic);
53
+
54
+ if (existingSubscription) {
55
+ // If already subscribed, unsubscribe first to get retained message again
56
+ this.removeTopicHandler(topic);
57
+ }
58
+
51
59
  this.subscribedTopics.push({ topic, handler });
52
60
  if (topic == "") return;
53
61
  // console.log("New Subscription Handler:", topic);
@@ -61,6 +69,25 @@ export class MQTTService {
61
69
  }
62
70
  }
63
71
 
72
+ // removeTopicHandler removes a specific topic handler and unsubscribes from the topic
73
+ public removeTopicHandler(topic: string): void {
74
+ try {
75
+ // Remove all handlers for this topic
76
+ this.subscribedTopics = this.subscribedTopics.filter(sub => sub.topic !== topic);
77
+
78
+ // Unsubscribe from the MQTT topic
79
+ if (topic && topic !== "") {
80
+ this.client.unsubscribe(topic, (err?: unknown) => {
81
+ if (err) {
82
+ console.error(`Failed to unsubscribe from topic ${topic}:`, err);
83
+ }
84
+ });
85
+ }
86
+ } catch (e) {
87
+ console.error(`Failed to unsubscribe from topic ${topic}:`, e);
88
+ }
89
+ }
90
+
64
91
  // clearTopics clears all the topics subscribed to
65
92
  public clearTopics(): void {
66
93
  this.subscribedTopics = [];