MQTT Docs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

What is MQTT?

MQTT is a standards-based messaging protocol, or set of rules, used for machine-to-machine


communication. Smart sensors, wearables, and other Internet of Things (IoT) devices typically have to
transmit and receive data over a resource-constrained network with limited bandwidth. These IoT devices
use MQTT for data transmission, as it is easy to implement and can communicate IoT data efficiently.
MQTT supports messaging between devices to the cloud and the cloud to the device.

Why is the MQTT protocol important?


The MQTT protocol has become a standard for IoT data transmission because it delivers the following
benefits:

Lightweight and efficient

MQTT implementation on the IoT device requires minimal resources, so it can even be used on small
microcontrollers. For example, a minimal MQTT control message can be as little as two data bytes. MQTT
message headers are also small so that you can optimize network bandwidth.

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.

What is the history behind MQTT


protocol?
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.

What is the principle behind MQTT?


The MQTT protocol works on the principles of the publish/subscribe model. In traditional network
communication, clients and servers communicate with each other directly. The clients request resources
or data from the server, then the server processes and sends back a response. However, MQTT uses a
publish/subscribe pattern to decouple the message sender (publisher) from the message receiver
(subscriber). Instead, a third component, called a message broker, handles the communication between
publishers and subscribers. The broker’s job is to filter all incoming messages from publishers and
distribute them correctly to subscribers. The broker decouples the publishers and subscribers as below:

Space decoupling

The publisher and subscriber are not aware of each other’s network location and do not exchange
information such as IP addresses or port numbers.

Time decoupling

The publisher and subscriber don’t run or have network connectivity at the same time.

Synchronization decoupling

Both publishers and subscribers can send or receive messages without interrupting each other. For
example, the subscriber does not have to wait for the publisher to send a message.

What are MQTT components?


MQTT implements the publish/subscribe model by defining clients and brokers as below.

MQTT client

An MQTT client is any device from a server to a microcontroller that runs an MQTT library. If the client is
sending messages, it acts as a publisher, and if it is receiving messages, it acts as a receiver. Basically,
any device that communicates using MQTT over a network can be called an MQTT client device.

MQTT broker

The MQTT broker is the backend system which coordinates messages between the different clients.
Responsibilities of the broker include receiving and filtering messages, identifying clients subscribed to
each message, and sending them the messages. It is also responsible for other tasks such as:

 Authorizing and authenticating MQTT clients


 Passing messages to other systems for further analysis
 Handling missed messages and client sessions

MQTT connection

Clients and brokers begin communicating by using an MQTT connection. Clients initiate the connection by
sending a CONNECT message to the MQTT broker. The broker confirms that a connection has been
established by responding with a CONNACK message. Both the MQTT client and the broker require a
TCP/IP stack to communicate. Clients never connect with each other, only with the broker.

How does MQTT work?


An overview of how MQTT works is given below.

1. An MQTT client establishes a connection with the MQTT broker.


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.

Let’s break down the details for further understanding.

MQTT topic

The term ‘topic’ refers to keywords the MQTT broker uses to filter messages for the MQTT clients. Topics
are organized hierarchically, similar to a file or folder directory. For example, consider a smart home
system operating in a multilevel house that has different smart devices on each floor. In that case, the
MQTT broker may organize topics as:

ourhome/groundfloor/livingroom/light

ourhome/firstfloor/kitchen/temperature

MQTT publish

MQTT clients publish messages that contain the topic and data in byte format. The client determines the
data format such as text data, binary data, XML, or JSON files. For example, a lamp in the smart home
system may publish a message on for the topic livingroom/light.

MQTT subscribe

MQTT clients send a SUBSCRIBE message to the MQTT broker, to receive messages on topics of interest.
This message contains a unique identifier and a list of subscriptions. For example, the smart home app on
your phone wants to display how many lights are on in your house. It will subscribe to the topic light and
increase the counter for all on messages.

What is MQTT over WSS?


MQTT over WebSockets (WSS) is an MQTT implementation to receive data directly into a web browser.
The MQTT protocol defines a JavaScript client to provide WSS support for browsers. In this case, the
protocol works as usual but it adds additional headers to the MQTT messages to also support the WSS
protocol. You can think of it as the MQTT message payload wrapped in a WSS envelope.

Is MQTT secure?
MQTT communication uses SSL protocol to protect sensitive data transmitted by IoT devices. You can
implement identity, authentication, and authorization between clients and the broker using SSL
certificates and/or passwords. The MQTT broker typically authenticates clients using their passwords as
well as unique client identifiers it allocates to each client. In most implementations, the client
authenticates the server with certificates or DNS lookups. You can also implement encryption protocols
with MQTT.

You might also like