Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Fundamental Features of MQTT | Set 3

  • Last Updated : 21 Aug, 2020

Prerequisites –

Last Will and Testament :
In MQTT, publisher client publishes messages on specific topic to Broker. The broker then publishes these messages to clients subscribed to that topic. If the publisher/subscriber client wants to end connection with broker, it sends DISCONNECT message. In this way, communication can be disconnected gracefully.

But there are several scenarios in which client might get disconnected abruptly. In such case, client ends communication with broker without sending DISCONNECT message. In such scenarios, other clients will not be informed about failure and hence they will keep waiting for messages from disconnected client.

A client could get disconnected abruptly due to any of following reasons –

  • Failure of underlying network due to loss of connection or hardware failure.
  • Failure to send message within keep-alive period.

To solve such problem, concept of last will messages is used. Using this feature, client provides predefined will message when it first gets connected to broker. This message is stored by the broker.

  • If the client disconnects abruptly, broker will publish will message on will topic for all subscribed clients.
  • If the client disconnects gracefully, broker will discard will message.

LWT message and LWT topic :

  • LWT message defines message to be published as last will to subscribed clients.
  • LWT topic defines topic on which last will message will be published.

Figure – Last Will and Testament

Example –

Figure – Mosquitto Illustration of Last Will and Testament

In the above illustration, will topic is “sensor/status” and will message is “offline”. Once client disconnects abruptly, socket error is displayed on mosquito broker terminal. Hence broker then sends will message “offline” to client subscribed to topic “sensor/status”.

Mosquitto Commands :

  • Last Will Topic (mandatory) –
    If no other parameters are provided, then publishing only last will topic will publish non-retained, zero-length message with QoS0 to subscribed clients.
  • Last Will Message (optional) –
    Defaults to an empty message of zero length.
  • Last Will QoS (optional) –
    Defaults to QoS0.
  • Last Will Retain (optional) –
    Defaults to retain false.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!