Reliable MQTT: QoS, Retained, LWT and Security

ReviewedAdvanced ProAbout 6 min
NRF24
In short

Reliable MQTT: QoS, Retained, LWT and Security: the short answer

MQTT offers three quality-of-service (QoS) levels for each message. QoS 0 sends once with no acknowledgement; messages are lost if the link drops, which is fine for frequent sensor readings.

Level
Advanced
Time
About 6 min
Topic
Communication
You will learn to

Build an MQTT system that survives disconnects, reports device status and keeps data private.

Before this: MQTT Basics: Publish and Subscribe, Security, Privacy & Safe Remote Control

MQTT offers three quality-of-service (QoS) levels for each message. QoS 0 sends once with no acknowledgement; messages are lost if the link drops, which is fine for frequent sensor readings. QoS 1 retries until acknowledged, so the message arrives at least once but may be duplicated; make the receiver tolerant, for example by including a sequence number or timestamp. QoS 2 guarantees exactly once with a four-step handshake and costs the most. Note that PubSubClient publishes only at QoS 0; libraries such as AsyncMqttClient or esp-mqtt support QoS 1 and 2.

Two features make dashboards work properly. A retained message is stored by the broker and sent immediately to any new subscriber, so the latest state or setting is always available; publish states retained but not fast-changing readings. The Last Will and Testament (LWT) is a message the device registers when it connects: if it disappears without disconnecting cleanly, the broker publishes it. The usual pattern is to set the will to home/garden/status = offline (retained) and publish online (retained) after connecting, so everyone always knows whether the device is alive. Set the keep-alive to suit your network, typically 30 to 60 s.

Secure the broker before it leaves your bench. Turn off anonymous access, give each device its own user name and password, and use access control lists so a device can only publish to its own topics. Use TLS on port 8883 so passwords and data are encrypted; on the ESP32, WiFiClientSecure with the broker's CA certificate handles this. Persistent sessions (clean session off) with QoS 1 let the broker queue commands for a device that is briefly offline.

Choose the quality of service

MQTT has three levels. QoS 0 sends once with no confirmation, which is fine for a reading repeated every few seconds. QoS 1 sends until acknowledged, so the message arrives at least once and may arrive twice. QoS 2 guarantees exactly once at the cost of more traffic. Most projects use 0 or 1, and the receiver should cope with duplicates.

The level applies to each leg, from publisher to broker and from broker to subscriber, and the lower of the two settings is used on the last leg.

Retained messages and last will

A retained message is kept by the broker and given to any new subscriber at once, which suits status and settings, so a dashboard that starts later still shows the latest value. A last will and testament is a message the broker publishes if a device disappears without disconnecting. Publish an online status when connecting, and set the last will to offline, so a dashboard knows a device has dropped out.

Keep alive sets how often the client pings the broker, which decides how quickly a dead connection is noticed.

Topics and security

Design topic names with a structure, such as site/device/measurement, and avoid putting data in the name. Use a username and password per device with permissions limited to its own topics, and TLS on the connection. Do not use wildcard subscriptions on a large system if you only need a few topics.

Common mistakes
  • Expecting QoS 1 to prevent duplicate messages.
  • Not using last will, so a dead device looks as if it is still online.
  • Using one shared password with access to every topic.
Advanced lesson · Pro plan

Practise this lesson with Pro

The explanation above is free for everyone. The practice for Advanced lessons in every domain are part of the Pro plan at ₹999 a month.

With Pro you also get: a worked example, a hands-on task, a 3-question check.

Common questions

What will I learn in "Reliable MQTT: QoS, Retained, LWT and Security"?

Build an MQTT system that survives disconnects, reports device status and keeps data private.