All Products
Search
Document Center

ApsaraMQ for MQTT:Will Message

Last Updated:Nov 13, 2025

This topic describes the Will Message feature of Message Queuing Telemetry Transport (MQTT).

Feature description

When a client connects to an MQTT broker, it can provide a Will Message. This message includes information such as the content, topic, and Quality of Service (QoS) level. The broker stores this information. If the client disconnects abnormally, for example, due to a power outage or network failure, the broker automatically publishes the Will Message to the specified topic.

The MQTT 5.0 protocol enhances the Will Message feature with the following properties:

  • Will Delay Interval: A Will Message can include a delay interval property. This property lets you specify a period for the broker to wait before it publishes the Will Message. This delay gives the client a chance to reconnect and prevents the unnecessary publication of the Will Message.

  • Content Type: A Will Message can include a content type property that describes its payload. This helps the recipient understand the intent of the message and process it correctly.

  • Will Message Properties: A Will Message can include custom properties, such as a message expiry interval, a will delay interval, and request-response information. This increases the flexibility and descriptiveness of the Will Message.

  • Response Topic and Correlation Data: A Will Message can include a response topic and correlation data. The response topic specifies the topic to which the recipient of the Will Message should publish a response. Correlation data is used to maintain state or link the context between the original request and the response.

  • Payload Format Indicator: This property indicates whether the payload of the Will Message is structured data, such as JSON or XML, or an opaque byte stream. This allows the recipient to understand and process the message correctly.

  • User Properties: A Will Message can include user properties to convey application-specific or business-related information.

The MQTT 5.0 Will Message is more flexible and powerful. It allows clients and service providers to customize Will Message information and behaviors to meet the requirements of various application scenarios.

Limits

This feature is available only for the Professional and Platinum editions. To enable it, you can submit a ticket.

Scenarios

  • Device monitoring: In Internet of Things (IoT) applications, if a device disconnects unexpectedly because of network issues or device failure, a Will Message can notify a monitoring system or other devices. This allows them to take appropriate action, such as triggering an alarm or attempting to reconnect.

  • Subscriber notifications: In instant messaging applications, when a user disconnects unexpectedly, a Will Message can be used to notify other users that the user is offline.

  • System fault recovery: In a distributed system, if a component exits abnormally, a Will Message can notify other parts of the system to initiate a failover or reroute requests.

  • Last state update: In a home automation system, a smart device can set a Will Message to update its last known state if it disconnects.

The MQTT 5.0 Will Message provides a reliable way to handle abnormal client disconnections. This enhances the robustness and reliability of IoT applications.

Message example

For example, a sensor in a smart irrigation system connects to a control server using MQTT. When connecting, the sensor sets a Will Message. If the sensor loses its connection for any reason, such as a power outage or a network problem, the MQTT broker publishes the Will Message.

// The sensor connects to the MQTT broker and sets the Will Message.
CONNECT
ClientId: sensor1
Will Topic: home/garden/sensor1/status
Will Message: disconnected
Will Delay Interval: 10 // The broker publishes the Will Message after 10 seconds.
Session Expiry Interval: 60 // The session expiry interval is set to 60 seconds.
// Other CONNECT message information

If the sensor loses its connection without sending a DISCONNECT message, which indicates a normal disconnection, the MQTT broker publishes the following Will Message to the will topic after the specified delay interval:

Topic: home/garden/sensor1/status
Payload: disconnected

Other clients that subscribe to the will topic, such as the control server, receive this Will Message. The message notifies them that the sensor has disconnected. The control server can then take appropriate action, such as attempting to re-establish the connection, sending an alert, or switching to a backup sensor.