Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Topic unpopulated once client reconnects #1213

Closed
jkominiakgps opened this issue Nov 17, 2020 · 5 comments
Closed

Topic unpopulated once client reconnects #1213

jkominiakgps opened this issue Nov 17, 2020 · 5 comments

Comments

@jkominiakgps
Copy link

jkominiakgps commented Nov 17, 2020

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

@YoDaMa YoDaMa added the bug label Nov 24, 2020
@akiroz
Copy link
Contributor

akiroz commented May 6, 2021

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).

@akiroz
Copy link
Contributor

akiroz commented May 6, 2021

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:

Screen Shot 2021-05-07 at 00 48 53

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:

Screen Shot 2021-05-07 at 00 41 29

@akiroz
Copy link
Contributor

akiroz commented May 6, 2021

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?

options.properties.topicAliasMaximum = packet.properties.topicAliasMaximum

I can work around this issue by deleting properties after connect:

client.on("connect", () => {
    client.options.properties = {};
});

@github-actions
Copy link

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 github-actions bot added the stale label Sep 21, 2022
@jkominiakgps
Copy link
Author

Appears to be resolved with version 4.3.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants