Link Search Menu Expand Document

Quick Start!

The following contains a simple example on how to add the connector to your project, connect to a broker, subscribe to a topic and ultimately publish messages to it using the MQTT 3.

Add the MQTT connector to your project

Include the following Maven dependency:

<dependency>
    <groupId>desi.juan</groupId>
    <artifactId>mule-mqtt-connector</artifactId>
    <version>1.0.0</version>
    <classifier>mule-plugin</classifier>
</dependency>

See the Installation section for more information.

Connecting to the broker

First, let’s connect to an actual broker. The following example shows how to connect to the test.mosquitto.org broker, which is a publicly available MQTT broker.

To achieve this, let’s set up the host with the following value: test.mosquitto.org, like this:

Connection Setup

  • quickstart-connection

  • <mqtt:config name="mosquitto-config">
    	<mqtt:mqtt3-connection host="test.mosquitto.org"/>
    </mqtt:config>
    

Click the Test Connection button to validate that the connection is working. You should see a message like this one:

quickstart-test-connection

Subscribing to a topic

Now that the connectivity execution test to the broker was successful, you can setup a subscription. To subscribe to a tooic let’s add the MQTT connector On New Message source and add a Subscription to the test/mule/topic topic. Finally, add a logger after the source to log to the console all incoming messages.

Subscribe Flow

  • quickstart-sub

  • <flow name="OnNewMessage">
        <mqtt:listener config-ref="mosquitto-config">
            <mqtt:subscriptions>
                <mqtt:subscription topicFilter="test/mule/topic"/>
            </mqtt:subscriptions>
        </mqtt:listener>
        <logger level="INFO" message="#[payload]"/>
    </flow>
    

Publishing to the subscribed topic

Similar to subscribing, once you have a connected configuration, you can also publish messages to the recently subscribed topic using the same configuration.

For this example, let’s publish to the topic that we subscribed in the previous step. Let’s create a new flow with a scheduler to trigger the flow and after that add the MQTT connector Publish operation. Once all processors are added, change the message property from the Publish operation to Hello World!.

Publish Flow

  • quickstart-pub

  • <flow name="PublishFlow">
        <scheduler>
            <scheduling-strategy>
                <fixed-frequency/>
            </scheduling-strategy>
        </scheduler>
        <mqtt:publish config-ref="mosquitto-config" topic="test/mule/topic">
            <mqtt:message>#['Hello world!']</mqtt:message>
        </mqtt:publish>
    </flow>
    

Run the application

Now go and run your app! You’ll start to see log messages from the subscription flow that are being published by your publish flow.

That’s all!