0
0
IOT Protocolsdevops~10 mins

Retained messages in IOT Protocols - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Retained messages
Client publishes message with retain flag
Broker stores message as retained
New client subscribes to topic
Broker sends retained message immediately
Client receives last retained message
Client processes message
When a client sends a message with the retain flag, the broker saves it. New subscribers get this saved message right away.
Execution Sample
IOT Protocols
PUBLISH topic='home/temp' payload='22C' retain=true
SUBSCRIBE topic='home/temp'
RECEIVE message='22C' (retained)
Client publishes a retained message; new subscriber immediately receives it.
Process Table
StepActionTopicPayloadRetain FlagBroker StateClient Output
1Client publishes messagehome/temp22CtrueStores '22C' as retained for 'home/temp'None
2New client subscribeshome/tempN/AN/ARetained message existsSends retained message '22C'
3Client receives messagehome/temp22CretainedNo changeReceives '22C' immediately
4Client processes messagehome/temp22CretainedNo changeProcesses '22C'
5No new messagesN/AN/AN/ANo changeNo further output
💡 No new messages published; retained message delivered to subscriber; process ends.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Broker Retained Message for 'home/temp'None'22C''22C''22C''22C'
Client OutputNoneNone'22C' sent'22C' received'22C' processed
Key Moments - 3 Insights
Why does the new subscriber get the message immediately even though it was published before?
Because the broker saved the message as retained (see Step 1 and Step 2 in execution_table), it sends it instantly to new subscribers.
What happens if a message is published without the retain flag?
The broker does not save it, so new subscribers won't get any previous message immediately (not shown here, but inferred from Step 1 retain flag).
Does the retained message get deleted after sending to a new subscriber?
No, the broker keeps the retained message until a new retained message replaces it or it is cleared (see Broker State in variable_tracker).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the broker's retained message after Step 1?
A'22C'
BNone
C'22F'
DEmpty string
💡 Hint
Check the 'Broker State' column at Step 1 in the execution_table.
At which step does the client receive the retained message?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Client Output' column to see when the message is received.
If the retain flag was false in Step 1, what would change in the execution table?
ABroker would store the message as retained
BClient would receive the message immediately on subscription
CBroker would not store the message; new subscriber gets no immediate message
DClient output would be '22C' processed at Step 4
💡 Hint
Refer to the 'Retain Flag' and 'Broker State' columns in Step 1 and Step 2.
Concept Snapshot
Retained messages:
- Client publishes with retain=true
- Broker saves last message per topic
- New subscribers get saved message immediately
- Retained message stays until replaced or cleared
- Useful for last known state delivery
Full Transcript
Retained messages in IoT protocols work by having a client send a message with a retain flag set to true. The broker then saves this message as the last known message for that topic. When a new client subscribes to the topic, the broker immediately sends the saved retained message to the client. This ensures the new subscriber gets the latest state without waiting for a new message. The retained message remains stored until a new retained message replaces it or it is cleared. This mechanism helps devices know the last known value instantly upon subscription.