When the client disconnects, a will message is sent to the relevant subscriber. Will Messages will be sent when:
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)
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.
Bit | 7 | 6 | 5 | 4 | 2 | 1 | 0 |
---|---|---|---|---|---|---|---|
User Name Flag | Password Flag | Will Retain | Will QoS | Will Flag | Clean Start | Reserved | |
byte 8 | X | X | X | X | X | X | X |
The will message is not sent after the client calls the disconnect method normally.
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.
Here is an example of Will Message:
Sub side ClientID = sub predefined will message:
mosquitto_sub --will-topic test --will-payload die --will-qos 2 -t topic -i sub -h 192.168.1.1
clientid = alive subscribes to the will topic at 192.168.1.1 (EMQ server)
mosquitto_sub -t test -i alive -q 2 -h 192.168.1.1
Abnormally disconnect the sub end from the server end (EMQ server), and the pub end receives the will message.
Here's how to use Retained messages with Will messages.
A/status
that is the same as the topic of a normal sending status;A/status
. When other clients subscribe to the topicA/status
, they obtain the Retained message as "online";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 ”.