How IOT communicates (MQTT protocols)
Abhishek Jaiswal
SDE @Turtlemint | ex-SDE Intern'22 @Truminds | Microsoft Certified | Linux Certified
This series is going to be a comprehensive yet accessible guide to understanding how IoT devices communicate, breaking down complex concepts into easy-to-follow steps for both beginners and experienced professionals.
How IOT communicate with the user ?
The IoT communication process involves five key components, they are:
We'll not be deeply involved into the hardware aspects; instead, we'll focus on the software side, including machine-to-machine communication and the protocols involved.
IoT Communication Protocols
Some widely used IoT communication protocols are MQTT (Message Queuing Telemetry Transport), CoAP (Constrained Application Protocol), HTTP (Hypertext Transfer Protocol), and Zigbee. There are various advantages/disadvantages of one over another, but in this article we will be covering the MQTT protocols and moving with the same.
Advantage of MQTT over HTTP is as below :
Getting Started with MQTT protocol
Imagine MQTT as a TV broadcaster transmitting a program on a specific channel, while the viewer tunes in to that channel to watch. There's no direct connection between the broadcaster and the viewer.
In short, MQTT is a messaging protocol designed for transferring messages using a publish-subscribe model. This model allows messages to be sent to 0, 1, or N clients. In MQTT, a publisher sends messages on a specific topic, and a subscriber must subscribe to that topic to receive the message.
Important Points to Note
MQTT Client-Broker Connections
MQTT Client-Broker Connections can be defined in terms of Quality of Service (QoS). MQTT provides three levels of QoS:
1. At most once (QoS 0) :
This provides a best-effort delivery mechanism where the sender neither expects an acknowledgment nor guarantee delivery of message. In this setup, the recipient doesn't confirm receipt of the message, and the sender doesn't store or re-transmit it. It is often referred to as "fire and forget" operates similarly to the underlying TCP protocol, where the message is sent without any follow-up or confirmation.
2. At least once (QoS 1) :
The emphasis is on guaranteeing that a message is delivered to the receiver at least once.
If the sender republishes the same message, it includes a duplicate (DUP) flag. This flag is for internal tracking and is not processed by the broker or client. Despite the DUP flag, the receiver will still send a PUBACK packet to confirm receipt, ensuring the sender is aware of successful delivery.
3. Exactly once (QoS 2) :
QoS 2 provides the highest level of service in MQTT by ensuring that each message is delivered exactly once to the intended recipients. It involves a four-step handshake between the sender and receiver:
If the sender does not receive the PUBREC packet within a reasonable time, it will retransmit the PUBLISH packet with the duplicate (DUP) flag until acknowledgment is received. The receiver retains a reference to the packet identifier of the original PUBLISH packet until the PUBCOMP packet is sent, ensuring that the message is not processed more than once.
Client ID (Client Name)
Each client must have a unique client name or ID.
The MQTT broker uses this client name to manage subscriptions and other functions.
If you try to connect to an MQTT broker using a client name that is already in use, the existing client's connection will be dropped. Since most MQTT clients will attempt to reconnect after being disconnected, this can create a cycle of disconnections and reconnections.
The screenshot below illustrates what occurs when you attempt to connect a client with the same client ID as one that is already connected to the broker.
Last Will Messages (LWM)
The last will and testament message is used to notify subscribers of an unexpected shut down of the publisher.
The basic process is.
Attached is the comprehensive article about Last Will Message. Feel free to read this amazing article from steves-internet-guide
Clean Sessions
By default, MQTT clients establish a clean session with the broker.
In a clean session, the broker does not retain any information about the client after it disconnects.
In contrast, a non-clean session allows the broker to remember client subscriptions and potentially store undelivered messages.
Whether or not messages are held depends on the Quality of Service (QoS) levels used when subscribing to and publishing to topics.
Attached is the comprehensive article about Clean Sessions. Feel free to read this amazing article from steves-internet-guide
Stay tuned for 2 more upcoming articles on MQTT protocols and forget not to subscribe to this newsletter.