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.
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.
All mission critical devices needs a backup. 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.