Use of MQTT Will Message

EMQ Technologies
3 min readDec 10, 2019

--

Overview

When the client disconnects, a will message is sent to the relevant subscriber. Will Messages will be sent when:

  • I / O error or network failure occurred on the server;
  • The client loses contact during a defined heartbeat period;
  • The client closes the network connection before sending offline packets;
  • The server closes the network connection before receiving the offline packet.

Will messages are usually specified when the client is connected. As shown below, it is set during the connection by calling the setWill method of theMqttConnectOptions instance. Any client who subscribes to the topic below will receive the will message.

//method1 
MqttConnectOptions.setWill(MqttTopic topic, byte[] payload, int qos, boolean retained)
//method2
MqttConnectOptions.setWill(java.lang.String topic, byte[] payload, int qos, boolean retained)

Usage scenarios

When client A connects, the will message is set to “offline” and client B subscribes to this will topic. When A disconnects abnormally, client B will receive this will message of “offline” to know that client A is offline.

Connect Flag packet field

The will message is not sent after the client calls the disconnect method normally.

Will flag function

In short, it is the last will (also known as the Testament) that the client has defined in advance and left when it is disconnected abnormally. This will is a topic and a corresponding message pre-defined by the client, which is attached to the variable packet header of CONNECT. In case of abnormal connection of the client, the server actively publishes this message.

When the bit of Will Flag is 1, Will QoS and Will Retain will be read. At this time, the specific contents of Will Topic and Will Message will appear in the message body, otherwise the Will QoS and Will Retain will be ignored.

When the Will Flag bit is 0, Will Qos and Will Retain are invalid.

Command line example

Here is an example of Will Message:

Advanced usage scenarios

Here’s how to use Retained messages with Will messages.

Here’s how to use Retained messages with Will messages.

  1. The will message of client A is set to “offline”, and the topic of the will is set to A/status that is the same as the topic of a normal sending status;
  2. When client A is connected, send the “Online” Retained message to the topic A/status. When other clients subscribe to the topicA/status, they obtain the Retained message as "online";
  3. When client A disconnects abnormally, the system automatically sends an “offline” message to the topic A/status. Other clients that subscribe to this topic will immediately receive an" offline "message; if the will message is set Retained, and when a new client subscribing to the A/status topic comes online, the message obtained is“ offline ”.

Welcome to our open source project github.com/emqx/emqx. Please visit the documentation for details.

--

--

EMQ Technologies
EMQ Technologies

Written by EMQ Technologies

EMQ is an open-source IoT data infrastructure software provider, delivering the world’s leading open-source MQTT message broker and stream processing database.

No responses yet