MQTT Essentials Part 9: Last Will and Testament
Welcome to the ninth part of the MQTT Essentials, a blog series about the core features and concepts in the MQTT protocol. In this post we will cover the Last Will and Testament feature of MQTT.
MQTT is often used in scenarios were unreliable networks are very common. Therefore it is assumed that some clients will disconnect ungracefully from time to time, because they lost the connection, the battery is empty or any other imaginable case. Therefore it would be good to know, if a connected client has disconnected gracefully (which means with a MQTT DISCONNECT message) or not, in order to take appropriate action. The Last Will and Testament feature provides a way for clients to do exactly that.
Last Will and Testament
The Last Will and Testament (LWT) feature is used in MQTT to notify other clients about an ungracefully disconnected client. Each client can specify its last will message (a normal MQTT message with topic, retained flag, QoS and payload) when connecting to a broker. The broker will store the message until it detects that the client has disconnected ungracefully. If the client disconnect abruptly, the broker sends the message to all subscribed clients on the topic, which was specified in the last will message. The stored LWT message will be discarded if a client disconnects gracefully by sending a DISCONNECT message.
LWT helps to implement strategies when the connection of a client drops or at least to inform other clients about the offline status.
How to specify a LWT message for a client?
The LWT message can be specified by each client as part of the CONNECT message, which serves as connection initiation between client and broker.
More details about establishing a connection between client and broker can be found in part 3 of the MQTT Essentials series.
When will a broker send the LWT message?
According to the MQTT 3.1.1 specification the broker will distribute the LWT of a client in the following cases:
- An I/O error or network failure is detected by the server.
- The client fails to communicate within the Keep Alive time.
- The client closes the network connection without sending a DISCONNECT packet first.
- The server closes the network connection because of a protocol error.
We will hear more about the Keep Alive time in the next post.
Best Practices – When should you use LWT?
LWT is ideal for notifying other interested clients about the connection loss. In real world scenarios LWT is often used together with retained messages, in order to store the state of a client on a specific topic. For example after a client has connected to a broker, it will send a retained message to the topic client1/status with the payload “online“. When connecting to the broker, the client sets the LWT message on the same topic to the payload “offline” and marks this LWT message as a retained message. If the client now disconnects ungracefully, the broker will publish the retained message with the content “offline“. This pattern allows for other clients to observe the status of the client on a single topic and due to the retained message even newly connected client now immediately the current status.
So that’s the end of part nine in our MQTT Essentials series. We hope you enjoyed it. In the next and last post we’ll cover the MQTT heartbeat mechanism and how the broker knows a client is online or offline.
Have a great week and we’ll hope to see you on the next MQTT Monday!
You like the MQTT Essentials? Then sign up for our newsletter and get notified on each new post as soon as its available. If you prefer RSS, you can subscribe to our RSS feed here.