Retained Messages in MQTT

Spread the love

Retained messages are one of the unique features of MQTT. You won’t find in a lot of other protocols.

What is Retained Messages?

A retained messages is a last known good value persisted by the broker for a topic. So, this means the last message which was sent by any client on a specific topic can be persisted on a broker so new subscribers subscribing to this topic would get the most the freshest value available on the broker and they don’t need to start with an empty value.

This is especially useful, if you have an application which requires a start state. So, you cannot wait a seconds or even minutes until you get a fresh value pushed by the broker in case anybody produces data but you need the last known good value.

How does this work?

A client publishes a MQTT publish packet to the broker and sets the retained flag to true this signalizes to a broker that it needs to persist the message on the broker and any new subscriber who subscribed to the broker on this specific topic would get the value immediately even if they are not online at the point of time the message was published.

So, it’s a very great way to solve the start value problem where you need some some values to start.

When to use retained messages?

Retained messages are used on a topic level. So, retained messages don’t care about client contacts at all. Every subscription whose who’s new will get a retained message delivered directly.

Retained messages have a global context on the whole broker and only the last message per topic is persisted here. So, new subscribers get the freshest value.

When to use queued messages?

Queued message is always on a per client context. So, if you have a persistent session for a client and this client is offline the broker will queue all messages that this specific client lost.

How do i delete a retained message persisted on a broker for this topic?

Normally, you don’t need to do this, if you have a topic which gives gets frequent updates then a new retail message will overwrite the old one.

If you really need to delete it you can always send a retained message with an empty payload to a specific topic and then the retained message is deleted.

Also Read: Beginner’s Guide To Understanding The Internet of Things

Spread the love

Be the first to comment

Leave a Reply

Your email address will not be published.