Every IoT network is a collaborative working of edge devices. All devices should work in unison to make the environment conducive for the humans. This article helps you make the best use of the MQTT Last WILL Messages.

Last Will is a way of peer to peer monitoring between MQTT clients that operate in a more understanding way. Before we go into the real time use cases, it is better to understand the spec better.

What is the MQTT Last WILL ?

Last Will is a message stored in the MQTT broker specific to particular clients. The Last Will message is a normal MQTT Message that has a will topic, will message , QoS and retain flag. These messages will be sent to the clients who have subscribed to those WILL topics by the broker when the particular client disconnects ungracefully.

The Last WILL Message should be sent to the broker when the clients connect to the broker via the Connect packet.

A more detailed explanation of how you have to send the connect packet is available here.

When it will be published ?

The WILL Messages will be published by the broker to all the interested subscribers when the particular client gets disconnected from the broker ungracefully. When the client disconnects using a proper disconnect message, the WILL will not be published. The client needs to notify the interesting parties on going down by publishing proper message by itself.

Putting it to use

All mission critical devices needs a back up. Let us take an example of a Database server in an online application. It is one of the core component that needs to run always for the complete service to be useful.

These applications will have masters , slaves and standby servers which needs to be deployed when any of the servers fails. The master server should add a WILL on Connection. The slave will be subscribing to the particular WILL. The WILL should be configured specific to each cluster of the master slave. In this scenario, the broker should be configured to publish the WILL for the graceful as well as ungraceful disconnection of the Master. When the master disconnects, the slave receives the master unavailable message and the necessary scripts can be triggered for the slave to become the master. The standby servers will  become slave and now will subscribe to the WILL of the new master.

This is one scenario of automated take over. Such scenarios exist in all industries like Medical, all kind of Manufacturing industries. We can also use the WILL Message triggers for monitoring the devices and their uptime from a central server.

You can avoid a lot of unwanted monitoring messages to each devices by making the right use of MQTT Last WILL Messages.