In MQTT, retained messages serve a specific function. What is the main purpose of using retained messages?
Think about what happens when a new device connects and wants the latest data.
Retained messages keep the last message on a topic so that new subscribers get the most recent data right away without waiting for the next update.
Consider this MQTT publish command with the retain flag set:
mosquitto_pub -t 'home/temperature' -m '22' -r
What is the expected behavior of the broker regarding this message?
Retain flag means the broker keeps the message for future subscribers.
When a message is published with the retain flag, the broker saves it as the last retained message for that topic and delivers it immediately to any new subscribers.
A subscriber connects to the MQTT broker and subscribes to a topic with a retained message, but does not receive the retained message. Which of the following is the most likely cause?
Think about how retained messages can be removed from the broker.
Publishing an empty retained message to a topic clears the retained message. If this happens before the subscriber connects, the subscriber will not receive any retained message.
Arrange the following steps in the correct order to successfully publish a retained message and have a new subscriber receive it immediately.
Think about what happens first: publishing, storing, subscribing, then delivery.
The correct order is: publish with retain flag, broker stores the message, subscriber connects and subscribes, broker sends the retained message immediately.
You have a temperature sensor publishing data to an MQTT topic with retained messages enabled. What is the best practice to update the retained message correctly?
Consider how retained messages keep the last known good value.
Publishing each new reading with the retain flag updates the retained message so new subscribers always get the latest sensor data immediately.