A retained message is a special message that the MQTT broker stores and sends immediately to any client that subscribes to the topic. It keeps the last message sent on that topic.
Click to reveal answer
beginner
How does a retained message help new subscribers?
New subscribers get the last retained message right after subscribing, so they immediately have the latest information without waiting for the next update.
Click to reveal answer
intermediate
How do you publish a retained message using MQTT command line?
Use the option to mark the message as retained. For example: mosquitto_pub -t 'home/temperature' -m '22°C' -r This publishes '22°C' as a retained message on the topic 'home/temperature'.
Click to reveal answer
intermediate
What happens if you publish a retained message with an empty payload?
Publishing a retained message with an empty payload clears the retained message on that topic. The broker deletes the stored message.
Click to reveal answer
intermediate
Why should you be careful when using retained messages?
Because retained messages stay on the broker until cleared, outdated or wrong data can be sent to new subscribers if not managed properly.
Click to reveal answer
What does a retained message do in MQTT?
ADeletes all messages on a topic
BEncrypts the message content
CSends messages only to one client
DStores the last message on a topic for new subscribers
✗ Incorrect
A retained message stores the last message on a topic so new subscribers get it immediately.
How do you clear a retained message on a topic?
APublish a message without the retained flag
BUnsubscribe from the topic
CPublish a message with an empty payload and retained flag
DRestart the MQTT broker
✗ Incorrect
Publishing an empty retained message clears the stored retained message on that topic.
Which MQTT command publishes a retained message?
Amosquitto_pub -t 'topic' -m 'message' -r
Bmosquitto_sub -t 'topic' -r
Cmosquitto_pub -t 'topic' -m 'message'
Dmosquitto_sub -t 'topic'
✗ Incorrect
The '-r' flag in mosquitto_pub marks the message as retained.
When does a new MQTT subscriber receive a retained message?
AImmediately after subscribing to the topic
BOnly after the next message is published
COnly if the subscriber requests it
DNever, retained messages are for the broker only
✗ Incorrect
New subscribers get the retained message immediately after subscribing.
What is a risk of using retained messages carelessly?
ALosing messages permanently
BSending outdated data to new subscribers
COverloading the broker with messages
DPreventing clients from subscribing
✗ Incorrect
Retained messages stay until cleared, so old data can be sent if not updated.
Explain what retained messages are and how they help MQTT clients.
Think about how new clients get the latest info without waiting.
You got /3 concepts.
Describe how to publish and clear retained messages using MQTT commands.
Focus on the command line options and what happens on the broker.
You got /3 concepts.
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
Step 1: Understand retained message concept
Retained messages keep the last message sent on a topic stored on the broker.
Step 2: Identify the purpose for new subscribers
New subscribers receive the last retained message immediately upon subscribing, ensuring they have the latest state.
Final Answer:
To store the last message on a topic for new subscribers -> Option B
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
Step 1: Identify publish options related to message retention
The retain flag controls whether the broker stores the message as retained.
Step 2: Confirm correct flag for retention
Setting retain=true tells the broker to keep the message for future subscribers.
Final Answer:
retain -> Option A
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?
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
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.
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.
Step 3: Determine what new subscribers receive
New subscribers get no retained message because the last retained message was cleared by the second publish.
Final Answer:
No message because retain is false on last publish -> Option C
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
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
Step 1: Understand how to clear retained messages
Publishing an empty payload with retain=True clears the retained message on that topic.
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.
Final Answer:
client.publish('device/status', '', retain=True) -> Option A
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
Step 1: Understand MQTT wildcard subscription
The subscription to devices/device123/status matches only that specific topic, not all devices.
Step 2: Understand retained message delivery
The broker sends the last retained message on that exact topic immediately upon subscription.
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.
Final Answer:
It receives the last retained status message for device123 immediately -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.