Will Message

EMQX implements the will message feature of MQTT. If a will message is set for a client, EMQX sends the message to relevant subscribers when the client is accidentally disconnected, so that the subscribers can be informed and update the client status.

You can use client tools to try this messaging service in EMQX. This section introduces how to use the MQTTX ClientWill Message - 图1 (opens new window) and MQTTX CLIWill Message - 图2 (opens new window) to simulate clients and see how a will message is published and received.

Prerequisites

  • Knowledge about MQTT Will Message
  • Basic publishing and subscribing operations using MQTTX

Publish Will Message with MQTTX Client

  1. Start the MQTTX Client. Click the New Connection to create an MQTT connection named Demo.

    • The localhost 127.0.0.1 is used as an example in this demonstration.

    TIP

    For detailed instructions on creating an MQTT connection, see MQTTX Client.

    Configure-new-connection-general

    In Last Will and Testament section, fill in the will message configuration.

    • Last-Will Topic: Type offline.
    • Last-Will QoS: Set as the default value 0.
    • Last-Will Retain: Set disabled as default. If enabled, the will message will also be a retained message.
    • Last-Will Payload: Type I'm offline.
    • Will Delay Intervals (s): Set 5 seconds.

    Configure-new-connection-will

    Leave the rest settings as default. Click the Connect button.

  2. Create a new connection for another client. Type the connection name as Subscriber.

  3. Click New Subscription in the Subscribe pane. Type offline in the Topic textbox. Leave the other settings as default. Click the Confirm button.

    Subscribe-will-message

  4. Select the client connection named Demo in the Connections pane. Right-click and select New Window. In the new window, click the Connect button.

    Open-new-window

  5. Close the new window and wait for 5 seconds. The client Subscriber receives a will message I'm offline.

    Receive-will-message

Publish Will Message with MQTTX CLI

  1. Initiate a connection request with one client. Set the topic to t/1 and payload to A will message from MQTTX CLI:

    1. $ mqttx conn -h 'localhost' -p 1883 --will-topic 't/1' --will-message 'A will message from MQTTX CLI'
    2. Connected
  2. Subscribe to topic t/1 with another client for receiving the will messages:

    1. mqttx sub -t 't/1' -h 'localhost' -p 1883 -v
  3. Disconnect the client specified in step 1, then the client specified in step 2 will receive the will message:

    1. topic: t/1
    2. payload: A will message from MQTTX CLI