Topic
- In MQTT, a topic is a UTF-8 string that filters messages for a connected client. A topic consists of one or more levels separated by a slash (/), and these levels follow a hierarchical structure. A topic allows clients to easily access the data they are interested in.
- Example:
- home/living_room/temperature: This topic refers to the temperature data of the living room in the house.
- vehicles/car/speed: This topic can represent vehicle speed data.
- By subscribing to a particular Topic, clients can receive messages published on that Topic or send messages to that Topic.
Quality of Service (QoS)
In MQTT, Quality of Service (QoS) determines the level of reliability of message delivery. There are three different QoS levels:
- QoS 0 (at most once):
- Allows messages to be sent to the receiver at most once. No acknowledgement is expected when messages are transmitted over the cable. Therefore, it can be used if some data loss is not a problem.
Only available if the transmitter and receiver are connected by cable.
- Usage Area: Situations that are constantly updated, such as temperature, and where data loss can be tolerated.
- QoS 1 (at least once):
- Guarantees that the sender transmits the message at least once. The message can be resent until confirmation is received from the receiver. May result in the same data being sent more than once.
- Usage Area: Situations where it is critical that data is not lost, but some repetition is acceptable (e.g. alarm notifications).
- QoS 2 (exactly once):
- Reliable level and ensures that the message is delivered only once. The sent message is finalized with acknowledged packets. Therefore there is no data loss or retransmission, but it takes longer.
- Usage Area: Situations where sensitive data transfer is required and data duplication is undesirable (e.g. financial transactions).
Retain
- Retain means that a message is saved and then forwarded to newly connected clients. If a message is sent with the retain flag, it is saved in the MQTT Broker and every new client that connects to that Topic is first forwarded this retained message.
- Example: When a message “User” is sent with retain true, the broker stores this message. Afterwards, several different messages may have been sent with retain false. When you log in again, you will see the retained message “User” first. To delete this retained message, an empty (zero byte) message must be sent with retain true. This will unregister the “User” message and you will not see any retained message the next time you log in.
The Retain feature allows certain data to be quickly forwarded to newly connected clients and is useful when the latest status information needs to be retained.