Lightweight MQTT Protocol

The most adopted messaging protocol for IoT

What is MQTT?

MQTT is a lightweight and open source machine-to-machine OASIS standard messaging / communication protocol that operates over TCP/IP. It’s mostly utilised for M2M (machine-to-machine) communication and Internet of Things communications. Designed specifically for the Internet of Things (IoT) and for IoT control devices with limited bandwidth. As a result, it’s the perfect method for transferring data between various IoT devices. With a Central Broker, Clients and Gateways, Publishing and Subscribing Clients, Topics, Three Levels of Quality of Service (QoS), and Message Retention, communication becomes much more robust.

MQTT Specifications

The MQTT protocol is an OASIS standard. The OASIS technical committee is in charge of the MQTT specification. Checkout the following links for more information on the standard and specification:

How MQTT works?

MQTT Architecture

It is based on a publish and subscribe architecture, which allows the protocol to send messages to one or more clients. Because of its small code footprint and the lack of a direct relationship between the clients, it is a lightweight publish subscribe messaging transport (sender and receiver). As a result, the broker serves as a middle-ware between these clients. Duplex communication is used in this protocol (bidirectional). A client (sender) publishes messages on a topic, and another client (receiver) subscribes to the same topic to receive the publisher’s messages. MQTT clients are the publishers who send the messages and the subscribers who receive them.

Why you need MQTT?

It is a protocol for exchanging data between controlled devices and server applications. It is appropriate for machine-to-machine (M2M) communication because it minimizes network bandwidth requirements, handles unreliable networks, and requires little developer effort. An Internet of Things (IoT) system is a network of interconnected devices that communicate with one another. MQTT is a good fit for this idea. It’s a quick-response approach that’s low on bandwidth. The scalability of a MQTT broker is determined by the implementation. It can handle an increase in the number of connected devices and the message flow that goes with it. The protocol’s flexibility enables IoT devices and services to accommodate a wide range of IoT application scenarios. This reliable protocol improves the efficiency of device interaction, regardless of the number of devices involved. Last WILL & Retained Message delivery with retained flag set, Flexible Subscription pattern, MQTT Security for device connectivity through SSL / TLS, and the protocol specified authentication with username and password are all defined in the protocol.

MQTT Components

MQTT clients and MQTT Broker/Server are the two main components. The MQTT connection exists between the client and the broker at all times. Clients do not communicate with one another directly. As a result, the broker serves as a gateway for clients. Let’s have a look at the above-mentioned components in more detail.

Broker - Client

MQTT Clients

An MQTT client is essentially any device that speaks MQTT via a TCP/IP stack. These clients are among both publishers and subscribers. The publisher and subscriber labels indicate whether the client is currently publishing or receiving messages, thus publish and subscribe functionality can be integrated in the same client. The Client is required for every device or edge application in the (MQTT) Message Queuing Telemetry Transport infrastructure to read and communicate edge data via sensors. Because the client connection is established with the MQTT server, offering subscriptions on topics of interest to them, and publishing messages to other clients.

Because the clients cannot connect to one other directly, they only engage via the broker. Every client is both a transmitter and a receiver. Any device that runs a MQTT library and connects to the broker over the network can operate as the client.

MQTT Broker

MQTT Broker is a central server or a go-between for the devices that serves as a data warehouse for all messages sent between clients and devices using the MQTT protocol OASIS standard. The MQTT broker receives all messages, filters them, determines who is interested in them, and then publishes the message to all subscribers. Clients establish a connection with the broker and subscribe to the topics that the broker requires in order to receive messages. When an event occurs on the sensors, the edge clients also publish data.

The broker keeps track of the subscriptions and data that has been released. The broker provides data to the clients based on the QoS levels (QoS 0, 1, 2), retention definition, and clean sessions specified by the clients. MQTT 5 specification defines the way the broker’s handling of the shared subscription and the client’s disconnection.

MQTT Devices

Devices and apps are referred to as “things” in a MQTT network. This is because, in terms of the broker, there is no difference between the two. To do this, the broker’s subjects can be published and subscribed to by both devices and applications. The devices range from simple Arduino-based devices to devices for mission-critical commercial, industrial, and smart home applications in the field today. Interconnected devices are used in a lot of smart homes and enterprises.

IoT Applications Development

MQTT can be used in two ways to create an IoT application.

Application Over MQTT Broker

The collection of data from multiple sources accounts for 90% of today’s application requirements. The data will be transferred to the central broker because it is being collected. As a result, it’s preferable to design the application just behind the Broker, where we’ll acquire the most meta data about the data. We reduce the flow of data from the broker to the application client by building the application over the MQTT broker. The Broker was transformed into an IoT Application Framework that performed the same process. Currently IoT products and applications can be built over MQTT broker. Broker that supports MQTT version 3.1.1 & version 5.0 and may be easily extended to create a complete application.

Application behind MQTT Client

There will be a master client in this approach of IoT Application development that subscribes to all of the subjects either one by one or using the # subscription. The subscribed clients will receive all data sent to the broker. The client will have a framework in place to store data, process it, analyse it, and offer it to the client as needed. However, if the number of real time data providers grows, data flow to this client will increase. To avoid data overloading, use version 5.0 shared subscription.

IoT Edge Development

MQTT clients and brokers can be installed on IoT gateways and specialised devices. To publish messages to a central broker, deploy a MQTT client on the device edge. By deploying a MQTT broker to the device’s edge, the device can perform more autonomous data processing and coordination among its sensors. A broker, for example, maybe deployed in an auto-mobile to coordinate messaging between the various components. The processing power of edge devices is often insufficient to facilitate clustering. As a result, a single instance of the MQTT broker would be deployed.

MQTT Gateway

MQTT Gateway

In general, the MQTT gateway can be considered as an intermediary between any IoT platform and sensors. It operates by converting data from these sensors or smart devices into MQTT messages. It collects messages, gets data from edge sensors, turns it to messages, and sends it to IoT apps or the MQTT platform. Between the sensors and the server, the gateway serves as a middle ware broker. This gateway includes a feature that detects all available sensors on the network that surrounds it. The discovered sensor and device are given permission to connect with one another and are managed by the central IoT platform.The self-advertising of sensors and devices helps to find the client’s gateway. The gateway is considered as an IoT edge device in this framework.

Common Questions on MQTT

Why MQTT is the leading message protocol for IIoT?

MQTT is a machine to machine (M2M) data transfer protocol. The extremely lightweight overhead, publish/subscribe model, bidirectional capabilities and the tcp based connected communication are uniquely suited to meet the demands of industrial control systems. Hence it is the leading message protocol for IIoT.

Why is MQTT used in IoT?

MQTT initially referred as “MQ Telemetry Transport” is a simple, lightweight publish / subscribe messaging protocol, designed for constrained devices with low-bandwidth. So it is perfect for internet of things applications.

Name some practical uses of this protocol?
  • Monitoring production in manufacturing Industry.
  • Automated Metering in Energy Industries.
  • Location awareness and safety.
Is MQTT UDP or TCP ?

MQTT is an application layer protocol built on top of the TCP protocol for data transmission. A variant, MQTT-Sn designed exclusively for edge sensor networks is over UDP.

What are the three levels of QoS?

MQTT QoS comprise of three levels which are classified as per increasing order of overhead.

QoS 0 – At most once

QoS 1 – At least once

QoS 2 – Exactly once

To know more about QoS, navigate to MQTT QoS article.

How MQTT is better than other Protocols ?

MQTT is easy of use and it  is essential when response time, throughput, lower battery and bandwidth usage are on the first place for future solutions. Though HTTP is worthy and extendable, MQTT is more suitable in case of IoT development.

What is the most common MQTT port?

1883 is the default port.

Do you need a broker to use MQTT?

Yes, Of course.

Is it possible to publish the same topic from several clients?

Yes. Multiple clients subscribe to the same topic in message queuing telemetry protocol.

Can the identity of the client who sent a message be determined?

No, unless the client includes it in the topic or payload.

What happens to messages sent to topics to which no one has subscribed?

The broker will discard the messages.

Can I subscribe to a topic in which no one is publishing?

Absolutely yes.

Do messages get stored on the broker?

Yes, but only for a brief duration. They are then discarded after they have been sent to all subscribers. However, in retained messages, when you publish a message, the broker can save the most recently published message. When new subscribers subscribe to that topic, this will be the first message they see. MQTT only retains one message.

How do I find out what topics have been published?

You won’t be able to do this easily because the broker doesn’t seem to keep a list of published topics because they aren’t permanent.

Key Features

MQTT QoS level

Quality of Service Levels

Understanding each level of QoS

MQTT LAST WILL

Last WILL & Testament

Effective use of Last WILL messages

Broker - Client

MQTT Topics

Term to address that allows clients to share information.

Persistence Session and Message Queuing

Persistent Sessions & Message Queuing

Representing an ongoing connection between a client and a Broker

MQTT Retained Messages

Retained Messages

Storing the “Last Good Message” on a topic

minimize-message-flow

Keep Alive & Client Take Over

Checking the status of the TCP/IP connection

MQTT over websockets

MQTT over Websockets

Allows you to receive data directly into a web browser