Will Message

Updated at:
Copy as MD

A Will Message is a notification that the MQTT broker automatically publishes when a client disconnects unexpectedly. When a client connects, it registers a Will Message with the broker. If that client later disconnects without sending a clean DISCONNECT packet -- for example, due to a power outage or network failure -- the broker publishes the message to a specified topic, alerting subscribers to take action.

How a Will Message works

When a client sends a CONNECT packet to the MQTT broker, it includes a Will Message with three fields:

  • Will Topic -- the topic the broker publishes the message to.

  • Will Payload -- the message content (for example, offline or disconnected).

  • Will QoS -- the Quality of Service (QoS) level for delivery.

The broker stores the Will Message as part of the client's session state. If the client later disconnects without sending a clean DISCONNECT packet, the broker publishes the Will Message to the specified topic.

When does the broker publish the Will Message?

The broker publishes the Will Message when the network connection closes and the broker has not received a DISCONNECT packet with a Reason Code of 0x00 (normal disconnection). This happens when:

  1. The broker detects an I/O error or network failure.

  2. The client fails to communicate within the Keep Alive period.

  3. The client closes the connection without first sending a DISCONNECT packet.

  4. The broker closes the connection due to a protocol error, without first receiving a DISCONNECT packet.

In summary, the broker publishes the Will Message whenever the network connection closes without a DISCONNECT packet that has Reason Code 0x00.

When the Will Message is not published: If the client sends a DISCONNECT packet with Reason Code 0x00 before closing the connection, the broker discards the Will Message. This is the normal, clean disconnection flow.

MQTT 5.0 enhancements

MQTT 5.0 introduces Will Message properties for finer control over delivery timing and payload metadata.

Will Delay Interval

By default, the broker publishes the Will Message immediately on disconnection. The Will Delay Interval property specifies a delay (in seconds) before publication. During this window, the client can reconnect and prevent the Will Message from being sent. This avoids false alerts caused by brief network interruptions.

The interaction between the Will Delay Interval and Session Expiry Interval determines when the Will Message is published:

Scenario

Outcome

Will Delay Interval = 0

Will Message is published immediately on unexpected disconnection.

Will Delay Interval > 0, client reconnects with Clean Start = 0 before the interval expires

Will Message is canceled; the session resumes.

Will Delay Interval > 0, client reconnects with Clean Start = 1 before the interval expires

Will Message is published immediately because Clean Start = 1 ends the existing session.

Will Delay Interval > 0, interval expires before Session Expiry Interval

Will Message is published when the Will Delay Interval expires.

Session Expiry Interval expires before Will Delay Interval

Will Message is published when the session expires, even if the Will Delay Interval has not elapsed.

The Will Message is part of the session state. When a session ends -- whether by expiry or by a new connection with Clean Start = 1 -- the broker publishes the Will Message before discarding the session, even if the Will Delay Interval has not fully elapsed.

Additional Will Message properties

Property

Purpose

Content Type

Describes the payload format (for example, application/json), so recipients can parse the message correctly.

Payload Format Indicator

Indicates whether the payload is UTF-8 text or a binary byte stream.

Message Expiry Interval

Sets how long the Will Message remains valid after publication.

Response Topic

Specifies a topic for recipients to publish a response to.

Correlation Data

Links a response back to the original Will Message for request-response workflows.

User Properties

Carries custom key-value pairs for application-specific metadata.

Use cases

Device monitoring (IoT)

When an IoT device disconnects due to a network failure or hardware fault, the Will Message notifies the monitoring system. The system can then trigger an alarm, attempt to reconnect, or activate a backup device.

Online/offline status tracking

Combine a Will Message with retained messages to maintain real-time device status:

  1. On connection, publish a retained message with payload online to a status topic (for example, devices/sensor1/status).

  2. Set a Will Message with payload offline on the same topic, with the retain flag enabled.

  3. If the client disconnects unexpectedly, the broker publishes offline as the new retained message.

  4. Any client that subscribes to the status topic -- even after the disconnection -- receives the current status.

Last state update

In a home automation system, a smart device sets a Will Message to update its last known state if it disconnects. Other devices or controllers that subscribe to the status topic can detect the disconnection and respond accordingly.

System fault recovery

In a distributed system, if a component exits abnormally, the Will Message notifies other components to initiate failover or reroute requests.

Subscriber notifications

In messaging applications, the Will Message notifies other users when someone goes offline unexpectedly.

Example: smart irrigation sensor

This example shows a soil moisture sensor that uses a Will Message to notify a control server when it loses connectivity. All steps use MQTT packet pseudocode.

Step 1: The sensor connects and sets a Will Message.

CONNECT
  ClientId: sensor1
  Will Topic: home/garden/sensor1/status
  Will Message: disconnected
  Will QoS: 1
  Will Delay Interval: 10    // Wait 10 seconds before publishing
  Session Expiry Interval: 60

The sensor also publishes a retained message to confirm it is online:

PUBLISH
  Topic: home/garden/sensor1/status
  Payload: connected
  Retain: true

Step 2: The control server subscribes to the status topic.

SUBSCRIBE
  Topic: home/garden/sensor1/status

The control server immediately receives the retained message connected.

Step 3: The sensor loses connectivity.

If the sensor disconnects without sending a DISCONNECT packet (for example, due to a power outage or network failure), the broker waits 10 seconds (the Will Delay Interval). If the sensor does not reconnect within that window, the broker publishes:

PUBLISH
  Topic: home/garden/sensor1/status
  Payload: disconnected
  Retain: true

Step 4: The control server receives the Will Message and takes action -- for example, sending an alert, attempting to re-establish the connection, or switching to a backup sensor.

Limits

Item

Description

Supported editions

Will Message is available only for the Professional Edition and Platinum Edition.

Activation

To enable this feature, submit a ticket.