Rudy Kurniawan
Rudy Kurniawan

Reputation: 2428

Is there any way to differentiate between new and retained MQTT message?

Like the title, can I differentiate between new and retained MQTT message? And how to know when the message are published by client? All of this without specify timestamp string in topic or message.

Upvotes: 3

Views: 1574

Answers (2)

ralight
ralight

Reputation: 11628

The retained flag is used by a client to indicate to the broker that the broker should keep the message as the "last known good" value for the topic being published to.

When the broker talks to a client, the retained flag has a different meaning: If the flag is set on a message, it means that the message is one that was published as a retained message before the client subscribed to the topic. In other words it is a "stale" message. When the broker sends a message that does not have the retained flag set, it means that the message has just been published by another client and can be considered to be fresh/new.

As @hardillb says, there is no way of determining whether a fresh message you receive was designated as a retained message by the publishing client.

Upvotes: 5

hardillb
hardillb

Reputation: 59816

There is a flag in the MQTT publish packet header the indicates a message is retained.

But assuming you are trying to spot messages delivered twice this won't help as a publisher could publish a new retained message just as a subscriber reconnects and it would be delivered for the first time with the retained bit set.

As for a message timestamp, no, if you want one you will have to include it in the payload.

Upvotes: 2

Related Questions