binarybucks / homA

An extensible framework for the smart home
Eclipse Public License 1.0
137 stars 24 forks source link

deprecated mqtt.createClient function #142

Open hmueller01 opened 7 years ago

hmueller01 commented 7 years ago

I am new to homA and just installed this and node.js on a RPi running jessie. After npm install and running demo I got an error message: self.mqttClient = mqtt.createClient(port || params.brokerPort, host || params.brokerHost, extraParameters); TypeError: mqtt.createClient is not a function Google helped:

createClient() has been deprecated by mqtt module, use connect() instead.

Installed versions:

> npm version
{ npm: '3.10.10',
  ares: '1.10.1-DEV',
  http_parser: '2.7.0',
  icu: '58.2',
  modules: '48',
  node: '6.10.0',
  openssl: '1.0.2k',
  uv: '1.9.1',
  v8: '5.1.281.93',
  zlib: '1.2.8' }

> node_modules/mqtt/mqtt.js version
MQTT.js version: 2.4.0

So, I patched homa.js like this:

--- ./misc/libraries/node/homa/homa.js  2017-02-22 19:46:33.878485086 +0100
+++ ./components/demo/node_modules/homa/homa.js 2017-02-22 21:55:41.019057301 +0100
@@ -2,6 +2,7 @@
 // 2013 Alexander Rust <mail@alr.st>
 // Use it as you like if you find id usefull

+const url = require('url');
 var util = require('util');
 var events = require('events');
 var mqtt = require('mqtt');

@@ -107,7 +108,10 @@
    self.scheduledPublishes = [];

    this.connect = function(host, port, extraParameters) {
-       self.mqttClient = mqtt.createClient(port || params.brokerPort, host || params.brokerHost, extraParameters);
+       const brokerUrl = url.parse(host || params.brokerHost);
+       brokerUrl.port = port || params.brokerPort;
+       self.mqttClient = mqtt.connect(brokerUrl);
        log.info("MQTT", "Connecting to %s:%s", host || params.brokerHost, port || params.brokerPort);

      self.mqttClient.on('connect', function() {

I do not know what extraParameters is for and how it should be used in url object context. Right now it works so far ...

Holger

hmueller01 commented 5 years ago

I got news. extraParameters is needed, if you like to pass a username and password. I changed the connect call to mqtt.connect(brokerUrl, extraParameters) The homa connect call looks like this:

var options = {
  username: "xxx",
  password: "xxx"
};
homa.mqttHelper.connect(null, null, options);

I update the code. Where do we need to upload homa.js that npm install downloads this new version?

hmueller01 commented 5 years ago

@binarybucks Can you please upload the updated homa.js. Thanks. Holger