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 the 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 are 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 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 to subscribe 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
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 build a sample .NET Core console application to test the library. Following are code screenshots, which are self explanatory and you can download the code from git if needed.
Solution contains 3 projects as follows and all of the projects have a reference to MQTTnet.
Broker
Both, Publisher and Subscriber connects to Broker.
Publisher
Here is the code for SimulatePublish method:
Subscriber
Execution
Following is screenshot of running the solution.
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 to check 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
Discover more from Hex Quote
Subscribe to get the latest posts sent to your email.