Skip to content

Commit 7466819

Browse files
redboltzYoDaMa
andauthored
fix(resubscribe): message id allocate twice (mqttjs#1337)
* fix: messageeId * Fix messageId allocate twice on deliver. resubscribe is out of MQTT spec. It is MQTT.js expansion. On connect sequence, the following three steps are defined by the MQTT Spec. 1. The client sends CONNECT to the broker with CleanStart:false 2. The broker sends CONNACK to the client with SessionPresent:true if session exists 3. The client re-sends in-flight PUBLISH and PUBREL messages resubscribe was processed between the step 2 and step 3. It's too early. The resubscribe might allocate messageId that is the same as PUBLISH or PUBREL packet. It is not good. So I moved resubscribe process to after the step 3. * Removed invalid fallback code. * Stored CONNACK packet instead of sessionPresent. Co-authored-by: Yoseph Maguire <yoseph.maguire@gmail.com>
1 parent e3e15c3 commit 7466819

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

lib/client.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ function MqttClient (streamBuilder, options) {
335335
var packet = null
336336

337337
if (!entry) {
338+
that._resubscribe()
338339
return
339340
}
340341

@@ -343,10 +344,7 @@ function MqttClient (streamBuilder, options) {
343344
var send = true
344345
if (packet.messageId && packet.messageId !== 0) {
345346
if (!that.messageIdProvider.register(packet.messageId)) {
346-
packet.messageeId = that.messageIdProvider.allocate()
347-
if (packet.messageId === null) {
348-
send = false
349-
}
347+
send = false
350348
}
351349
}
352350
if (send) {
@@ -360,7 +358,7 @@ function MqttClient (streamBuilder, options) {
360358
}
361359
)
362360
} else {
363-
debug('messageId: %d has already used.', packet.messageId)
361+
debug('messageId: %d has already used. The message is skipped and removed.', packet.messageId)
364362
deliver()
365363
}
366364
}
@@ -1674,11 +1672,11 @@ MqttClient.prototype.getLastMessageId = function () {
16741672
* _resubscribe
16751673
* @api private
16761674
*/
1677-
MqttClient.prototype._resubscribe = function (connack) {
1675+
MqttClient.prototype._resubscribe = function () {
16781676
debug('_resubscribe')
16791677
var _resubscribeTopicsKeys = Object.keys(this._resubscribeTopics)
16801678
if (!this._firstConnection &&
1681-
(this.options.clean || (this.options.protocolVersion === 5 && !connack.sessionPresent)) &&
1679+
(this.options.clean || (this.options.protocolVersion === 5 && !this.connackPacket.sessionPresent)) &&
16821680
_resubscribeTopicsKeys.length > 0) {
16831681
if (this.options.resubscribe) {
16841682
if (this.options.protocolVersion === 5) {
@@ -1714,9 +1712,9 @@ MqttClient.prototype._onConnect = function (packet) {
17141712

17151713
var that = this
17161714

1715+
this.connackPacket = packet
17171716
this.messageIdProvider.clear()
17181717
this._setupPingTimer()
1719-
this._resubscribe(packet)
17201718

17211719
this.connected = true
17221720

0 commit comments

Comments
 (0)