MQTT Protocol
MQTT Protocol
Characteristics of MQTT
Scalable
MQTT implementation requires a minimal amount of code that consumes very little power in
operations. The protocol also has built-in features to support communication with a large number of
IoT devices. Hence, you can implement the MQTT protocol to connect with millions of these devices.
Reliable
Many IoT devices connect over unreliable cellular networks with low bandwidth and high latency.
MQTT has built-in features that reduce the time the IoT device takes to reconnect with the cloud. It
also defines three different quality-of-service levels to ensure reliability for IoT use cases— at most
once (0), at least once (1), and exactly once (2).
Secure
MQTT makes it easy for developers to encrypt messages and authenticate devices and users using
modern authentication protocols, such as OAuth, TLS1.3, Customer Managed Certificates, and
more.
Well-supported
Several languages like Python have extensive support for MQTT protocol implementation. Hence,
developers can quickly implement it with minimal coding in any type of application.
History of MQTT
The MQTT protocol was invented in 1999 for use in the oil and gas industry. Engineers needed a
protocol for minimal bandwidth and minimal battery loss to monitor oil pipelines via satellite. Initially,
the protocol was known as Message Queuing Telemetry Transport due to the IBM product MQ
Series that first supported its initial phase. In 2010, IBM released MQTT 3.1 as a free and open
protocol for anyone to implement, which was then submitted, in 2013, to Organization for the
Advancement of Structured Information Standards (OASIS) specification body for maintenance. In
2019, an upgraded MQTT version 5 was released by OASIS. Now MQTT is no longer an acronym
but is considered to be the official name of the protocol.
MQTT Architecture
MQTT runs on top of TCP/IP using a PUSH/SUBSCRIBE topology. In MQTT architecture,
there are two types of systems: clients and brokers. A broker is the server that the
clients communicate with. The broker receives communications from clients and sends
those communications on to other clients. Clients do not communicate directly with
each other, but rather connect to the broker. Each client may be either a publisher, a
subscriber, or both.
○ Message
○ Client
○ Server or Broker
○ TOPIC
Message
The message is the data that is carried out by the protocol across the network for the
application. When the message is transmitted over the network, then the message
contains the following parameters:
1. Payload data
3. Collection of Properties
4. Topic Name
Client
In MQTT, the subscriber and publisher are the two roles of a client. The clients subscribe
to the topics to publish and receive messages. In simple words, we can say that if any
program or device uses an MQTT, then that device is referred to as a client. A device is a
client if it opens the network connection to the server, publishes messages that other
clients want to see, subscribes to the messages that it is interested in receiving,
unsubscribes to the messages that it is not interested in receiving, and closes the
network connection to the server.
● Publish: When the client sends the data to the server, then we call this operation
as a publish.
● Subscribe: When the client receives the data from the server, then we call this
operation a subscription.
Server
The device or a program that allows the client to publish the messages and subscribe to
the messages.
A server
TOPIC
The label provided to the message is checked against the subscription known by the
server is known as TOPIC.
2. Once connected, the client can either publish messages, subscribe to specific messages, or
do both.
3. When the MQTT broker receives a message, it forwards it to subscribers who are interested.
To understand it more clearly, we will look at the example. Suppose a device has a
temperature sensor and wants to send the rating to the server or the broker. If the phone
or desktop application wishes to receive this temperature value on the other side, then
there will be two things that happened. The publisher first defines the topic; for example,
the temperature then publishes the message, i.e., the temperature's value. After
publishing the message, the phone or the desktop application on the other side will
subscribe to the topic, i.e., temperature and then receive the published message, i.e., the
value of the temperature. The server or the broker's role is to deliver the published
message to the phone or the desktop application.
How to Use MQTT in Home
Automation and IoT Projects
MQTT is great for home automation and internet of things projects. Here’s an
example of how it can be used in a Home Automation System built with
low-cost development boards like a Raspberry Pi, ESP32, ESP8266, and
Arduino.