diff --git a/example/example.js b/example/example.js index 3a4cd23..fadffaa 100644 --- a/example/example.js +++ b/example/example.js @@ -9,7 +9,7 @@ let mqtt = new Client({id:'example-mqtt-client', topics:'lights'}) console.log(await mqtt.connect()) await mqtt.send({cmd:'lights/on', status:'sent to topics lights'}) - // await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 explicitly but not listening'}) + await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 explicitly but not listening'}) // await mqtt.subscribe('topic2') // await mqtt.send('topic2', {cmd:'test', status:'sent to topic2 with subscription'}) // now can send via some mqtt client diff --git a/package.json b/package.json index b6ee871..88e0971 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@uci/mqtt", - "version": "0.1.8", + "version": "0.1.9", "description": "mqtt client with json payloads and mqtt custom broker", "main": "src", "scripts": { @@ -26,7 +26,7 @@ "homepage": "https://github.com/uCOMmandIt/uci-changeme#readme", "dependencies": { "@uci-utils/logger": "0.0.13", - "async-mqtt": "^2.2.0", + "async-mqtt": "^1.0", "better-try-catch": "^0.6.2", "is-plain-object": "^2.0.4", "lodash.merge": "^4.6.1", diff --git a/src/client.js b/src/client.js index ed0b62d..4c4fbd1 100644 --- a/src/client.js +++ b/src/client.js @@ -1,4 +1,5 @@ import { connect } from 'async-mqtt' +import { promisify } from 'util' import merge from 'lodash.merge' import btc from 'better-try-catch' import union from 'lodash.union' @@ -28,7 +29,7 @@ class MQTTClient { name: 'mqtt', id: this.id }) - this.url = opts.url || '' + this.url = opts.url || null // subscription topics can be string of commna delimited or array of strings see object see mqtt.js docs this.topics = Array.isArray(opts.topics) ? opts.topics @@ -52,7 +53,10 @@ class MQTTClient { this.url, this.opts ) + // version 2.0 and up messes this up. Need to look at another way + // maybe better not to merge but have mqtt client be a property of class this._extend(mqtt, 'subscribe,unsubscribe') // merge mqtt client into class extend with given functions + this.close = promisify(this.end).bind(this) let timeout = this.opts.connectTimeout || 5000 setTimeout(() => { reject({msg:'ending mqtt connection attempt, no broker', url:this.url, opts:this.opts}) @@ -213,6 +217,7 @@ class MQTTClient { temp[func] = this[func] }) merge(this, obj) + funcs.forEach(func => { this['_' + func] = this[func] this[func] = temp[func]