Contact Us 1-800-596-4880

Configure the Last Will and Testament Messages for MQTT Connector

In this example, you set a Last Will and Testament (LWT) message for Anypoint Connector for MQTT (MQTT Connector) to ensure that a message is sent to all of the subscribers of a specific LWT topic if the client disconnects temporarily or permanently from the broker. The LWT message is sent when the connection to the broker is established.

Configure the LWT message in the configuration element and specify whether or not you want the broker to retain that message. If an MQTT message is set for retention, the broker stores the last message marked as retained that arrived at a specific topic. If no listeners are subscribed to a matching topic at the time the message is sent, the broker stores the message until a listener connects to that topic. Then, the broker delivers the listener to the last message retained for that topic.

  1. In Studio, navigate to the Global Elements tab.

  2. Click Create.

  3. In the filter box, type mqtt and select MQTT3 Config.

  4. Click OK.

  5. In MQTT3 Config > Connection, select MQTT3 Form Connection.

  6. For Client id generator, select Client id random suffix generator.

  7. Set the following fields:

    • Client ID: smart-bentley-123

    • Username: usertest

    • Password: passtest

    • URL: tcp://127.0.0.1:1883

  8. Click LWT.

  9. Set the following fields:

    • Topic: LWT

    • Body: Testing body message

    • Is retained: True.

  10. Click OK.

MQTT Config window with Last Will and Testament configuration window selected

In the Configuration XML editor, the <mqtt3:last-will-and-testament> configuration looks like this:

<mqtt3:config name="MQTT_LWT_Config">
    <mqtt3:connection username="usertest" password="passtest" url="tcp://127.0.0.1:1883">
        <mqtt3:client-id-generator >
            <mqtt3:client-id-random-suffix-generator clientId="smart-bentley-123" />
        </mqtt3:client-id-generator>
        <mqtt3:last-will-and-testament topic="LWT" isRetained="true" >
            <mqtt3:body ><![CDATA[Testing body message]]></mqtt3:body>
        </mqtt3:last-will-and-testament>
    </mqtt3:connection>
</mqtt3:config>
View on GitHub