Raynerv / node-red-contrib-mqttssl

A node-red node for MQTT over SSL/TLS
4 stars 6 forks source link

Can't connect to mosquitto v1.4.2 broker #4

Open teknynja opened 9 years ago

teknynja commented 9 years ago

I am unable to connect to a v1.4.2 broker using this package. Using a simple node.js application with a recent mqtt package (v1.1.5) does connect and works as expected.

I initially thought that this issue may be caused by my usage of self-signed certificates, but later discovered that this is unrelated to the issue. It is also not a authorization issue as discussed in Issue #1.

Symptoms are when running node-red in verbose mode it shows repeated `[mqtt] [88b5cac3.774a38] connected to broker' messages, but no mqtt messages are ever received. On the broker (mosquitto) side, the logs show the following lines repeating for each connection attempt:

New connection from 192.168.0.136 on port 8883.
New client connected from 192.168.0.136 as node-red-playground (c1, k15, u'myuser').
Socket error on client node-red-playground, disconnecting.

This sounds like the problem that @VirTERM was referencing in Issue #1.

Digging into the problem for a few hours reveals that there appears to be a bug in the (extremely) old version (v0.3.x) of mqtt referenced in this project - it sends a 'connect' command before the secure connection is completely up, which causes mosquitto to close the connection. Hand-patching the mqtt library in place to fix the issue allows me to successfully connect to the broker.

Also while looking into this issue, I found what appears to be a bug in your mqttClientPool.js code. Around line 63 there is the line

var connecting = false;

This is causing node-red-contrib-mqttssl to immediately generate two connections in a row when initially connecting. It also then causes the extra connection to repeatedly disconnect and reconnect while node-red is running. Setting this variable to true fixes the issue and only one connection is made and maintained.

For anyone interested in the hand-patching of the old mqtt package, I edited ./node_modules/node-red-contrib-mqttssl/node_modules/mqtt/lib/client.js and commented out the lines:

  // Send a connect packet on stream connect
  this.stream.on('connect', function () {
    that.conn.connect(that.options);
  });

leaving the on secureConnect handler intact. Once that is removed, I am able to connect to mosquitto and receive published messages. After that, fixing the connecting flag issue causes things to work rock-solid.

pierce-jason commented 9 years ago

I can confirm the reconnect issue with Mosquitto v1.4.2. Testing against a free account on cloudmqtt I was able to get it working by just changing the mqttClientPool.js line 63 to true. I did not have to edit the client.js file.

teknynja commented 9 years ago

To be clear, I am actually reporting 2 different issues (as i discovered them both trying to resolve my issue). The reconnect issue was not preventing me from connecting and receiving data from my mosquitto broker, it was simply an issue I noticed. (I also noticed that this issue appears to be present in the node-red mqtt client's source as well).

The problem in client.js (which is actually a part of mqtt itself, and not directly part of the node-red-contrib-mqttssl project) was preventing me from connecting to the mosquitto broker via SSL/TLS. The problem is outside this project, but I'm not sure how big a task it is to move to a newer version of mqtt. I am able to successfully connect to my broker using mqtt V1.1.5, so I assume they have resolved this issue now.

I did try just changing the line in mqttClientPool.js to true without modifying the client.js file, but was still unable to connect to my mosquitto broker.

pierce-jason commented 9 years ago

Oddly enough I had to modify the client.js file on another node-red system to successfully connect to the same broker as I've previously tested with.

gschintgen commented 7 years ago

Hi, is there any update on this issue? I've just started using Node-RED, or at least I tried, and immediately stumbled upon this issue: I can't use the default mqtt node, since it requires certificate-based authentication and I can't use mqttssl because it always fails to connect. I'm constantly getting the following messages:

18 Jun 19:38:58 - [mqtt] [ea753119.64dfc] connected to broker ssl://XXXXXXXXXXX
18 Jun 19:39:00 - [mqtt] [ea753119.64dfc] connected to broker ssl://XXXXXXXXXXX
18 Jun 19:39:01 - [mqtt] [ea753119.64dfc] connected to broker ssl://XXXXXXXXXXX

In mosquitto's log this is what's shown:

1497807617: Socket error on client mqtt_202c3d4c.1eed12, disconnecting.
1497807618: New connection from XXXXX on port 1234.
1497807618: New client connected from XXXXX as mqtt_202c3d4c.1eed12 (c1, k15, u'gilles').
1497807618: Socket error on client mqtt_202c3d4c.1eed12, disconnecting.
1497807619: New connection from XXXXX on port 1234.
1497807619: New client connected from XXXXX as mqtt_202c3d4c.1eed12 (c1, k15, u'gilles').

I also tried changing line 63 of mqttClientPool.js (and restarted nodered), but to no avail. I can't figure out how to send a single mqtt message :-( Any help would be very much appreciated.

(I'm connecting to the broker from various other devices without any problems; I'm not willing to move to unsecure transport.)