Introduction to MQTT (Chapter 6: Last Will & Testament)

Introduction to MQTT (Chapter 6: Last Will & Testament)

MQTT was designed to facilitate communication between devices on unreliable networks. One of the characteristics that contribute to a network being deemed unreliable is that clients or devices can suddenly and inexplicably become disconnected. In large IoT or industrial automation applications where there are many interconnected devices, the failure of a critical device can negatively impact the whole system.

Clients in an MQTT network can disconnect gracefully or ungracefully. A graceful disconnect is one that is expected and being done for a specific reason. The client will generate an MQTT DISCONNECT message during a graceful disconnect, which provides information that allows the system to respond appropriately. In a robust implementation, the system will continue to function despite the disconnected client.

In the case of an ungraceful or unexpected disconnect, the Last Will and Testament (LWT) feature of MQTT informs other clients about the event so they can take suitable action. LWT messages are only delivered upon an ungraceful disconnect and are deleted once a client disconnects gracefully. The inclusion of a Last Will and Testament message ensures that a final word is heard from a client no matter how they disconnect.

What is MQTT’s Last Will and Testament?

The purpose of an LWT message is to specify the actions to be taken after a client goes offline unexpectedly. The last will information is contained in a normal MQTT message that contains a topic, a retained message flag, quality of service level, and a data payload. This information is only used in the event of an ungraceful disconnect.

The LWT message is delivered to the broker when the client generates a CONNECT message to contact the broker. The broker stores the LWT message until it is needed to address an ungrateful disconnect. LWT messages for a client are discarded by the broker as soon as a client generates the DISCONNECT message that indicates a graceful exit. Since the client disconnected normally, there is no need for the LWT message to be delivered.

If the broker detects that a client disconnected ungracefully, the LWT message is sent to all clients that have subscribed to the last will message topic. There are several scenarios in which the broker sends the LWT message to the subscribed clients.

?     When a network failure or I/O error is detected by the broker;

?     The broker needs to close the network connection to address a protocol error;

?     The client closes the network connection without sending a DISCONNECT message;

?     The client does not communicate with the broker within the Keep Alive period.

If any of these conditions are met, the LWT message for the given client is sent to all subscribers so they can address the situation. The ability to send LWT messages can be a critical factor in maintaining an MQTT network’s functionality.

Using Last Will and Testament

An LWT message is essentially a final message delivered by a broker on behalf of a client that can no longer send messages itself. The information contained in the message can be used to initiate recovery procedures in the event of an important client being disconnected. In conjunction with retained messages, an LWT message keeps other clients updated as to the status of a specific client regarding a topic of interest.

A simple example helps clarify the interaction of the LWT message with its associated clients. A client has connected to the broker and requested an LWT message to be delivered that informs all subscribers that it is now offline. Under normal circumstances, the client sends an “online” message to topic subscribers. They are notified of a change in status by either a graceful DISCONNECT message received by the broker and disseminated to subscribers or the LWT message in cases where the connection terminates abnormally.

Based on the way the system is configured, an LWT message may be captured by a monitoring system that determines the nature of the outage and whether further alerts need to be generated. The LWT message can also be used to modify the system’s configuration so functionality can be maintained despite the absence of a particular client. Using LWT messages effectively protects the network against unexpected component failures by notifying interested subscribers when a failure occurs. 

要查看或添加评论,请登录

GARRETT SCHMIDT的更多文章

社区洞察

其他会员也浏览了