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



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.

    --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

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.