DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. IoT
  4. A Look at MuleSoft's MQTT Connector

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.

Arun VS user avatar by
Arun VS
·
May. 29, 17 · Tutorial
Like (1)
Save
Tweet
Share
7.52K Views

Join the DZone community and get the full member experience.

Join For Free

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>


MQTT Connector (mathematics)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Create a Real-Time Scalable Streaming App Using Apache NiFi, Apache Pulsar, and Apache Flink SQL
  • Better Performance and Security by Monitoring Logs, Metrics, and More
  • Kubernetes vs Docker: Differences Explained
  • How To Check Docker Images for Vulnerabilities

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: