Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A Look at MuleSoft's MQTT Connector

DZone's Guide to

A Look at MuleSoft's MQTT Connector

MuleSoft's MQTT connector makes it easy to set up communications for your IoT projects. Let's check out its benefits and how to get it implemented.

· IoT Zone
Free Resource

If you've worked with IoT at all, you're probably familiar with MQTT. As a simple reminder, MQTT is protocol best suited for low-power IoT devices.

Image title

Features of MQTT

  • Lightweight, bandwidth efficient, and suitable for low-power devices

  • Simple

  • QoS data delivery (at most once, at least once, and exactly once QoS data delivery)

  • Continuous session awareness

MQTT Broker Supports

  • Interoperability

  • Security (authentication and authorization, payload encryption and signing, topic permissions)

  • Scaling

    • Bridging (parallel processing)

    • Clustering (high availability)

    • Publish-Subscribe design pattern (topic-based flexible routing)

    • Subject-based filtering

Publish a Message

The MQTT connector requires a client id to identify its client. The Broker Server URI is mentioned as"tcp://localhost:1883". Before deploying the application, start the Mosquitto Server that resides in the local machine (MQTT broker).

Image title


To start an MQTT broker:

  • Open Services in Windows (in the Star menu, search for "Services")

  • Locate the installed server "Mosquitto Broker" and change the status to "Running"

Image title

Subscribe a Message

If a message is subscribed for a topic, then whenever a message is posted for a topic, the broker sends the message to the other interested clients that subscribed.

Image title

Code Snippet

For those who are curious about how MQTT achieved through Mule ESB, in "Anypoint Studio", an embedded Mule Server, paste the following code and test it. A basic MQTT client and server communication is shown below

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:mqtt="http://www.mulesoft.org/schema/mule/mqtt" xmlns:amqp="http://www.mulesoft.org/schema/mule/amqp" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/mqtt http://www.mulesoft.org/schema/mule/mqtt/current/mule-mqtt.xsd http://www.mulesoft.org/schema/mule/amqp http://www.mulesoft.org/schema/mule/amqp/current/mule-amqp.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8082" doc:name="HTTP Listener Configuration" />
    <mqtt:config name="MQTT" doc:name="MQTT" clientId="1235" lwtRetained="true">
        <mqtt:connection-pooling-profile initialisationPolicy="INITIALISE_ONE" exhaustedAction="WHEN_EXHAUSTED_GROW" />
    </mqtt:config>
    <flow name="MQTTPublish">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP" />
        <mqtt:publish config-ref="MQTT" topicName="/sample" doc:name="publishes MQTT message for topic /sample" clientId="1234" />
        <logger message="***********Published Success" level="INFO" doc:name="Prints Published Success" />
    </flow>
    <flow name="MQTTSubscribe">
        <mqtt:subscribe config-ref="MQTT" topicFilter="/sample" metadata:id="a8f64177-a868-43bd-9f25-eb6a037b5504" doc:name="Subscribes MQTT (Streaming) message for topic /sample" />
        <logger message=" #[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logs message that is published" />
        <set-payload value=" #[message.payloadAs(java.lang.String)]" doc:name="Set Payload" />
    </flow>
</mule>


Topics:
mulesoft ,mqtt ,mosquitto ,iot ,tutorial

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}