mqttaedes

mqtt js get channel value at subscribe


I am using an Aedes as the broker and MQTT.js as a client in a web application.

At startup I connect to the broker and subscribe to some channels.

I would like to know if there's a way to get the last previous messages in a MQTT way.


Solution

  • There are 2 different things here.

    1. Retained messages. This is a flag set on the message by the publisher, it tells the broker to hold on to the message and always deliver the last retained message on any given topic to a client when they first subscribe to that topic before any new messages are sent. The last retained message will always be replaced by the next message published on that topic that also has the retained flag set. You can read more about retained messages here

    2. Queued messages. If the client subscribes to a topic at greater than QOS 0 then the broker will queue up messages sent for that client while it is offline. The client needs to reconnect using the same client ID and with the cleanSession flag set to false. You can read more about persistent sessions and queued messages here