MQTT Last Will and Testament

In practical applications, combining the Last Will and Testament (LWT) feature with retained messages can be a powerful method for monitoring the online and offline status of devices. Later in this article, we will delve into this concept with a more comprehensive explanation.

The Last Will and Testament (LWT) feature allows a client to specify a message that will be published by the broker on its behalf when the client disconnects unexpectedly or abnormally. The LWT message serves as a notification to other clients or subscribers that the client has disconnected without the chance to send a proper goodbye message.

Here's how the MQTT Last Will and Testament works:

  1. Setting the Last Will and Testament: When a client establishes a connection to the MQTT broker, it has the option to set a Last Will and Testament message. The client defines the topic, payload, QoS level, and retain flag for the LWT message.

  2. Unexpected Disconnection: If the client becomes disconnected unexpectedly, such as due to a network failure or a client-side issue, the broker detects the disconnection.

  3. Publishing the Last Will and Testament: When the broker detects the unexpected disconnection, it publishes the specified LWT message on behalf of the disconnected client. The message is published to the topic specified during the connection setup.

  4. Message Delivery: The LWT message is delivered to the subscribers or clients who have subscribed to the topic. The delivery follows the QoS level specified in the LWT message, ensuring reliable and orderly message delivery.

The Last Will and Testament feature is useful in scenarios where it is important to notify other clients or subscribers about the unexpected disconnection of a client. It allows clients to communicate their status or intentions even if they are unable to disconnect gracefully.

Some common use cases for Last Will and Testament include:

  • Presence Monitoring: Clients can use LWT to notify others of their online/offline status, enabling presence monitoring in MQTT-based applications.

  • State Update: Clients can use LWT to publish their current state or status information before disconnecting, ensuring that the latest information is available to interested parties.

  • Failure Detection: LWT messages can help detect failures or abnormal disconnections of clients, allowing other clients or systems to take appropriate action based on the received LWT message.

By utilizing the Last Will and Testament feature, MQTT clients can provide essential notifications to the MQTT network in the event of an unexpected disconnection, enabling better coordination and awareness among connected devices or systems.

Best Practices

In real world, LWT combines with retained messages feature can provide an effective way to notify subscribed clients about the current status of a publishing client and keep subscribed clients updated on the publishing client's state.

Let's break down the process using an example scenario:

  1. Setting up LWT: The publishing client (let's call it client1) sends a CONNECT message to the broker, including a lastWillMessage with a payload of "Offline", the lastWillRetain flag set to true, and the lastWillTopic set to client1/status. This sets up the LWT message that will be published on behalf of client1 if it disconnects unexpectedly. Refer step 4 below.

  2. Initial State Update: After the connection is established, client1 sends a PUBLISH message to the topic client1/status with the payload "Online" and the retained flag set to true. This initial retained message informs newly-subscribed clients about the current state of client1 as being "Online".

  3. Keeping Subscribers Updated: As long as client1 remains connected, any new clients that subscribe to the client1/status topic will receive the retained message with the payload "Online". This ensures that they are immediately informed about the current state of client1 upon subscription.

  4. Unexpected Disconnection: If client1 disconnects unexpectedly, such as due to a network failure or an abnormal disconnection, the broker detects the disconnection.

  5. Publishing LWT Retained Message: In response to the unexpected disconnection, the broker publishes the LWT message on behalf of client1 which was set at step 1 above.

  6. Notifying Subscribers: Clients that subscribe to the client1/status topic while client1 is offline receive the LWT retained message with the payload "Offline" from the broker. This ensures that they are promptly informed about the change in client1's status.

By using retained messages in conjunction with the LWT feature, other clients or systems can stay updated on the current status of client1 for the specific topic (client1/status). This pattern of combining retained messages and LWT ensures that subscribers receive the most recent state information, even if client1 unexpectedly disconnects.

RELATED ARTICLES

Leave a comment

Your email address will not be published. Required fields are marked *

Please note, comments must be approved before they are published