MQTT Last Will and Testament Use and Examples

last-will-messages-iconThe last will and testament message is used to notify subscribers of an unexpected shut down of the publisher.

The basic process is.

  1. The publisher tells the broker to notify all subscribers to a topic, using the last will message , in the event that the connection breaks
  2. If the broker detects a connection break it sends the last will message to all subscribers of that topic.
  3. 3. If the client disconnects using the disconnect message then no last will message is sent.

Example Outline and Objectives

In this Example  I will be using a 2 python scripts that represent two clients called Python1 and Python2.

One client is used as the subscriber to some topics and the other script is used to publish messages  on those topics.

The objective is to see what happens when the publisher gets disconnected.

Procedure :

  1. Use a client to publish messages.
  2. The publisher will set a last will message for a topic.
  3. We monitor the publisher using a subscribing client.
  4. We break the connection between the publisher and broker first using an normal disconnect and secondly using an abnormal disconnect.
  5. We observer the result.

Example 1- Normal Disconnect

In this example we disconnect from the broker by sending a disconnect message and note that no Last will Message is sent.

last-will-diconnect

Notes Refer to screen shot above

1.The subscriber client starts

2 The publisher sets a last will message for one (bulb1).

3. The publisher publishes two test messages one on each topic.

4. The subscriber sees these messages.

5. The connection between the publisher and broker is broken by sending a normal disconnect command. Notice the Broker screen showing this disconnect

6. Notice that no Last will message is received by the subscriber

Example 2- Abnormal Disconnect

In this example we disconnect from the broker by simulating a network fault  and note that a Last will Message is sent.

mqtt-ast-will-example

Notes Refer to screen shot above

1.The subscriber client starts

2 The publisher sets a last will message for one (bulb1).

3. The publisher publishes two test messages one on each topic.

4. The subscriber sees these messages.

5. The connection between the publisher and broker is broken.

I achieved the connection break simply by closing the python IDE for the publisher client. The broker sees it as a socket error. -see broker screen shot below.

mqtt-last-will-broker

6. The Last will message is sent by the broker and received by the subscriber

Python MQTT Client Notes

To set the last will message you use the auxiliary function will_set(). This you must call before you establish the connection.

Here is a code snippet taken for my example scripts.


lwm="Bulb1 Gone Offline" # Last will message
topic1="bulb1"
print("Setting Last will message=",lwm,"topic is",topic1 )
client2.will_set("bulb1",lwm,QOS1,retain=False)

Note: Setting a QOS of 1 for the last will message attempts to makes sure the subscribers see it.

Setting the retain flag to True will guarantee this even to subscribers that don’t currently have a connection.

Last Will Usage Example

A client (sensor or device) could publish it’s current connection status on a topic e.g. sensors/sensor1/connection_status.

The client would update this status when starting and when disconnecting. The Last Will would then be used to update the status in the case of a failure.

Summary

The last will message, if used, is only sent if a publisher is disconnected abnormally. e.g. through a network error.

If the publisher disconnects uses the MQTT disconnect sequence then no last Will message is sent.

Was This Useful?

MQTT by Example Series

Related tutorials and Resources:

Save

Save

Save

Please rate? And use Comments to let me know more

6 comments

  1. Hi Steve,

    I have an issue with the Last will message. When the broker goes down/restarts, still it will send the LWM that was registered for an MQTT client. Is this the expected behavior? I want the LWM to be sent out only if the connections get closed by the client itself, not when the broker disconnects/resets the connection.

    Thanks,
    Pradeep

    1. The last will is only sent when the connection drops unexpectedly. If the client disconnects normally it is not sent.
      So how are you testing it?
      Rgds
      Steve

  2. Hello Steve,

    Your valuable tutorials are priceless; thank you for that!!

    How can I change the time for the mosquitto server to publish a WLT message for an abnormal disconnected client to a topic?
    I manage to capture the event of a client closes its connection in an abnormal way however it takes a few minutes until the WLT is published by the server from the moment the client killed; I’m running mosquitto server version 1.4.15 and my clients connected via WSS; my will settings are:

    var willMessage = UserID + “_SYSTEM_DEAD”;
    client = mqtt.connect({servers : [{ host: host, port: port}], username :username, password:pass, useSSL:true, will:{topic:’sandbox’, payload:willMessage, qos:0, retain:true }});

    my .conf file settings are:
    listener 1883
    listener 9001
    protocol websockets
    allow_anonymous false
    password_file /home/ubuntu/mqttBrokerPassword.txt
    tls_version tlsv1

    Many thanks for considering my question!
    Cheers,
    Ram

    1. The last will is sent immediately when the broker detects the failure.Om MQTT v5 you can delay the sending but you need that in the configuration file.
      The problem is that the broker might not detect the failure which I suspect is your case here. I have tried it over websockets but I have seen this problem over a standard MQTT connection.
      It is the keep alive that ultimately closes the connection.
      The last will is useful but I wouldn’t rely on it completely.
      Rgds
      Steve

      1. got it! I’ve added the keepalive and set it to 15 seconds, so far it looks like it does the job well, for now its more than enough for our POC.

        once again, your feedback is massively appreciated!!
        I wish you nothing but the best!!
        RS

Leave a Reply

Your email address will not be published. Required fields are marked *