MQTT – Message Queue Telemetry Transport
MQTT — what it is, pub/sub, REST, and architectures. Learn the benefits and how to use it!
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.
- 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.
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.
- MQTT is payload agnostic
- A simple byte array
- A simple string
- Or a JSON
PUBLISH to home/livingroom/light/1 message
- SSL/TLS support
- Encrypt payload (data/payload agnostic)
- IoT security should not be underestimated!
- SSL/TLS is a must-have
Broker and Clients
- Azure IoT Hub
- Eclipse Paho
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.
Both Publisher and Subscriber connect to the Broker.
Here is the code for SimulatePublish method:
Following is a screenshot of running the solution.
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
Published at DZone with permission of Jawad Hasan Shani. See the original article here.
Opinions expressed by DZone contributors are their own.