MQTT Last Will and Testament
What is MQTT Last Will and Testament?
MQTT's Last Will and Testament (LWT) is a message a client registers with the broker in its CONNECT packet, which the broker publishes on the client's behalf if the connection ends without a normal disconnect, so other devices learn it went offline. AdminKit's MQTT exposure check never registers a will.
By AdminKit team, Netbase JSC · Updated
An MQTT client cannot announce its own crash, so the protocol lets it leave a message with the broker in advance. The OASIS MQTT 5.0 specification calls this the Will Message: when the Will Flag in the CONNECT packet is set, the broker stores the will with the session and must publish it after the network connection closes, unless the client first disconnects normally.
How does an MQTT Last Will work?
The will travels in the CONNECT packet as a small, complete publish that the broker holds back:
- Will Topic and Will Payload — where the message goes and what it says, for example
offlineon a device status topic. - Will QoS — the quality of service level (0, 1 or 2) the broker uses when it publishes the will.
- Will Retain — whether the published will is kept as the topic’s retained message for later subscribers.
- Will Properties (MQTT 5) — including the Will Delay Interval, in seconds, which defaults to 0. The broker waits that long before publishing and drops the will if the client reconnects to its session in time, so a brief network blip does not send a false “offline”.
The specification lists the situations that trigger the will: an I/O or network failure, a client that stays silent past its keep-alive time, or a connection closed without a DISCONNECT carrying reason code 0x00. A normal disconnect deletes the will. In MQTT 5 a client can also leave on purpose and still have its will published, by disconnecting with reason code 0x04. Client tools expose the same fields: the mosquitto_pub manual page documents --will-topic, --will-payload, --will-qos and --will-retain for a message the broker sends out if the client disconnects unexpectedly. HiveMQ’s MQTT Essentials article on Last Will describes the common pairing of a retained “Offline” will with a retained “Online” message published to the same status topic after connecting, so new subscribers always see the device’s current state.
Why does the Last Will matter on an exposed broker?
A will is a deferred publish, so it carries the same trust as any other message on the broker. On a broker that accepts anonymous connections, anyone who can reach it can connect and register a will on any topic the broker lets unauthenticated clients publish to. Dashboards, alerting rules or automation that treat a status topic as proof of a device’s state can then be fed a forged “offline”, or a retained will that stays on the topic after the connection is gone. Requiring authentication and per-client topic permissions closes this along with every other anonymous publish; AdminKit’s guide to securing an MQTT broker walks through those settings.
AdminKit’s MQTT exposure check shows whether a broker on your own public IP accepts an anonymous connection in the first place. It sends a CONNECT with no will and no credentials and never publishes.
The MQTT Last Will belongs to AdminKit’s machine protocols cluster — browse the AdminKit glossary index for other related terms.
Frequently asked questions
When does an MQTT broker publish a client's Last Will?
When the network connection closes without a DISCONNECT carrying reason code 0x00 (normal disconnection): for example an I/O or network error, a client that misses its keep-alive window, or a client that just closes the TCP connection. In MQTT 5 the broker waits for the Will Delay Interval first, and drops the will if the client reconnects to the session in that time.
Is the Last Will sent when a client disconnects normally?
No. A DISCONNECT with reason code 0x00 tells the broker to delete the stored will. In MQTT 5 a client that wants its will published anyway can disconnect with reason code 0x04, Disconnect with Will Message.
Why do devices often set Will Retain on a status topic?
A retained will stays on the topic after it is published, so a subscriber that connects later still sees the device's last known state. A common pattern publishes a retained "online" message on connect and registers a retained "offline" will on the same topic.
Does AdminKit's MQTT check set a Last Will on my broker?
No. The check sends an anonymous CONNECT with a clean session, no will and no username or password, and it never publishes on any topic.