Getting started Community Training Tutorials Documentation APIs, AI & Tools
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.
In Studio, navigate to the Global Elements tab.
Click Create.
In the filter box, type mqtt and select MQTT3 Config.
Click OK.
In MQTT3 Config > Connection, select MQTT3 Form Connection.
For Client id generator, select Client id random suffix generator.
Set the following fields:
Client ID: smart-bentley-123
Username: usertest
Password: passtest
URL: tcp://127.0.0.1:1883
Click LWT.
Set the following fields:
Topic: LWT
Body: Testing body message
Is retained: True.
Click OK.
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>