MQTT.js
MQTT.js copied to clipboard
Topic unpopulated once client reconnects
I have a client, using version 4.2.5, with a subscription and once the client reconnects, the topic
property is no longer populated when a message is received. Inspecting the packet of the message
event shows the topic
as empty as well. To recreate the issue, the client just needs to be kicked from the broker it is connected to by either kicking out that specific client or by restarting the broker. Both scenarios result in the topic
not populating.
Code:
client.on("connect", () => {
console.log("Connection Success");
client.subscribe("tmp/#", { qos: 1 }, (err) => {
console.log(err || "Subscribe Success");
});
});
client.on("message", (topic, message, packet) => {
console.log("Received from", topic);
console.log("Received packet", packet);
});
client.on("reconnect", () => {
console.log("Reconnecting");
});
client.on("error", (err) => {
console.log("Error:", err);
});
Log:
Connection Success
Subscribe Success
Received from tmp/hello
Received packet Packet {
cmd: 'publish',
retain: false,
qos: 0,
dup: false,
length: 33,
topic: 'tmp/hello',
payload: <Buffer 7b 0d 0a 20 20 22 68 65 6c 6c 6f 20 77 6f 72 6c 64 22 0d 0a 7d>
}
Received from tmp/hello
Received packet Packet {
cmd: 'publish',
retain: false,
qos: 0,
dup: false,
length: 33,
topic: 'tmp/hello',
payload: <Buffer 7b 0d 0a 20 20 22 68 65 6c 6c 6f 20 77 6f 72 6c 64 22 0d 0a 7d>
}
Reconnecting
Connection Success
Subscribe Success
Received from tmp/hello
Received packet Packet {
cmd: 'publish',
retain: false,
qos: 0,
dup: false,
length: 36,
topic: 'tmp/hello',
payload: <Buffer 7b 0d 0a 20 20 22 68 65 6c 6c 6f 20 77 6f 72 6c 64 22 0d 0a 7d>,
properties: { topicAlias: 1 }
}
Received from
Received packet Packet {
cmd: 'publish',
retain: false,
qos: 0,
dup: false,
length: 27,
topic: '',
payload: <Buffer 7b 0d 0a 20 20 22 68 65 6c 6c 6f 20 77 6f 72 6c 64 22 0d 0a 7d>,
properties: { topicAlias: 1 }
}
AB#8825351
I'm experiencing the same issue with async-mqtt: 2.6.1
which depends on mqtt: 4.1.0
.
I'm guessing this has something to do with the MQTTv5 implementation?
===============================================
EDIT: I've done some further digging and it seems to be a broker issue. After some packet capturing via Wireshark, it seems the topic is missing from the incoming MQTT packet. I'm using the EMQ X broker (4.2.10).
Okay I've finally got to the bottom of this, it seems to be a MQTT.js bug in the reconnect logic.
Some context: MQTTv5 supports a feature called Topic Aliasing where topics can be replaced by a short numeric ID after being sent once to reduce packet size.
During a fresh connect from MQTT.js, the CONNECT
packet's Topic Alias Maximum
property is set to 0
by default which indicates that aliasing is not supported:
However, when MQTT.js reconnects, the CONNECT
packet's Topic Alias Maximum
property is set to 0xFFFF
causing the broker to start using topic aliases for subsequent messages:
So it seems like the topicAliasMaximum
property is actually set when handling the first CONNACK
packet provided by the server, is this expected behavior when users are expected to handle topic de-aliasing manually?
https://github.com/mqttjs/MQTT.js/blob/37b12cb94737acddfe0fa016e013516ba3b9a031/lib/client.js#L1174
I can work around this issue by deleting properties after connect
:
client.on("connect", () => {
client.options.properties = {};
});
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.
Appears to be resolved with version 4.3.7