homA icon indicating copy to clipboard operation
homA copied to clipboard

deprecated mqtt.createClient function

Open hmueller01 opened this issue 8 years ago • 2 comments

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 <[email protected]>
 // 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 avatar Feb 22 '17 21:02 hmueller01

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 avatar Oct 27 '18 20:10 hmueller01

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

hmueller01 avatar Dec 03 '18 09:12 hmueller01