MQTT.js
MQTT.js copied to clipboard
Behavior of "resubscribe" option on manual "reconnect()"
Hi, i stumbeled over a unexptected behavior. Can you please clearify whether this is exptecter or not. Thanx in advance.
I user mqtt to establish a TLS WS connection to a mosquitto broker. I do an manual reconnect and expected all the subscriptions are carried over into the new connection.
A minimal viable example can be found here : https://jsfiddle.net/3c8jzv96/7/ After the reconnect no more messages are received.
Alex
AB#8867200
I went to track this down, and maybe find a way to call _resubscribe to get around this bug and have it resubscribe your topics...
But the problem is much much deeper.
I would go so far as to say that 'reconnect()' is completely broken and should not be used at all. It causes the state of the client object to be extremely weird.... for example if you add to your code:
client.on('connect',function()
{
console.log(client.disconnecting);
console.log(client.disconnected);
});
You will see that even after the reconnect, the client still thinks it is disconnecting/disconnected, and this breaks lots of things including the ability to re-subscribe.
My guess is that the author intended reconnect to be called when there is a already finished a clean disconnection of some kind.
To 'fix' your code this works:
if (messageCount == 3) {
client.end();
client.reconnect();
}
Basically I don't think it is ever safe to call reconnect() without end()
Hi sectokia,
thx for having a look! Currently i do exaclty what you suggested. I track all my subscriptions and resubscribe after "reconnect" (ending the current connection and reestablish a fresh one)
I recommend switching to the Eclipse Paho JavaScript Client for browser side client work.
mqtt.js for browser has way too many bugs, and for some absurd reason is an order of magnitude bigger and slower ( mqtt.is for browser is over 16,000 lines?! includes node.js stuff that only runs on server??)
Dug into the 'client.js' code, it turned out that the topic is not added to the reconnection object only by the resubscribe variable set to 'true'. It turns out that you also need to set the reconnectionPeriod > 0. In theory, if you properly adjust the reconnection mechanism, as another workaround, you can set the reconnection period to 2147483647, which corresponds to the maximum positive value of a 32-bit number.
This is an automated message to let you know that this issue has gone 365 days without any activity. In order to ensure that we work on issues that still matter, this issue will be closed in 14 days.
If this issue is still important, you can simply comment with a "bump" to keep it open.
Thank you for your contribution.
This issue was automatically closed due to inactivity.