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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Overview of Telemetry for Kubernetes Clusters: Enhancing Observability and Monitoring
  • OPC-UA and MQTT: A Guide to Protocols, Python Implementations
  • A Developer's Guide to Modern Queue Patterns
  • Security Considerations for Observability: Enhancing Reliability and Protecting Systems Through Unified Monitoring and Threat Detection

Trending

  • Memory Leak Due to Time-Taking finalize() Method
  • Docker Model Runner: Streamlining AI Deployment for Developers
  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  • Designing a Java Connector for Software Integrations
  1. DZone
  2. Data Engineering
  3. IoT
  4. MQTT – Message Queue Telemetry Transport

MQTT – Message Queue Telemetry Transport

MQTT — what it is, pub/sub, REST, and architectures. Learn the benefits and how to use it!

By 
Jawad Hasan Shani user avatar
Jawad Hasan Shani
DZone Core CORE ·
Apr. 24, 20 · Analysis
Likes (9)
Comment
Save
Tweet
Share
17.9K Views

Join the DZone community and get the full member experience.

Join For Free

What Is MQTT

  • A message protocol with “a small code footprint and on-the-wire footprint”.
  • MQTT is a publish-subscribe-based messaging protocol.
  • On top of TCP/IP.
  • Requires a broker (e.g. mosquito, hivemq, azure IO Hub).
  • ISO standard (ISO/IEC PRF 20922).
  • A message bus for: unreliable, high latency, low bandwidth
  • Payload with a plain byte array.

MQTT PUB/SUB

  • The protocol uses a publish/subscribe architecture in contrast to HTTP with its request/response paradigm.
  • Publish/Subscribe is event-driven and enables messages to be pushed to clients.
  • The central communication point is the MQTT broker, it is in charge of dispatching all messages between the senders and the rightful receivers.
  • Each client that publishes a message to the broker, includes a topic into the message. The topic is the routing information for the broker.
  • Each client that wants to receive messages subscribes to a certain topic and the broker delivers all messages with the matching topic to the client.
  • Therefore the clients don’t have to know each other, they only communicate over the topic.
  • This architecture enables highly scale-able solutions without dependencies between the data producers and the data consumers.

… and What Is With REST?

  • HTTP/REST is useful to handle documents and resources.
  • MQTT is useful to handle messages.
  • HTTP/REST can be complex and is not always the best solution for simple messages.
  • The MQTT packet size is 2 byte + payload.
  • MQTT supports 1-to-1, 1-to-many, and many-to-many messages.
  • Request and response vs publisher and subscriber.

Architecture

The difference to HTTP is that a client doesn’t have to pull the information it needs, but the broker pushes the information to the client, in case there is something new.

Therefore each MQTT client has a permanently open TCP connection to the broker. If this connection is interrupted by any circumstances, the MQTT broker can buffer all messages and send them to the client when it is back online.

As mentioned before the central concept in MQTT to dispatch messages is topics. A topic is a simple string that can have more hierarchy levels, which are separated by a slash.

A sample topic for sending temperature data of the living room could be house/living-room/temperature.

On the one hand, the client can subscribe to the exact topic or on the other hand use a wildcard. The subscription to house/+/temperature would result in all message send to the previously mention topic house/living-room/temperature as well as any topic with an arbitrary value in the place of living room, for example, house/kitchen/temperature

The plus sign is a single level wild card and only allows arbitrary values for one hierarchy. If you need to subscribe to more than one level, for example to the entire subtree, there is also a multilevel wildcard (#). It allows subscribing to all underlying hierarchy levels. For example, house/# is subscribing to all topics beginning with house.

Payload

  • MQTT is payload agnostic
    • A simple byte array
    • A simple string
    • Or a JSON

PUBLISH to home/livingroom/light/1 message

PUBLISH

Security

  • SSL/TLS support
  • Username/Password
  • Encrypt payload (data/payload agnostic)
  • IoT security should not be underestimated!
  • SSL/TLS is a must-have

Broker and Clients

  • Mosquitto
  • HivenMQ
  • Azure IoT Hub
  • MQTT.fx
  • Eclipse Paho
  • MQTTnet

Code Sample

I have to build a sample .NET Core console application to test the library. The following are code screenshots, which are self-explanatory and you can download the code from git if needed.

The solution contains three projects as follows and all of the projects have a reference to MQTTnet.

mqtt basics

Broker

Both Publisher and Subscriber connect to the Broker.

Publisher and Subscriber

Publisher

publisher

Here is the code for SimulatePublish method:

SimulatePublish method

Subscriber

subscriber

Execution

Following is a screenshot of running the solution.

Publisher and Subscribers

Publishers and Subscribers are connected to Broker.

Publisher/Subscriber/Broker in action

Publisher/Subscriber/Broker in action.

Summary

This was a very basic introduction to MQTT and its usage. To keep the discussion simple, I kept the code to very minimal. Publisher/Subscriber pattern is very powerful and allows us to create decoupled application easily and use of MQTTNet library make it very easy to implement this pattern in our applications. You can download the sample from my git repository on the source code link below. Also, I will suggest checking the links in reference for more insight. Till next time, Happy Coding.

Git Repository Link

>>git clone https://github.com/jawadhasan/mqttBasic.git

References

  • https://www.hivemq.com/blog/how-to-get-started-with-mqtt/
  • https://github.com/chkr1011/MQTTnet/wiki/Client
MQTT Message queue Telemetry

Published at DZone with permission of Jawad Hasan Shani. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Overview of Telemetry for Kubernetes Clusters: Enhancing Observability and Monitoring
  • OPC-UA and MQTT: A Guide to Protocols, Python Implementations
  • A Developer's Guide to Modern Queue Patterns
  • Security Considerations for Observability: Enhancing Reliability and Protecting Systems Through Unified Monitoring and Threat Detection

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!