From 85725a79b56350b952414d211f44a4e75210ab9a Mon Sep 17 00:00:00 2001 From: krislankford Date: Tue, 13 Sep 2016 07:07:41 -0700 Subject: [PATCH 01/13] Update README.md --- README.md | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e348e7f..8ba0a8c 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,31 @@ -# nativescipt-mqtt -NativeScipt MQTT Module +# nativescipt-mqtt - NativeScipt MQTT 3.1.1 Module + +This nativescript-mqtt module is a cross-platofrm javascript implementation leveraging native socket support and the open source [MQTT 3.1.1 PAHO library link](http://www.eclipse.org/paho). Currently the library only supports the websocket protocol for cross-platform on port 80 or 443 for SSL. + +## Usage Sample +### Connection Options + host: string + port: int - default 80 | useSSL 443 + path: string - default empty + useSSL: bool - default false + clientId: string - default UUID + retryOnDisconnect: bool - default false + +### Connection Events +ConnectionSuccess(): void +ConnectionFailure(): string +ConnectionLost(): string +MessageArrived(): Message + +### Message Object +```javascript +Message { + payload: string + bytes: ArrayBuffer + topic: string + qos: number +} +``` +### Sample Code + +### Callouts From af37b9eb95a95cec21ad83da41abc51bc6a1711c Mon Sep 17 00:00:00 2001 From: krislankford Date: Tue, 13 Sep 2016 07:09:11 -0700 Subject: [PATCH 02/13] Update README.md --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8ba0a8c..5470d2d 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,12 @@ This nativescript-mqtt module is a cross-platofrm javascript implementation leve ## Usage Sample ### Connection Options - host: string - port: int - default 80 | useSSL 443 - path: string - default empty - useSSL: bool - default false - clientId: string - default UUID - retryOnDisconnect: bool - default false +host: string +port: int - default 80 | useSSL 443 +path: string - default empty +useSSL: bool - default false +clientId: string - default UUID +retryOnDisconnect: bool - default false ### Connection Events ConnectionSuccess(): void From 807a3fac40f36b84003729425ef5f5ca51c01095 Mon Sep 17 00:00:00 2001 From: krislankford Date: Tue, 13 Sep 2016 07:10:46 -0700 Subject: [PATCH 03/13] Update README.md --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5470d2d..7affaa3 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,19 @@ -# nativescipt-mqtt - NativeScipt MQTT 3.1.1 Module +# NativeScript MQTT 3.1.1 Module This nativescript-mqtt module is a cross-platofrm javascript implementation leveraging native socket support and the open source [MQTT 3.1.1 PAHO library link](http://www.eclipse.org/paho). Currently the library only supports the websocket protocol for cross-platform on port 80 or 443 for SSL. ## Usage Sample ### Connection Options host: string + port: int - default 80 | useSSL 443 + path: string - default empty + useSSL: bool - default false + clientId: string - default UUID + retryOnDisconnect: bool - default false ### Connection Events From 50fd0c72d8206b59d8ce43b061c63a38cb90d4e0 Mon Sep 17 00:00:00 2001 From: krislankford Date: Tue, 13 Sep 2016 07:11:30 -0700 Subject: [PATCH 04/13] Update README.md --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index 7affaa3..02f1f90 100644 --- a/README.md +++ b/README.md @@ -7,13 +7,9 @@ This nativescript-mqtt module is a cross-platofrm javascript implementation leve host: string port: int - default 80 | useSSL 443 - path: string - default empty - useSSL: bool - default false - clientId: string - default UUID - retryOnDisconnect: bool - default false ### Connection Events From d27cf0bebeff081b5fafdc05c9c7dca2849129ca Mon Sep 17 00:00:00 2001 From: krislankford Date: Wed, 21 Sep 2016 07:03:50 -0700 Subject: [PATCH 05/13] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 02f1f90..02c116c 100644 --- a/README.md +++ b/README.md @@ -4,14 +4,14 @@ This nativescript-mqtt module is a cross-platofrm javascript implementation leve ## Usage Sample ### Connection Options +```text host: string - port: int - default 80 | useSSL 443 path: string - default empty useSSL: bool - default false clientId: string - default UUID retryOnDisconnect: bool - default false - +``` ### Connection Events ConnectionSuccess(): void ConnectionFailure(): string From 87c38fc34c64af94d44a4288f266a3b6f0a34759 Mon Sep 17 00:00:00 2001 From: krislankford Date: Wed, 21 Sep 2016 07:04:29 -0700 Subject: [PATCH 06/13] Update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 02c116c..4311e7c 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,12 @@ clientId: string - default UUID retryOnDisconnect: bool - default false ``` ### Connection Events +```text ConnectionSuccess(): void ConnectionFailure(): string ConnectionLost(): string MessageArrived(): Message - +``` ### Message Object ```javascript Message { From 8fb43ac099e422ea04a31236d08925f6736cf6e9 Mon Sep 17 00:00:00 2001 From: Dan Bunker Date: Thu, 6 Apr 2017 14:13:26 -0600 Subject: [PATCH 07/13] Update index.ts the error returned has no property "message", but rather "errorMessage" --- index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.ts b/index.ts index ca74fe2..4ec7ee4 100644 --- a/index.ts +++ b/index.ts @@ -59,7 +59,7 @@ class MQTTClient { this.connected = true; }, onFailure: (err: any) => { - this.connectionFailure.trigger(err.message); + this.connectionFailure.trigger(err.errorMessage); } } From 2daa9872aa3692d6796a3eccb9dcd5d1698343c8 Mon Sep 17 00:00:00 2001 From: Dan Bunker Date: Fri, 7 Apr 2017 13:13:14 -0600 Subject: [PATCH 08/13] Add nativescript-websockets as a dependency --- .gitignore | 2 ++ package.json | 3 +++ 2 files changed, 5 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a56a7ef --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules + diff --git a/package.json b/package.json index 2f4d581..e53b85e 100644 --- a/package.json +++ b/package.json @@ -18,5 +18,8 @@ }, "scripts": { "build": "tsc" + }, + "dependencies": { + "nativescript-websockets": "^1.3.3" } } From ba188e0d661fb24a625c1522b5ddf9890f2c8de5 Mon Sep 17 00:00:00 2001 From: Dan Bunker Date: Fri, 7 Apr 2017 19:47:56 -0600 Subject: [PATCH 09/13] Add ClientOptions interface and typings file --- .gitignore | 1 + index.d.ts | 9 +++++++++ index.ts | 31 ++++++++++++++++++++----------- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index a56a7ef..02db887 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules +.idea diff --git a/index.d.ts b/index.d.ts index 99d7c6a..5b5173c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -29,3 +29,12 @@ declare class MQTTClient { onMessageArrived(): IEvent; connected: boolean; } + +declare interface ClientOptions { + host? : string, + port? : number, + useSSL? : boolean, + path? : string, + clientId? : string, + retryOnDisconnect? : boolean +} diff --git a/index.ts b/index.ts index ca74fe2..c7f763c 100644 --- a/index.ts +++ b/index.ts @@ -1,6 +1,15 @@ import { IEvent, EventHandler, guid, Message } from './common'; let MQTT = require('./mqttws31'); +interface ClientOptions { + host? : string, + port? : number, + useSSL? : boolean, + path? : string, + clientId? : string, + retryOnDisconnect? : boolean +} + class MQTTClient { private mqttClient; private host: string; @@ -15,7 +24,7 @@ class MQTTClient { private connectionLost = new EventHandler(); private messageArrived = new EventHandler(); - constructor(options:any){ + constructor(options:ClientOptions){ /* options host: string port: int - default 80 | useSSL 443 @@ -48,7 +57,7 @@ class MQTTClient { public connect(username, password){ if(this.connected){ return; - }; + } let connectOptions = { userName: username, @@ -59,18 +68,18 @@ class MQTTClient { this.connected = true; }, onFailure: (err: any) => { - this.connectionFailure.trigger(err.message); + this.connectionFailure.trigger(err.errorMessage); } - } + }; this.mqttClient.onConnectionLost = (err) => { - this.connectionLost.trigger(err.errorMessage); - this.connected = false; - } + this.connectionLost.trigger(err.errorMessage); + this.connected = false; + }; - this.mqttClient.onMessageArrived = (message:any) => { - this.messageArrived.trigger(new Message(message)); - } + this.mqttClient.onMessageArrived = (message: any) => { + this.messageArrived.trigger(new Message(message)); + }; this.mqttClient.connect(connectOptions); } @@ -89,4 +98,4 @@ class MQTTClient { } -export { MQTTClient } +export { MQTTClient, ClientOptions } From 90581a63b8ea4b8313d1e1b3aa3ba5b4c8f71b5c Mon Sep 17 00:00:00 2001 From: Dan Bunker Date: Fri, 7 Apr 2017 20:38:20 -0600 Subject: [PATCH 10/13] Update README.md Add some code examples. --- README.md | 76 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 4311e7c..59527a8 100644 --- a/README.md +++ b/README.md @@ -3,21 +3,64 @@ This nativescript-mqtt module is a cross-platofrm javascript implementation leveraging native socket support and the open source [MQTT 3.1.1 PAHO library link](http://www.eclipse.org/paho). Currently the library only supports the websocket protocol for cross-platform on port 80 or 443 for SSL. ## Usage Sample -### Connection Options -```text -host: string -port: int - default 80 | useSSL 443 -path: string - default empty -useSSL: bool - default false -clientId: string - default UUID -retryOnDisconnect: bool - default false -``` -### Connection Events -```text -ConnectionSuccess(): void -ConnectionFailure(): string -ConnectionLost(): string -MessageArrived(): Message +### Create an MQTT Client +``` +import {MQTTClient, ClientOptions} from "nativescript-mqtt"; +... +mqtt_host : string = "broker.mqttdashboard.com"; +mqtt_port : number = 8000; +mqtt_useSSL : boolean = false; + +mqtt_clientOptions: ClientOptions = { + host: this.mqtt_host, + port: this.mqtt_port, + useSSL: this.mqtt_useSSL +}; + +mqtt_client : MQTTClient = new MQTTClient(this.mqtt_clientOptions); +``` +### Setup Handlers +``` +... +mqtt_topic : string = "sample-topic" + +constructor() { + this.setupHandlers(); +} + +setupHandlers() : void { + this.mqtt_client.onConnectionFailure.on((err : any) => { + console.log("Connection failed: " + JSON.stringify(err)); + }); + this.mqtt_client.onConnectionSuccess.on(() => { + console.log("Connected successfully!"); + this.subscribe(); + }); + this.mqtt_client.onConnectionLost.on((err : any) => { + console.log("Connection lost: " + JSON.stringify(err)); + }); + this.mqtt_client.onMessageArrived.on((message: Message) => { + console.log("Message received: " + message.payload); + }); +} + +subscribe() : void { + this.mqtt_client.subscribe(this.mqtt_topic); +} +``` + +### Connect +``` +mqtt_username : string = ""; +mqtt_password : string = ""; + +connect() : void { + this.mqtt_client.connect(this.mqtt_username, this.mqtt_password); +} +``` +And in the template: +``` + ``` ### Message Object ```javascript @@ -28,6 +71,3 @@ Message { qos: number } ``` -### Sample Code - -### Callouts From 51ec18d499286cff3f2974cd6a5a0b5aa0854ca0 Mon Sep 17 00:00:00 2001 From: Dan Bunker Date: Fri, 7 Apr 2017 20:51:37 -0600 Subject: [PATCH 11/13] Update README.md --- README.md | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 83 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 59527a8..0802dd8 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ mqtt_client : MQTTClient = new MQTTClient(this.mqtt_clientOptions); ### Setup Handlers ``` ... -mqtt_topic : string = "sample-topic" +mqtt_topic : string = "sample-topic"; constructor() { this.setupHandlers(); @@ -71,3 +71,85 @@ Message { qos: number } ``` +### A full Angular 2/4 Component and Template Sample +#### app.component.ts +``` +import {Component} from "@angular/core"; +import {MQTTClient, ClientOptions} from "nativescript-mqtt"; +import {Message} from "nativescript-mqtt/common"; + +@Component({ + selector: "ns-app", + templateUrl: "app.component.html", +}) +export class AppComponent { + mqtt_host: string = "broker.mqttdashboard.com"; + mqtt_port: number = 8000; + mqtt_useSSL: boolean = false; + mqtt_path: string = "/mqtt"; + mqtt_username: string = ""; + mqtt_password: string = ""; + mqtt_topic: string = "ninja-topic"; + + mqtt_clientOptions: ClientOptions = { + host: this.mqtt_host, + port: this.mqtt_port, + useSSL: this.mqtt_useSSL, + path: this.mqtt_path + }; + + mqtt_client: MQTTClient = new MQTTClient(this.mqtt_clientOptions); + + loading : boolean = false; + + constructor() { + this.setupHandlers(); + } + + connect() : void { + try{ + this.mqtt_client.connect(this.mqtt_username, this.mqtt_password); + } + catch (e) { + console.log("Caught error: " + e); + } + } + + subscribe() : void { + try { + this.mqtt_client.subscribe(this.mqtt_topic); + } + catch (e) { + console.log("Caught error: " + e); + } + } + + setupHandlers() : void { + this.mqtt_client.onConnectionFailure.on((err) => { + console.log("Connection failed: " + err); + }); + + this.mqtt_client.onConnectionSuccess.on(() => { + console.log("Connected successfully!"); + this.subscribe(); + }); + + this.mqtt_client.onConnectionLost.on((err) => { + console.log("Connection lost: " + err); + }); + + this.mqtt_client.onMessageArrived.on((message: Message) => { + console.log("Message received: " + message.payload); + }); + } +} +``` +#### app.component.html +``` + + + + + + +``` From 9b2e659c72eb2fafc8932d5aad2c1271194f5623 Mon Sep 17 00:00:00 2001 From: JScearcy Date: Tue, 24 Oct 2017 12:08:52 -0500 Subject: [PATCH 12/13] #8 create MQTT.Message using current implementation of Message, add retained flag to current Message (optional) --- common.js | 2 ++ common.js.map | 2 +- common.ts | 12 +++++++++++- index.d.ts | 1 + index.js | 13 +++++++++---- index.js.map | 2 +- index.ts | 8 +++++++- 7 files changed, 32 insertions(+), 8 deletions(-) diff --git a/common.js b/common.js index 3585421..239bc74 100644 --- a/common.js +++ b/common.js @@ -1,4 +1,5 @@ "use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); var EventHandler = (function () { function EventHandler() { this.handlers = []; @@ -21,6 +22,7 @@ var Message = (function () { this.bytes = mqttMessage.payloadBytes || null; this.topic = mqttMessage.destinationName || ''; this.qos = mqttMessage.qos || 0; + this.retained = mqttMessage.retained || false; } return Message; }()); diff --git a/common.js.map b/common.js.map index d6a905d..7b7b09d 100644 --- a/common.js.map +++ b/common.js.map @@ -1 +1 @@ -{"version":3,"file":"common.js","sourceRoot":"","sources":["common.ts"],"names":[],"mappings":";AAKA;IAAA;QACY,aAAQ,GAA4B,EAAE,CAAC;IAanD,CAAC;IAXU,yBAAE,GAAT,UAAU,OAA6B;QACnC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAChC,CAAC;IAEM,0BAAG,GAAV,UAAW,OAA6B;QACpC,IAAI,CAAC,QAAQ,GAAG,IAAI,CA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AAC;IAClB,CAAC;IACD,MAAM,CAAC,EAAE,EAAE,GAAG,EAAE,EAAE,GAAG,GAAG,GAAG,EAAE,EAAE,GAAG,GAAG,GAAG,EAAE,EAAE,GAAG,GAAG;QAChD,EAAE,EAAE,GAAG,GAAG,GAAG,EAAE,EAAE,GAAG,EAAE,EAAE,GAAG,EAAE,EAAE,CAAC;AACpC,CAAC,CAAA;AAE8B,oBAAI"} \ No newline at end of file diff --git a/common.ts b/common.ts index d37db1b..14f2d54 100644 --- a/common.ts +++ b/common.ts @@ -24,11 +24,21 @@ class Message { public bytes: ArrayBuffer; public topic: string; public qos: number; - constructor(mqttMessage: any){ + public retained: boolean; + constructor( + mqttMessage: { + payloadString?: string, + payloadBytes?: ArrayBuffer, + destinationName?: string, + qos?: number, + retained?: boolean; + } + ){ this.payload = mqttMessage.payloadString || ''; this.bytes = mqttMessage.payloadBytes || null; this.topic = mqttMessage.destinationName || ''; this.qos = mqttMessage.qos || 0; + this.retained = mqttMessage.retained || false; } } diff --git a/index.d.ts b/index.d.ts index 5b5173c..59dbb75 100644 --- a/index.d.ts +++ b/index.d.ts @@ -18,6 +18,7 @@ declare class Message { bytes: ArrayBuffer; topic: string; qos: number; + retained: boolean; } declare class MQTTClient { diff --git a/index.js b/index.js index ca9bd82..7d4682a 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ "use strict"; -var common_1 = require('./common'); +Object.defineProperty(exports, "__esModule", { value: true }); +var common_1 = require("./common"); var MQTT = require('./mqttws31'); var MQTTClient = (function () { function MQTTClient(options) { @@ -46,7 +47,6 @@ var MQTTClient = (function () { if (this.connected) { return; } - ; var connectOptions = { userName: username, password: password, @@ -56,7 +56,7 @@ var MQTTClient = (function () { _this.connected = true; }, onFailure: function (err) { - _this.connectionFailure.trigger(err.message); + _this.connectionFailure.trigger(err.errorMessage); } }; this.mqttClient.onConnectionLost = function (err) { @@ -75,7 +75,12 @@ var MQTTClient = (function () { this.mqttClient.unsubscribe(topic); }; MQTTClient.prototype.publish = function (message) { - this.mqttClient.send(message); + var mqttMessage = message.bytes !== null ? + new MQTT.Message(message.bytes) : new MQTT.Message(message.payload); + mqttMessage.destinationName = message.topic; + mqttMessage.retained = message.retained; + mqttMessage.qos = message.qos; + this.mqttClient.send(mqttMessage); }; return MQTTClient; }()); diff --git a/index.js.map b/index.js.map index 945fb5c..f3b9cb6 100644 --- a/index.js.map +++ b/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";AAAA,uBAAoD,UAAU,CAAC,CAAA;AAC/D,IAAI,IAAI,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;AAEjC;IAcE,oBAAY,OAAW;QALf,sBAAiB,GAAG,IAAI,qBAAY,EAAQ,CAAC;QAC7C,sBAAiB,GAAG,IAAI,qBAAY,EAAU,CAAC;QAC/C,mBAAc,GAAG,IAAI,qBAAY,EAAU,CAAC;QAC5C,mBAAc,GAAG,IAAI,qBAAY,EAAW,CAAC;QAWnD,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;QACvB,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,WAAW,CAAC;QACxC,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,KAAK,CAAC;QACtC,EAAE,CAAA,CAAC,OAAO,CAAC,IAAI,CAAC;YAAC,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;QAC1C,IAAI;YAAC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,GAAC,GAAG,GAAC,EAAE,CAAC;QACpC,IAAI,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,EAAE,CAAC;QAC/B,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,IAAI,aAAI,EAAE,CAAC;QAC3C,IAAI,CAAC,iBAAiB,GAAG,OAAO,CAAC,iBAAiB,IAAI,KAAK,CAAC;QAG5D,IAAI,CAAC,UAAU,G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;QAC5C,WAAW,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QACxC,WAAW,CAAC,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC;QAE9B,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IACpC,CAAC;IAEH,iBAAC;AAAD,CAAC,AA5FD,IA4FC;AAEQ,gCAAU"} \ No newline at end of file diff --git a/index.ts b/index.ts index c7f763c..340f5df 100644 --- a/index.ts +++ b/index.ts @@ -93,7 +93,13 @@ class MQTTClient { } public publish(message: Message){ - this.mqttClient.send(message); + const mqttMessage = message.bytes !== null ? + new MQTT.Message(message.bytes) : new MQTT.Message(message.payload); + mqttMessage.destinationName = message.topic; + mqttMessage.retained = message.retained; + mqttMessage.qos = message.qos; + + this.mqttClient.send(mqttMessage); } } From 66d699604fbd64d63aa0520fb5550b22d7b00fc6 Mon Sep 17 00:00:00 2001 From: uniquejava <345343747@qq.com> Date: Sat, 28 Jul 2018 23:44:51 +0800 Subject: [PATCH 13/13] expose disconnect method. --- index.js | 5 ++++- index.ts | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 7d4682a..7c16d2e 100644 --- a/index.js +++ b/index.js @@ -68,6 +68,9 @@ var MQTTClient = (function () { }; this.mqttClient.connect(connectOptions); }; + MQTTClient.prototype.disconnect = function () { + this.mqttClient.disconnect(); + }; MQTTClient.prototype.subscribe = function (topic) { this.mqttClient.subscribe(topic); }; @@ -85,4 +88,4 @@ var MQTTClient = (function () { return MQTTClient; }()); exports.MQTTClient = MQTTClient; -//# sourceMappingURL=index.js.map \ No newline at end of file +//# sourceMappingURL=index.js.map diff --git a/index.ts b/index.ts index 340f5df..464ff67 100644 --- a/index.ts +++ b/index.ts @@ -84,6 +84,10 @@ class MQTTClient { this.mqttClient.connect(connectOptions); } + public disconnect(){ + this.mqttClient.disconnect(); + } + public subscribe(topic:string){ this.mqttClient.subscribe(topic); }