Bird
Raised Fist0
IOT Protocolsdevops~6 mins

Retained messages in IOT Protocols - Full Explanation

Choose your learning style10 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 message → A message flagged to be stored by the broker and sent immediately to new subscribers.
Broker → The server that receives, stores, and forwards messages between publishers and subscribers.
Retain flag → A marker on a message indicating it should be saved as the last known message for its topic.
Clearing retained message → Sending a message with an empty payload and retain flag to remove the stored message.
New subscriber → A 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.

Practice

(1/5)
1. What is the main purpose of a retained message in MQTT?
easy
A. To delete all messages from the broker
B. To store the last message on a topic for new subscribers
C. To encrypt messages during transmission
D. To limit the message size to 256 bytes

Solution

  1. Step 1: Understand retained message concept

    Retained messages keep the last message sent on a topic stored on the broker.
  2. Step 2: Identify the purpose for new subscribers

    New subscribers receive the last retained message immediately upon subscribing, ensuring they have the latest state.
  3. Final Answer:

    To store the last message on a topic for new subscribers -> Option B
  4. Quick Check:

    Retained message = last stored message [OK]
Hint: Retained means broker keeps last message for new subscribers [OK]
Common Mistakes:
  • Confusing retained messages with message encryption
  • Thinking retained messages delete old messages
  • Assuming retained messages limit message size
2. Which MQTT publish option must be set to true to create a retained message?
easy
A. retain
B. qos
C. cleanSession
D. willFlag

Solution

  1. Step 1: Identify publish options related to message retention

    The retain flag controls whether the broker stores the message as retained.
  2. Step 2: Confirm correct flag for retention

    Setting retain=true tells the broker to keep the message for future subscribers.
  3. Final Answer:

    retain -> Option A
  4. Quick Check:

    retain flag true = retained message [OK]
Hint: Set retain flag true to keep message on broker [OK]
Common Mistakes:
  • Confusing retain with cleanSession flag
  • Using qos instead of retain for message storage
  • Mistaking willFlag for retention control
3. Given the following MQTT publish code snippet, what will a new subscriber receive immediately after subscribing to topic home/temperature?
client.publish('home/temperature', '22°C', retain=True)
client.publish('home/temperature', '23°C', retain=False)
medium
A. The message '22°C' because it was retained
B. The message '23°C' because it was the last published
C. No message because retain is false on last publish
D. Both messages '22°C' and '23°C'

Solution

  1. Step 1: Analyze retained flag on each publish

    The first message '22°C' is published with retain=True, so it is stored on the broker.
  2. Step 2: Understand effect of second publish with retain=False

    The second message '23°C' is published without retain, so it is sent but not stored as retained. Importantly, publishing with retain=False on the same topic clears the retained message.
  3. Step 3: Determine what new subscribers receive

    New subscribers get no retained message because the last retained message was cleared by the second publish.
  4. Final Answer:

    No message because retain is false on last publish -> Option C
  5. Quick Check:

    Retain=False on last publish clears retained message [OK]
Hint: Retain=False on last publish clears retained message [OK]
Common Mistakes:
  • Assuming last published message is always retained
  • Thinking retain=False messages overwrite retained messages
  • Expecting multiple retained messages per topic
4. You want to clear a retained message on topic device/status. Which publish command will correctly clear it?
medium
A. client.publish('device/status', '', retain=True)
B. client.publish('device/status', '', retain=False)
C. client.publish('device/status', None, retain=True)
D. client.publish('device/status', None, retain=False)

Solution

  1. Step 1: Understand how to clear retained messages

    Publishing an empty payload with retain=True clears the retained message on that topic.
  2. Step 2: Analyze the options

    Publishing with retain=False does not update or clear the retained message. Using None as payload is invalid. Only publishing an empty string ('') with retain=True clears the retained message.
  3. Final Answer:

    client.publish('device/status', '', retain=True) -> Option A
  4. Quick Check:

    Empty payload + retain=True clears retained message [OK]
Hint: Publish empty message with retain=True to clear retained message [OK]
Common Mistakes:
  • Using retain=False when trying to clear retained message
  • Sending None as payload which causes error
  • Expecting retain=False to remove retained message
5. You have multiple IoT devices publishing their status to devices/+/status with retained messages. A new device subscribes to devices/device123/status. What will it receive and why?
hard
A. It receives only non-retained messages published after subscription
B. It receives all retained messages from all devices under devices/+/status
C. It receives no messages until a new status is published
D. It receives the last retained status message for device123 immediately

Solution

  1. Step 1: Understand MQTT wildcard subscription

    The subscription to devices/device123/status matches only that specific topic, not all devices.
  2. Step 2: Understand retained message delivery

    The broker sends the last retained message on that exact topic immediately upon subscription.
  3. Step 3: Determine what the new device receives

    The new device receives the last retained status message for device123 immediately, not messages from other devices.
  4. Final Answer:

    It receives the last retained status message for device123 immediately -> Option D
  5. Quick Check:

    Retained message sent on exact subscribed topic [OK]
Hint: New subscriber gets last retained message on exact topic [OK]
Common Mistakes:
  • Thinking wildcard subscription delivers all retained messages
  • Expecting no retained messages on subscription
  • Confusing retained with non-retained message delivery
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard