In MQTT, the Last Will and Testament (LWT) feature allows the client to specify a message that the broker will automatically send on behalf of the client if the client’s connection to the broker is lost. This can be useful in situations where it’s important to notify other clients or systems that a particular client is no longer available.
When a client connects to an MQTT broker, it can include a LWT message as part of the connection request. This message includes the topic and payload that the broker should use in the event that the client’s connection is lost. The broker will then retain this message until it receives a disconnect message from the client, or until the connection is lost.
![MQTT Last Will and Testament 1 Last Will and Testament](https://i0.wp.com/takethenotes.com/wp-content/uploads/2023/01/image-15.png?resize=640%2C365&ssl=1)
When the connection is lost and the broker detects that the client is no longer connected, it will then publish the LWT message to the specified topic with the specified payload. This message can be used to notify other clients or systems that the client is no longer available. In this blog, we will understand this feature in detail.
In order to use LWT, a client must first connect to a broker and specify the LWT message and topic. The client must also specify the “Will Flag” in the connect packet, which indicates to the broker that a LWT message should be sent in the event of an unexpected disconnection. The client can also specify the “Will QoS” and “Will Retain” flags, which determine the QoS level and retention of the LWT message.
LWT is a optional feature in MQTT, and whether or not to use it depends on the specific requirements of the application. However, in scenarios where reliable communication is essential, such as in IoT applications, using LWT can be a useful way to ensure that important information is still communicated even in the event of an unexpected disconnection.
The broker will send the Last Will and Testament (LWT) message when the client disconnects unexpectedly. This can happen if the client’s network connection is lost or if the client crashes. The LWT message is sent to a pre-defined topic and contains a pre-defined payload that was set by the client when it connected to the broker. The message is used to notify other clients that the client is no longer connected, allowing them to take appropriate action. It is important to note that the LWT message is only sent if the client has enabled the LWT feature when connecting to the broker.
Now let us understand how a client can inform the broker about its last will.
The client will send a CONNECT message to the broker to establish a connection. This message contains information such as the client’s protocol level, the client’s keep-alive time, and the client’s identifier. The CONNECT message also contains the LWT bit.
The LWT bit is used to indicate that the client wishes to publish a last will and testament message to a specific topic upon disconnection. For example, let’s consider a simple use case where a client is connecting to a broker for a home automation application. The client wishes to publish a last will and testament message to the topic “home/library/lightbulb” upon disconnection. The CONNECT message for this client may be represented in JSON as follows:
![MQTT Last Will and Testament 2 image 9](https://i0.wp.com/takethenotes.com/wp-content/uploads/2023/01/image-9.png?resize=585%2C564&ssl=1)
In this example, the client is using MQTT version 4 and has a keep-alive time of 60 seconds. The client’s identifier is “SH-18GL6SBIXY89OXYTBSLI” and the cleanSession flag is set to true. The LWT field is set to publish a message with the payload “offline” to the topic “home/library/lightbulb” with a QoS of 1 and the retain flag set to false upon disconnection.
References:
- MQTT QOS
- MQTT Persistent session
- MQTT Version 3.1.1 (oasis-open.org)
- MQTT Version 5.0 (oasis-open.org)