MQTT.js icon indicating copy to clipboard operation
MQTT.js copied to clipboard

Client does not resubscribe after reconnect if clean = false

Open holopekochan opened this issue 6 years ago • 2 comments

Is there any reason we don't want resubscribe work if clean = false? I have created a sub client with clean = false, and resubscribe = true, the client will not resubscribe the topics if MQTT aedes broker (no persistence) restart. And, sub-client will no longer receive the message of the topics https://github.com/mcollina/aedes

https://github.com/mqttjs/MQTT.js/blob/master/lib/client.js From MQTT.js/lib/client.js

 MqttClient.prototype._resubscribe = function () {
  if (!this._firstConnection &&
      this.options.clean &&
      Object.keys(this._resubscribeTopics).length > 0) {
    if (this.options.resubscribe) {
      this._resubscribeTopics.resubscribe = true
      this.subscribe(this._resubscribeTopics)
    } else {
      this._resubscribeTopics = {}
    }
  }

I can provide my sub client code and broker code if necessary

holopekochan avatar Dec 03 '18 05:12 holopekochan

@https://github.com/mqttjs/MQTT.js/issues/749

zz123er avatar Aug 20 '19 03:08 zz123er

I just ran into this myself with MQTT.js 4.2.8 and using Mosquitto 2.0.12 as the broker, and was extremely surprised by the behaviour. Was there a specific reason for it behaving like this, or is it some unintended bug?

VirtualWolf avatar Oct 03 '21 01:10 VirtualWolf

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.

github-actions[bot] avatar Oct 11 '22 02:10 github-actions[bot]

bump

Hypfer avatar Oct 11 '22 07:10 Hypfer

The sessionPresentFlag should be available since version 3.1.1. And it is possible for broker to lose the persistent session, in that case you would want the client to automatically resubscribe.

Thus this line: https://github.com/mqttjs/MQTT.js/blob/18bdd491da7921ede5b90f5ed4bbf2dc46a304ae/src/lib/client.ts#L2108-L2110

Should be:

(this.options.clean || (this.options.protocolVersion >= 4 && !this.connackPacket.sessionPresent)) &&

Right? @robertsLando

This problem seem to be observed in multiple issues #749, #895, #1288

I can make a pull request for this. Not sure if this would be considered a breaking change, But I think most people woud expect resubscribe to work like this.

simonnilsson avatar Jul 26 '23 14:07 simonnilsson

Yeah that's a bug. Open a PR if possible, remember to add a test. Thanks

robertsLando avatar Jul 26 '23 15:07 robertsLando