MQTT retained messages are one of the most powerful yet often misunderstood features of the MQTT protocol. They help ensure that subscribers always receive the most recent data, even if they connect after the message was published. This makes them especially useful in IoT systems where devices may go offline and reconnect frequently. In this guide, we’ll explain what retained messages are, how they work, and when you should (or shouldn’t) use them using simple, real-world examples.
Many industries rely on real-time data accuracy, whether it’s monitoring sensors, system status, or even inventory updates. Similar to how test kits for drugs provide immediate insights without waiting for lab results, retained messages allow MQTT clients to instantly access the latest known value without delay.
What Is an MQTT Retained Message?
A retained message is a special type of MQTT message that the broker stores as the last known good value for a specific topic. When a new client subscribes to that topic, the broker immediately sends this stored message without waiting for a new publish event.
In simple terms:
- Normal MQTT message → delivered only to currently connected subscribers
- Retained MQTT message → stored by the broker and delivered to future subscribers as well
How Retained Messages Work
Here’s the basic flow:
- A publisher sends a message with the retain flag set to true
- The MQTT broker stores this message for that topic
- Any new subscriber to the same topic receives that message instantly
- The retained message stays until it is replaced or cleared
The broker keeps only one retained message per topic always the most recent one.
Simple Example of MQTT Retained Messages
Imagine a smart home system publishing the temperature:
Topic:
home/livingroom/temperature
Payload:
25°C
If this message is published as retained:
- Any device subscribing later will instantly receive 25°C
- No need to wait for the sensor to publish again
This is extremely useful for dashboards, monitoring apps, and control systems.
Retained vs Non-Retained Messages
| Feature | Retained Message | Non-Retained Message |
| Stored by broker | Yes | No |
| Sent to new subscribers | Yes | No |
| Ideal for state data | Yes | No |
| Ideal for event data | No | Yes |
When Should You Use Retained Messages?
Retained messages are best used for state information, such as:
- Device online/offline status
- Latest sensor readings
- Configuration values
- System modes (ON/OFF, AUTO/MANUAL)
For example, if a device publishes its status as online, any monitoring tool connecting later will immediately know the current state.
When You Should Avoid Retained Messages
They are not suitable for event-based data, such as:
- Button presses
- Alerts or alarms
- One-time notifications
- Logs
Using retained messages for events can cause confusion, as new subscribers may receive outdated actions that should not be repeated.
Clearing a Retained Message
To remove a retained message from a topic, publish:
- The same topic
- An empty payload
- Retain flag set to true
This tells the broker to delete the stored message.
Retained Messages in MQTT Explorer
Tools like MQTT Explorer make retained messages easy to identify. They are usually marked with a special icon or label and appear instantly when you subscribe to a topic.
This visual confirmation helps developers:
- Debug data flow
- Validate correct topic usage
- Ensure brokers are storing values properly
Common Mistakes to Avoid
Some frequent errors include:
- Retaining temporary or event data
- Forgetting to clear old retained values
- Using retained messages on shared or dynamic topics
- Assuming retained messages update automatically (they don’t)
Always remember: the broker only updates retained messages when a new retained publish happens.
Best Practices for Using Retained Messages
To use retained messages effectively:
- Keep topic structures clean and predictable
- Use them only for “last known state” data
- Document retained topics clearly
- Regularly review and clear outdated values
This approach keeps systems reliable and prevents confusion for new subscribers.
Real-World Analogy
Think of retained messages like a notice board that always shows the latest update. Anyone walking in can immediately see the current status without asking around. In technical monitoring scenarios similar to understanding how to test cortisol levels to assess current stress conditions having the most recent data instantly available is critical for accurate decisions.
Final Thoughts
MQTT retained messages are a simple but powerful feature that can dramatically improve system reliability and user experience when used correctly. They ensure that new subscribers are never left guessing and always start with the latest known data. By understanding when and how to use them, you can design MQTT systems that are cleaner, smarter, and easier to maintain.
