0
0
IOT Protocolsdevops~6 mins

Retained messages in IOT Protocols - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you want to know the latest weather update as soon as you turn on your device, but you missed the broadcast earlier. Retained messages solve this problem by keeping the last important message ready for anyone who connects later.
Explanation
Purpose of Retained Messages
Retained messages store the last message sent on a topic so new subscribers receive it immediately upon subscribing. This ensures that devices joining late or reconnecting get the current state without waiting for the next update.
Retained messages provide the latest information instantly to new subscribers.
How Retained Messages Work
When a message is published with the retain flag, the broker saves it as the current message for that topic. Any new subscriber to that topic receives this saved message right away, even if it was sent before they subscribed.
The broker keeps and delivers the last retained message to new subscribers automatically.
Updating and Clearing Retained Messages
If a new retained message is published on the same topic, it replaces the old one. To clear a retained message, a publisher sends a message with an empty payload and the retain flag set, telling the broker to remove the stored message.
Retained messages can be updated or removed by publishing new messages with the retain flag.
Use Cases for Retained Messages
Retained messages are useful for status updates, sensor readings, or configuration data that should be available immediately. For example, a smart home device can show the last known temperature as soon as it connects.
They ensure important state information is always available to devices when they connect.
Real World Analogy

Imagine a bulletin board in a community center that always shows the latest weather forecast. Anyone entering the center can see the current forecast immediately without asking around or waiting for someone to announce it.

Purpose of Retained Messages → The bulletin board showing the latest weather so newcomers instantly know the forecast.
How Retained Messages Work → The community center staff updating the bulletin board with the newest forecast.
Updating and Clearing Retained Messages → Replacing the old forecast on the board with a new one or erasing it when no forecast is available.
Use Cases for Retained Messages → People relying on the bulletin board to get important updates like weather or event schedules as soon as they arrive.
Diagram
Diagram
┌───────────────┐       publishes with retain       ┌───────────────┐
│   Publisher   │───────────────────────────────▶│    Broker     │
└───────────────┘                                └───────────────┘
                                                      │
                                                      │ stores last message
                                                      ▼
                                              ┌─────────────────┐
                                              │ Retained Message │
                                              └─────────────────┘
                                                      │
                                                      │ delivers to new
                                                      │ subscribers
                                                      ▼
                                              ┌───────────────┐
                                              │  Subscriber   │
                                              └───────────────┘
This diagram shows how a publisher sends a retained message to the broker, which stores it and delivers it immediately to new subscribers.
Key Facts
Retained messageA message flagged to be stored by the broker and sent immediately to new subscribers.
BrokerThe server that receives, stores, and forwards messages between publishers and subscribers.
Retain flagA marker on a message indicating it should be saved as the last known message for its topic.
Clearing retained messageSending a message with an empty payload and retain flag to remove the stored message.
New subscriberA client that subscribes to a topic and immediately receives the retained message if available.
Common Confusions
Retained messages are sent to all subscribers every time.
Retained messages are sent to all subscribers every time. Retained messages are only sent immediately to new subscribers when they subscribe, not repeatedly to existing subscribers.
Retained messages stay forever without change.
Retained messages stay forever without change. Retained messages can be updated or cleared by publishing new retained messages or empty retained messages.
Summary
Retained messages keep the last message on a topic so new subscribers get the latest information instantly.
The broker stores and delivers retained messages automatically when clients subscribe.
Retained messages can be updated or removed by publishing new messages with the retain flag.