MQTT
MQTT
Characteristics of MQTT:
✓ It is a simple messaging protocol designed for the constrained devices and
with low bandwidth
✓ It is a machine-to-machine IoT connectivity protocol, which provides
communication between the devices.
✓ It is designed as a simple and lightweight messaging protocol that uses a
publish/subscribe system to exchange the information between the client and
the server.
✓ It does not require that both the client and the server establish a connection
at the same time.
✓ It provides faster data transmission, like how WhatsApp/messenger provides a
faster delivery. It's a real-time messaging protocol.
✓ It allows the clients to subscribe to the narrow selection of topics so that they
can receive the information they are looking for.
✓ This protocol is useful for the connection with the remote location where the
bandwidth is a premium and makes it easy for communication between
multiple devices.
25
MQTT Architecture
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
2. Quality of Service (QoS)
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. The device is
referred to as a client which 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.
In MQTT, the client performs two operations:
❖ 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.
26
Server: The device or a program that allows the client to publish the messages and
subscribe to the messages. A server accepts the network connection from the client,
accepts the messages from the client, processes the subscribe and unsubscribe
requests, forwards the application messages to the client, and closes the network
connection from the client.
TOPIC: The label provided to the message is checked against the subscription known
by the server is known as TOPIC.
27
✓ 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.
The MQTT uses the command and the command acknowledgment format, which
means that each command has an associated acknowledgment. As shown in the
above figure that the connect command has connect acknowledgment, subscribe
command has subscribe acknowledgment, and publish command has publish
acknowledgment. This mechanism is similar to the handshaking mechanism as in
TCP protocol.
28