Use QoS 1 for telemetry, retained messages for current state, and an online or offline status topic backed by Last Will.
Quality of Service
QoS 0 delivers a message at most once with no acknowledgement. QoS 1 delivers at least once, so duplicates can occur. QoS 2 delivers exactly once through a four-packet handshake and costs the most. QoS applies per hop, and a subscriber receives at the lower of the publish and subscribe levels.
Choosing a Level for IoT Data
QoS 1 suits most telemetry, with a timestamp or sequence number so duplicates can be discarded. QoS 0 suits frequent readings where the next one replaces a lost one. QoS 2 is rarely needed for sensor data.
Retained Messages
A retained message is stored by the broker and sent to any new subscriber, so a dashboard sees the last value at once. Include a timestamp, so a stale retained value is recognizable.
Last Will
A client registers a Last Will when it connects. The broker publishes it if the client disappears without a clean disconnect. In MQTT 3.1.1 it is not sent after a clean disconnect, so publish a retained online status on connect and replace it with offline on a clean shutdown.
Sessions and Security
A persistent session lets the broker queue QoS 1 and 2 messages for a client that is offline. MQTT uses TCP port 1883 in plain text and 8883 over TLS. Use TLS and per-device credentials or certificates for anything outside a closed lab.
Common Questions
What is the difference between QoS 1 and QoS 2?
QoS 1 guarantees delivery at least once and may duplicate. QoS 2 guarantees exactly once using a four-packet exchange, with more overhead.
When is a Last Will message sent?
When the broker detects that a client has dropped without a clean disconnect, for example after a network failure. A client that disconnects cleanly does not trigger it in MQTT 3.1.1.
