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

Read retained-only

Open reneklootwijk opened this issue 5 years ago • 4 comments

The mosquitto_sub client has a --retained-only option to only read the retained messages. How can this be implemented using MQTT.js? How do you know, after subscribing, that all retained messages are received and you can unsubscribe? My use case is a publisher that can determine the last state of the topics, managed by that same publisher, at startup time. This would enable to start with the last values also known to all subscribers instead of starting with default values.

AB#8651721

reneklootwijk avatar Oct 23 '20 10:10 reneklootwijk

@reneklootwijk can you explain more on what the option does? There could be a feature added but I'm not sure if a retained option is available currently in MQTT.js.

Are you suggesting that on disconnect, there's no state kept on what topics the client you're using is subscribed to? Could you give a more detailed explanation?

YoDaMa avatar Oct 26 '20 23:10 YoDaMa

That mosquitto_sub option only prints the messages that have the retained flag set. It could still be receiving non-retained messages, but it is choosing not to print them.

In MQTT.js, I think you can find the retained flag on the packet argument (3rd argument) for message events.

The MQTT protocol doesn't have a method for only receiving retained messages and there is no way to know you received all of them. Since you don't receive messages (retained or otherwise) unless you subscribe to a topic, you could count how many topics you subscribe to and how many messages you receive. Obviously won't work if the topic pattern contains a wildcard so you will probably have to fallback to timing out. Most brokers should send retained messages very quickly after subscribing.

jdiamond avatar Oct 27 '20 18:10 jdiamond

The mosquitto_sub command with the --retained-only parameter captures the messages with the retained flag set and then disconnects and exits.

My use case is a publisher of sensor readings. Normally the publisher only publishes readings that differ from the previous one, so it does not publish duplicate values. However, when you restart the publisher it cannot determine whether a reading is different than the last published value. I could of course persist the last published values in a configuration file or so, but it would be nice when the publisher can determine the last published values by looking at the retained messages (I publish readings with the retained flag set).

I now solve it by subscribing to a 'parent' topic where the sensor values are published, capture the messages with the retain flag set and unsubscribe after 1s.

The sensor values are published using the following topic structure: events//properties/

To capture the last published values for the properties of the sensor I subscribe to: events//properties/#

reneklootwijk avatar Oct 27 '20 21:10 reneklootwijk

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 Sep 21 '22 02:09 github-actions[bot]

This issue was automatically closed due to inactivity.

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