Fundamental Features of MQTT | Set 3

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





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.

    --will-topic
  • Last Will Message (optional) –
    Defaults to an empty message of zero length.

     --will-payload
  • Last Will QoS (optional) –
    Defaults to QoS0.

    --will-qos
  • Last Will Retain (optional) –
    Defaults to retain false.

    --will-retain

Contact Us