In this episode of MQTT Essentials series, we will talk about MQTT Last Will and Testament.
The Last Will and Testament (LWT) is a notification to other clients when a client disconnects ungracefully.
Each client can specify its last will message when it connects to a broker. The last will message is a normal MQTT message with a topic, retained message flag, QoS, and payload. The broker stores the message until it detects that the client has disconnected ungracefully. In response to the ungraceful disconnect, the broker sends the last-will message to all subscribed clients of the last-will message topic. If the client disconnects gracefully with a correct DISCONNECT message, the broker discards the stored LWT message.
👉 How do you specify an LWT message for a client?
Clients can specify an LWT message in the CONNECT message that initiates the connection between the client and the broker. You can set the lastWillTopic (topic of the MQTT message the broker should send in behalf of the client if it dies), the lastWillMessage (actual content of the message), the lastWillQoS, and you can set the lastWillRetain
👉 Scenarios for Last Will and Testament usage
The broker sends the LWT message when:
- the broker detects an I/O error or network failure
- the client fails to communicate within the defined Keep Alive period
- the client does not send a DISCONNECT packet before it closes the network connection
- the broker closes the network connection because of a protocol error
👉 Best practices:
Last Will Testament is a great way to notify other subscribed clients about the unexpected loss of connection of another client. In real-world scenarios, LWT is often combined with retained messages to store the state of a client on a specific topic. For example, client1 first sends a CONNECT message to the broker with a lastWillMessage that has “Offline” as the payload, the lastWillRetain flag set to true, and the lastWillTopic set to client1/status. Next, the client sends a PUBLISH message with the payload “Online” and the retained flag set to true to the same topic (client1/status). As long as client1 stays connected, newly-subscribed clients to the client1/status topic receive the “Online” retained message. If client1 disconnects unexpectedly, the broker publishes the LWT message with the payload “Offline” as the new retained message. Clients that subscribe to the topic while client1 is offline, receive the LWT retained message ("Offline") from the broker. This pattern of retained messages keeps other clients up to date on the current status of client1 on a specific topic.
*** Chapters / Table of Contents ***
00:00 Introduction MQTT Last Will & Testament in MQTT
00:19 What is Last Will and Testament (LWT) in MQTT
01:41 Connect packet and LWT in MQTT
02:29 Scenarios for LWT usage in MQTT
03:48 Best practices for LWT usage in MQTT
05:11 Announcing part 11 - Keep Alive & Client Takeover in MQTT
👉 If you missed Part 1 - What is MQTT, watch it now: [ Ссылка ]
👉 We also explain the Last Will and Testament (LWT) in detail in the HiveMQ blog here: [ Ссылка ]
👉 We also explain the core of MQTT concepts, its features, and other essential information on the HiveMQ blog: [ Ссылка ]
👉 Get your own MQTT Essentials in a comprehensive, free E-Book: [ Ссылка ]
👉 To know about HiveMQ: [ Ссылка ]
Subscribe to our HiveMQ channel to get video updates. Hit the subscribe button above or subscribe to our newsletter ([ Ссылка ]) to keep up to date on the latest MQTT news.
👉 We're looking forward to seeing you next week for MQTT Essentials part 11 - Keep Alive & Client Takeover: [ Ссылка ]
#MQTT #MQTTEssentials #MQTTMonday #IoT
MQTT Last Will and Testament | MQTT Essentials Part 10
Теги
MQTTMQTT EssentialsMQTT MondayIoTInternet of ThingsIoT protocolHiveMQMQTT ExplanationMQTT TutorialMQTT ProtocolMQTT Brokerpublish subscribeMQTT ClientMQTT MessageMQTT Connectionunsubscribe MQTTMQTT TopicMQTT WildcardsQuality of Service LevelMQTT QoSQoS 1QoS 2QoS 0Quality of Service MQTTMessage Queueing MQTTMessage QueueingLast Will MQTTLast Will & TestamentTestament MQTTLast Will & Testament MQTTLWT messageLWT