What is MQTT and How MQTT Works?

This tutorial explains the MQTT protocol and how the MQTT protocol works? We will start with the introduction of this lightweight messaging protocol, its applications in IoT, and what are the main concepts to understand Message Queuing Telemetry Transport protocol.

MQTT example block diagram

IoT Introduction

Internet of Things is very popular now a day. In simple words, the Internet of things means that connecting physical devices over the internet. The basic idea of IoT (internet of things) is to connect physical devices over the internet in such a way that they can collect or exchange data with each other and these devices can be operated by any person. But one thing we should keep in mind that for exchanging and collecting data, the device from which data is to be sent or receive should be in online mode i.e. connected to the Internet.

Examples of some physical devices are refrigerators, smartphones, Sensors, or any other electronic device.

Why We need light Weight Messaging Protocol?

An increased number of affordable microcontrollers like Arduino and Raspberry Pi to name a few are used to enable cheap devices for measuring sensor data and then send it over the Internet. This block diagram shows how IoT devices share a message via a messaging protocol.

IOT Messaging Protocol block Diagram

In order to make these devices communicate with each other, we need a simple messaging protocol in which they can communicate to send/receive data from each other. Therefore, a language that enables IoT devices to communicate is known as a messaging protocol. In response, many message protocols have been introduced and MQTT is one of them.

MQTT Abstract Overview

We can send a message from a client(Publisher) to a client (subscriber) to control a lamp.

MQTT Publisher Subscriber ESP32 with raspberry pi broker 1

Similarly, we can a sensor value from a client and send it to a subscriber (client).

MQTT Publisher Subscriber ESP32 with raspberry pi broker 2

Between publisher and subscriber is a broker which mange messages. Because each publisher can send messages to more than one subscriber. A broker is responsible for filtering messages and sending it to all subscribers.

MQTT Introduction

  • MQTT is known as Message Queuing Telemetry Transport protocol.
  • It is a lightweight messaging protocol and helps resource constrained network clients with a simple communication mechanism.
  • Unlike, most messaging system, we don’t have to assign addresses to MQTT clients.
  • MQTT uses simple publish/subscribe communication based on a topic.
  • This protocol runs on top of TCP / IP in order to provide reliable data delivery.
MQTT Protocol Introdution

MQTT Protocol Main Components

In order to understand MQTT protocol, you should grasp these four main components conpents

MQTT Publisher/Subscriber

The first main concept in MQTT is a Publisher/ Subscriber system.  A publisher device publishes a message on a specific topic and Subscriber device subscribe to the topic which will be sent by a publisher. On top of that, more than one device can subscribe to the topic.

MQTT Publisher Subscriber Example

For exampe, according toa above figure:

  • Device1 is a publisher and Devices(2-3) are subscribers.
  • Let suppose, device1 publishes on a topic “ESP32/Temperature” and Devices(2-3) subscribe to the same topic.
  • Hence, Devices(2-3) are receivers and Device1 is a sender.

MQTT – Messages

It is data or commands which we want to share between IoT devicess connected via a MQTT protocol.

MQTT- Topic

Topics are also a third significant concept to understand MQTT. In this protocol, multiple devices can subscribe to a publisher to receive specific messages according to their register interest.  Topics define which device we want to publish a message.

In this example, “ESP32/Ouput” is a topic and any device can subscribe to this topic.

MQTT Publisher Subscriber ESP32 with raspberry pi broker 1

Note: Topics are case sensitive that means “ESP32/Ouput” is not equal to “ESP32/OUTPUT”.

MQTT Broker

Broker is a hub that performs data receiving and sending functionality from publisher to subscriber.  It receives messages from the publisher and decodes the message. After decoding the message, Broker sends the message to subscribed clients according to Topics.

As you can see in this diagram, central main component to publish/subscribe system is MQTT broker. Every device first sends message to the broker. After performing filtration on received data, it distribute command or message to each subscribed device.

MQTT Publisher Subscriber Example

How does MQTT Works?

As we mentioned earlier, the MQTT protocol enables resource constraint IoT devices to publish or send information about some topics to a server that functions as an MQTT message broker. The function of the broker is to push that piece of information to only those clients that are previously subscribed to that topic. In simple words, we can explain this basic idea as the publisher is responsible for generating and transmission of information to subscribers through a broker. The main function of a broker is to ensure security by checking the authorization of subscribers and publishers.

To explain the working of MQTT protocol we will divide the MQTT session into 4 stages such as connection, authentication, communication, and termination.

  • First of all, a TCP / IP connection is initiated from client to broker by using a standard or custom port which is defined by a broker’s operation.
  • While establishing a connection, it’s important to recognize that whether the server has continued an old session or a new session.
  • The old session continued if reused client identity is provided to the broker.

Connection

As we mentioned above that MQTT protocol is a resource-constrained protocol and as it is widely used in IoT devices so SSL / TLS is not always an option so it is not desirable in some cases. But in such cases this authentication is presented as clear text username and password and the client send it to the server by attaching it in connect / connack packet sequence.

In case of open brokers that are published on the Internet, they can accept anonymous clients and as part of authentication, the USERNAME and PASSWORD are simply left bank in the handshaking process.

Authentication and Termination

During communication phase, client can perform operations like publishing, subscribing, unsubscribing and ping operations. The basic function of publishing is to send binary block of data defined by the publisher. Format of content is application specific. For subscription service a packet pair named as subscribe / suback is used and for unsubscribing operation unsubscribe / unsuback packet pair is used.

For ping operation, the client can check its connection with the broker by using pingreq / pingresp packet sequence. It keeps the client’s connection with the broker alive. One important point to keep in mind is that there is no direct connection between the publisher and subscriber.

Types of Communication for MQTT

There are two types of communication used for MQTT named as

  • Non-encrypted communication
  • Encrypted communication.

Different port numbers are assigned for both type of communication. If we talk about standard port for non-encrypted communications then port numbers are 1883 and if we need encrypted communication by using standard ports then port numbers are 8883 and they use SSL / TLS.

For SSL / TLS handshaking process id required and during this process, the client authenticates the server it is going to use by validating the server certificate. When a client is going to establish a connection with the broker.

The client provides a client certificate to the broker during connection establishment handshaking. This certificate is used by a broker to authenticate the client. It is expected from brokers to support the authentication of the client with the help of SSL / TLS client-side certificates.

Advantages Of MQTT Protocol

Some advantages of using MQTT protocol are:

  • Good choice for wireless networks
  • Distribute information reliably
  • Increase the scalability of the network
  • Reduce bandwidth consumption of network
  • It is well suited for control and remote sensing
  • Lightweight overhead
  • Reduce updating rates to seconds
  • Uses permission-based security making it more secure
  • Saves development time
  • Publish/subscribe protocol are able to collect more data by using less bandwidth as compared to other polling protocols.
  • Maximize bandwidth which is available

Why MQTT Protocol is Known as Lightweight?

MQTT is known as lightweight protocol as all messages of this protocol contains a small code footprint. Every message has a fixed header having a size of 2 bytes i.e. 16 bits only. An optional variable header is also present. Except these headers a message payload is also present and size of this message payload is limited to 256 MB of information and it also has a QoS (quality of service) level.

QUALITY OF SERVICE LEVEL

In MQTT protocol, we have three different QoS levels that are used to determine that how our content / message will be managed. One thing we can say for sure is that obviously higher levels of quality of service are more reliable because they have more bandwidth and latency requirements.

  1. The first and simplest quality of service level is an unacknowledged service i.e. it use publish packet sequence which means that the publisher will send message to broker and broker will pass this message to subscriber and both this message delivery will take place only one time. Broker will not store the message for the case if packet is lost so there is no reliable delivery of messages.
  2. The second quality of service level is acknowledged service. It make use of publish / puback packet sequence for communication between publisher and broker as well as broker and subscriber. In case of packet loss, a retry mechanism is present that will send that message from its buffer space. In this quality of service a subscriber can have multiple copies of same message if the message is received but acknowledgment was not received timely.
  3. The third and last level of quality of service is known as assured service. In this quality of service it covers drawbacks of 1st and 2nd level of quality of service by delivering each message with two packet pairs. The first pair is known as publish / pubrec and second pair is known as pubrel / pubcom. The purpose of using these two pairs is to ensure that message is sent only once irrespective of the number of retries in case of packet lost.

MQTT Applications

MQTT protocol is widely used and some of the main applications of this protocol are given below:

  • Used in Facebook messenger application
  • AWS
  • Google cloud
  • Microsoft Azure
  • Smart homes
  • Healthcare
  • Logistics

So in this article we discussed about MQTT protocol, its advantages, working, quality of service provided by this protocol, and some applications of this protocol. Any queries related to this article can be asked in the comment section below.

Leave a Comment

1 + 1 =