Stable and reliable communication is crucial for secure data transmission in an industrial Unified Namspace (UNS). But what happens if a device unexpectedly loses the connection? This is where the MQTT Last Will comes into play. In the following, we will examine how this digital “farewell letter” works.
What is MQTT Last Will?
Within the MQTT protocol, an MQTT Last Will acts as a kind of safety net in the event that a client unintentionally loses the connection to the broker. This is a special message that the client sends to the broker when the connection is established. If the client then loses the connection unexpectedly, the broker assumes responsibility and publishes this message on behalf of the client. In MQTT, unexpected disconnects are specified as follows:
- I/O error or network error: The broker detects problems with the input/output or the network connection.
- Failed communication within the keep-alive period: The client does not respond within the specified keep-alive period.
- Client closes the connection without DISCONNECT: The client closes the network connection without sending a DISCONNECT packet.
- Broker closes the connection due to a protocol error: The broker closes the network connection due to a protocol error.
- DISCONNECT message with the return code “disconnect-with-will-message”: A client terminates the connection with a special last will message, only in MQTT v5.0.
How does MQTT Last Will work?
The distinction between expected and unexpected disconnects is made by the MQTT disconnect packet. A clean disconnect by the client contains this disconnect packet, while an unexpected disconnect does not. If an unexpected disconnect is detected, the broker sends the Last Will message to all subscribers. Thereby, the broker saves the MQTT Last Will of the client until the client disconnects in a planned manner.
This mechanism enables a clear distinction to be made between planned and unplanned disconnects. While a client can send a message on a specific topic to communicate its status in the event of a planned disconnect, the publication of the MQTT Last Will by the broker signals an unexpected failure. An MQTT Last Will is a regular message with all the usual attributes such as QoS, retained flag and topic.
Use in the Unified Namespace (UNS)
The use of MQTT Last Will offers the following two advantages in the context of the Unified Namespace (UNS).
1. Reliability
-
- Topics are not left in an undefined state if a client loses the connection.
- In addition, other clients are notified if a client fails unexpectedly.
- Example: A device regularly sends its status on a topic within the UNS. In the event of an unexpected failure, the broker publishes the MQTT Last Will of the device, which contains an “Offline” message on a corresponding status topic. This message can be marked as a retained message so that new clients that connect later are also informed about the offline status of the device. This makes the status message very reliable and critical use cases can use the information.
2. Status tracking in the UNS
- The MQTT Last Will can be used to update the status of a device or component in the UNS when it goes offline.
- This enables other clients to react appropriately to the failure and take action if necessary.
- Example: Use the option of using Last Will Messages as an indicator for expired sessions by setting the Will Delay Interval greater than the Session Expiry Interval. This enables early detection of inactive clients, even in the event of a controlled disconnect (using the DISCONNECT package and reason code 0x04).
Conclusion
MQTT Last Will is a powerful feature to ensure reliability and status tracking in MQTT-based systems. Especially in context of an Industrial Unified Namespace (UNS), it enables clear communication about the status of devices and components, even if they fail unexpectedly.