MQTT Essentials for IoT Professionals
MQTT Essentials for IoT Professionals
Abstract
protocols cannot address with features such as persistent
In the rapidly evolving landscape of IoT, MQTT has emerged sessions, retained messages, Last Will and Testament (LWT),
as the de facto standard protocol for data exchange. MQTT Quality of Service (QoS) levels, and more. After reading this
Essentials is designed to equip decision-makers, solution guide, you’ll be ready to use MQTT to optimize connectivity
architects, and IoT professionals with a strategic and practical and lay the proper data foundation to enable any IoT or IIoT use
understanding of MQTT and how to execute it for scalable, case.
reliable, and seamless data movement. Delve into how MQTT
can help your organization overcome the challenges other IoT
TABLE OF CONTENTS
2
Chapter 1: Introduction to MQTT
MQTT is a lightweight messaging protocol originally designed for communication in constrained networks with limited bandwidth
and compute resources. Developed with simplicity and scalability in mind, MQTT is particularly well-suited for Internet of Things
(IoT) applications where the variety and quantity of devices are growing exponentially.
[Link]
MQTT Essentials Ebook
MQTT uses a binary message format for communication between clients and servers (brokers). This is in contrast to other
protocols that use text-based formats, such as HTTP or SMTP.
The binary format used by MQTT is designed to reduce bandwidth and compute resources required by clients to
the size of messages and increase the efficiency of exchange data.
communication. By using a binary format, the protocol can 2. Bidirectional communication: MQTT allows devices
minimize the amount of data that needs to be transmitted and to send and receive data from the server, allowing for
reduce the processing power required to interpret messages. bidirectional data exchange with other components in
This makes MQTT well-suited for use in low-bandwidth or the network.
low-power environments, such as IoT devices with limited 3. Scalable: MQTT can scale to support millions of devices
resources. It’s also used in enterprise systems, where real- or “things” in an IoT or IIoT ecosystem.
time data communication is necessary. 4. Reliable message delivery: MQTT specifies different
Quality of Service (QoS) levels to ensure reliable
Another important aspect of the protocol is that MQTT is message delivery.
extremely easy to implement on the client side. Ease of use 5. Message Buffering and Session Resumption: MQTT
was a key concern in the development of MQTT, and this supports persistent sessions between devices and
makes it a perfect fit for constrained devices with limited servers, enhancing message reliability by ensuring
resources. that messages are delivered to clients even after
disconnections.
Benefits of MQTT 6. Security features: MQTT supports TLS encryption for
message confidentiality and authentication protocols for
MQTT offers several key benefits:
client verification.
1. Lightweight and efficient: MQTT minimizes the network
4
Real-World Applications and Use Cases of The Origin and History of MQTT
MQTT: An Overview
In 1999, Andy Stanford-Clark of IBM and Arlen Nipper of
MQTT is used extensively in IoT, Industrial IoT (IIoT), and Arcom (now Cirrus Link) developed the MQTT protocol to
M2M applications. It has been adopted by companies enable minimal battery loss and bandwidth usage when
such as BMW, Air France-KLM, Liberty Global, Mercedes connecting with oil pipelines via satellite. The inventors
Benz, Hytera, Awair, and Matternet, as showcased in specified several requirements for the protocol, including:
HiveMQ’s customer success stories. These companies
have successfully leveraged MQTT in automotive, • Simple implementation
telecommunications, energy, public safety, and connected • Quality of Service data delivery
product domains. • Lightweight and bandwidth-efficient
• Data agnostic
Here are a few examples: • Continuous session awareness
[Link]
MQTT Essentials Ebook
more supportive ecosystem for MQTT. By contributing MQTT The Role of OASIS in Standardizing MQTT
client implementations to an open-source project like Paho,
In 2014, OASIS announced that it would take over the
IBM allowed developers to access the protocol and build
standardization of MQTT, with the goal of making it an open
their applications on top of it. This move helped to increase
and vendor-neutral protocol. Founded in 1993 as a non-pro
the visibility and adoption of MQTT among the developer
fit, OASIS (Organization for the Advancement of Structured
community.
Information Standards) is an international consortium
that develops open standards for the Internet and related
In 2012, HiveMQ became acquainted with MQTT and built the
technologies.
first version of their software that same year. In 2013, HiveMQ
released their software to the public.
It has developed numerous important standards for industries
such as cloud computing, security, and IoT, including AMQP,
SAML, and DocBook. The standardization process took
around one year, and on October 29, 2014, MQTT became an
officially approved OASIS standard.
6
the ever-increasing demands of modern IoT environments, recipients. MQTT clients publish messages to the broker,
where the number of connected devices and the amount of and other clients subscribe to specific topics to receive
data they generate are growing exponentially. messages. Each MQTT message includes a topic, and clients
subscribe to topics of their interest. The MQTT broker
maintains a subscriber list and uses it to deliver messages to
the relevant clients.
While MQTT 5 presents a substantial update to MQTT
3.1.1, it remains more of an evolution than a revolution,
faithfully retaining all the features that have contributed
to its success. Its lightweightness, push communication,
unique attributes, ease of use, exceptional scalability,
suitability for mobile networks, and decoupling of
communication participants all endure in this latest
version. However, several foundational mechanics have
been added or slightly adjusted, ensuring that while the
new version still has the feel of MQTT, it incorporates An MQTT broker can also buffer messages for disconnected
improvements that reinforce its standing as the most clients, ensuring reliable message delivery even in unreliable
popular Internet of Things protocol to date. network conditions. To achieve this, MQTT supports three
different Quality of Service (QoS) levels for message delivery:
0 (at most once), 1 (at least once), and 2 (exactly once).
The reassuring news for those acquainted with MQTT 3.1.1
is that the fundamental principles and features remain
unchanged in MQTT 5. Despite certain modifications and
There are two versions of the MQTT
expansions, the heart of the MQTT you are familiar with
specification: MQTT 3.1.1 and MQTT 5.
persists in version 5. Noteworthy adjustments include slight
While most commercial MQTT brokers now
changes to pre-existing features such as Last Will and
support MQTT 5, some IoT-managed cloud
Testament and the inclusion of popular HiveMQ features like
services still primarily support MQTT 3.1.1.
TTL and Shared Subscriptions, which you will learn in detail
We highly recommend using MQTT 5 for new
in the chapters ahead.
IoT deployments due to its enhanced features
that focus on robustness and cloud-native
The underlying protocol has experienced minor
scalability.
transformations, and an additional control packet — AUTH
— has been incorporated. However, these changes do
not obscure MQTT’s core identity. In essence, MQTT v5
is still unambiguously MQTT, retaining its recognizable
MQTT Clients
characteristics while enhancing its capabilities.
Many open source MQTT Client Libraries are available
in a variety of programming languages. HiveMQ provides
Chapter 2: Mastering the Basics of
MQTT Client Libraries, which are designed to simplify the
MQTT deployment and implementation of MQTT clients and offer
users top-notch functionality, performance, security, and
At the core of MQTT are MQTT brokers and MQTT clients.
reliability. Some of the programming languages supported
The MQTT broker is an intermediary between senders
include C#, C++, Java, WebSockets, Python, and more.
and receivers, dispatching messages to the appropriate
7
[Link]
MQTT Essentials Ebook
Eclipse Paho also offers MQTT client libraries for languages started with HiveMQ on-premises MQTT Broker.
like C/C++ and Python. You can find a comprehensive list of • Deploy an MQTT broker on Amazon Web Services (AWS).
MQTT clients at [Link]. Here’s how to get started with HiveMQ on AWS.
• Deploy an MQTT Broker on Microsoft Azure. Here’s how
MQTT Brokers to get started with HiveMQ on Azure.
8
MQTT Client to HiveMQ Cloud. • Client Initialization: The MQTT client initializes its MQTT
library and sets up the required parameters, such as the
1. Sign up for Serverless FREE HiveMQ Cloud plan.
broker's address and port.
2. Create a free HiveMQ Cloud cluster.
• Connection to the broker: The client establishes a TCP/
3. Create access credentials that your MQTT clients use to
IP connection with the MQTT broker. The default port for
connect to the cluster.
MQTT is 1883 (or 8883 for encrypted connections using
4. Copy the Cluster url, port number, and access
TLS/SSL).
credentials.
• Handshake: Once the TCP/IP connection is established,
5. Use the details in your MQTT client to connect to
an MQTT handshake occurs. This involves exchanging
HiveMQ Cloud. For detailed steps, check out our
control packets to establish the connection and deal
documentation.
with parameters.
• Connect packet: The client sends a "CONNECT" packet
Step 3: Start Publishing MQTT Messages/
Subscribing to an MQTT Topic to the broker, indicating its intention to establish a
connection. This packet includes information such as
Using MQTT CLI or via the console, you can start publishing
client ID, connection flags, and other settings.
MQTT messages and subscribing to MQTT topics. Here’s an
• Acknowledgment (CONNACK): The broker responds with
example using MQTT CLI:
a "Connack" packet, indicating whether the connection
1. In the terminal window of the MQTT client (testClient2), request is accepted or rejected.
enter sub -t testTopic -[Link] command subscribes • Subscriptions (optional): If the client is a subscriber,
testClient2 to all messages that are published with the it can then send "Subscribe" packets to the broker,
topic testTopic. indicating the topics it wants to subscribe to.
2. In the terminal window of another MQTT client • Publishing messages: Once the connection is
(testClient1), enter pub -t testTopic -m Hello. This established, clients can publish messages to topics
command publishes the message Hello from testClient1 or subscribe to topics to receive messages from other
with the topic [Link] message Hello appears clients.
immediately in the terminal window of testClient2. • Keep-Alive: To maintain the connection, clients
periodically exchange "Ping" packets to confirm that the
For detailed steps, check out our documentation. connection is still active.
[Link]
MQTT Essentials Ebook
As a first-time user of HiveMQ Cloud, you will be automatically directed to the "Getting Started" section within the management
view of your cluster. Here, you can create access credentials and obtain connection details, as you will see later in this example.
Use Case Example: An IoT Application Built Using Raspberry Pi, MQTT, and Temperature Sensor
In this example, we explore the connection of a temperature and brightness sensor to a Raspberry Pi, and then leveraging the
power of MQTT to transmit the sensor data to a designated MQTT broker effortlessly. You will discover how another device, acting
as a control center, can effortlessly receive and process the MQTT data, enabling efficient monitoring and control of your IoT
ecosystem.
Communication between the sensor client and the control center over MQTT
To establish a secure connection between your MQTT client and the cluster, creating MQTT client access credentials is essential.
Navigate to the Access Management tab of your HiveMQ Cloud cluster, and enter the username, password and set Permission to
Publish and Subscribe. These credentials will also replace "<your_username>" and "<your_password>" within the code snippet.
10
public class Sensor {
11
[Link]
MQTT Essentials Ebook
[Link](500);
[Link]().publishWith()
.topic("home/temperature")
.payload(getTemperature())
.send();
[Link](500);
}
}
//4. Simulate Temperature and Brightness sensor data
private static byte[] getBrightness() {
// simulate a brightness sensor with values between 1000lux and 10000lux
final int brightness =
[Link]().nextInt(1_000, 10_000);
return (brightness + "lux").getBytes(StandardCharsets.UTF_8);
}
Let’s dissect the code snippet provided above to understand its functionality:
1. Creating the MQTT Client: The code initializes the MQTT client, ensuring a unique identifier is used. An automatic reconnect
feature is also enabled to handle potential instability in the sensor’s internet connection.
2. Establishing Connection to "<your_host>": The client connects to the specified host. Notably, a “will” message is set, allowing
the broker to automatically publish a “sensor gone” notification if the sensor loses its connection.
3. Periodic Publication of Simulated Sensor Data: The code periodically publishes simulated brightness and temperature data
using the methods getBrightness() and getTemperature() methods, ensuring a steady stream of information for further
processing.
With this code snippet, you will have created an MQTT client, established a connection to the broker, and started regularly
transmitting the Brightness and Temperature sensor data.
12
Step 2 - Implementing the Subscribing Client
In this next step, we focus on creating the subscribing client responsible for consuming the values published on the topics home/
temperature and home/brightness.
Implementing the subscribing client enables the reception of sensor data transmitted via MQTT. This functionality allows you to
process and utilize the received information for various applications efficiently.
13
[Link]
MQTT Essentials Ebook
.callback(publish -> {
[Link]("Received message on topic " + [Link]()
+ ": " +
new String([Link](), StandardCharsets.
UTF_8));
})
.send();
}
}
The code snippet above performs the following actions: Chapter 3: MQTT Topics,
1. Creates an MQTT client instance, similar to the sensor Subscriptions, QoS, and Persistent
client, with the client ID prefixed as controlcenter-. Messaging
2. Establishes a connection between the client and the
specified host in <your_host>. To ensure message MQTT's Messaging Model: Topics and Subscriptions
buffering when the control center is offline, a session MQTT’s messaging model is based on topics and
expiry interval of 1 hour is set. subscriptions. Topics are strings that messages are published
3. The client Subscribes to all topics starting with home to and subscribed to. Topics are hierarchical and can contain
using the multi-level wildcard # in the topic filter. multiple levels separated by slashes, like a file path as shown
4. Any incoming messages with their corresponding topic below.
and payload are printed. If the sensor loses connection,
myhome/kitchen/smartdishwasher
the topic home/will and the payload “sensor gone” are
printed.
Subscriptions are used to specify which topics a client is
interested in receiving messages from.
The code in this example was tested on Java 11. You can also
find a publicly hosted example in our GitHub repository, along
When a client subscribes to a topic, it is essentially telling the
with an example tailored for our public broker.
broker that it is interested in receiving messages published
to that topic. The broker then keeps track of the subscription
By implementing both the sensor data publishing client and
and forwards any messages published to that topic to the
the subscribing client, you can establish a seamless MQTT
subscribed client.
communication system where sensor data is published and
consumed by the control center, enabling efficient monitoring
and control of devices.
15
[Link]
MQTT Essentials Ebook
16
The pub/sub features three dimensions of decoupling for appropriate subscribers based on the topic hierarchy. The
optimal efficiency: topic structure is hierarchical, with levels separated by a
forward slash (/), allowing subscribers to receive messages
• Space decoupling: Publisher and subscriber do not need
that match a specific topic level or a topic hierarchy. Here’s an
to know each other (for example, no exchange of IP
example of topic hierarchy.
address and port).
• Time decoupling: Publisher and subscriber do not need to
run at the same time.
• Synchronization decoupling: Operations on both
components do not need to be interrupted during
publishing or receiving.
17
[Link]
MQTT Essentials Ebook
Message Filtering in MQTT • Allows for filtering based on message content rather than
just topic hierarchy
MQTT uses subject-based filtering of messages. Every
• Flexible, allowing for complex filter expressions
message contains a topic (subject) that the broker
can use to determine whether a subscribing client
Drawbacks of Content-based Filtering in Pub/Sub:
gets the message or not. To handle the challenges
of a pub/sub system, MQTT has three Quality of • Can be more complex to use and set up than subject-
Service (QoS) levels. You can easily specify that a based filtering
message gets successfully delivered from the client • Requires publishers to include additional metadata in the
to the broker or from the broker to a client. However, message to enable filtering
there is the chance that nobody subscribes to the • Performance may suffer when processing large numbers
particular topic. If this is a problem, the broker must of filter expressions
know how to handle the situation. For example, the
HiveMQ MQTT Broker has an extension system that Use Case of Content-based Filtering in Pub/Sub:
can resolve such cases. You can have the broker take
Content-based filtering is best suited for use cases where
action or simply log every message into a database
messages are not organized into topics, and subscribers are
for historical analyses. To keep the hierarchical topic
interested in a specific subset of messages based on their
tree flexible, it is important to design the topic tree
content.
very carefully and leave room for future use cases.
If you follow these strategies, MQTT is perfect for
For example, in a logistics application, a subscriber may be
production setups.
interested in receiving messages only about packages with a
specific tracking number. The subscriber would subscribe to
a filter expression like "tracking-number = '123456'," and the
Option 2: Content-based Filtering of Pub/Sub broker would only send messages that match this expression
Architecture to the subscriber.
In this type of filtering, the broker filters messages based on
their content, which is specified using a filter expression. Option 3: Type-based Filtering of Pub/Sub
Subscribers indicate their interest by subscribing to a specific
Architecture
filter expression, and the broker routes the messages to In type-based filtering, the broker filters messages based
the appropriate subscribers based on the content of the on their type or class. This type of filtering is useful when
messages. working with object-oriented languages where messages are
represented as objects. Subscribers indicate their interest by
subscribing to a specific message type or class, and the broker
How to bring Content-based Filtering in MQTT?
routes messages to the appropriate subscribers based on the
Even though MQTT uses subject-based filtering of
message type.
messages, you can also set up content-based filtering
by using the HiveMQ MQTT Broker and our custom
Benefits of Type-based Filtering in Pub/Sub:
extension system.
• Allows for filtering based on message type, regardless of
the topic or content
Benefits of Content-based Filtering in Pub/Sub: • Simple to use, especially when working with object-
oriented languages
• Provides more granular control over which messages are
• Offers a high degree of flexibility and extensibility
received
18
Drawbacks of Type-based Filtering in Pub/Sub: connected to the broker who have subscribed to
the topic of a particular message, that message will
• Limited to filtering based on message type only
not be delivered to anyone. Therefore, it is vital for
• Publishers and subscribers need to agree on the message
publishers to keep in mind that message delivery
type hierarchy beforehand
is not guaranteed and to design their systems
accordingly.
Use Case of Type-based Filtering in Pub/Sub:
19
[Link]
MQTT Essentials Ebook
What Are the Key Benefits of MQTT Pub/Sub receives only the messages of interest. As discussed
Architecture in IoT and IIoT? earlier, pub/sub provides three filtering options: subject-
based, content-based, and type-based filtering. Each
The pub/sub model offers several benefits, making it a popular
option has its benefits and drawbacks, and the choice of
choice for various applications. Here are some of the key
filtering method will depend on the use case. MQTT uses
advantages of using pub/sub architecture:
subject-based filtering of messages and every message
Improved scalability: The pub/sub architecture is highly contains a topic that the broker uses to determine
scalable, making it suitable for applications that handle many whether a subscribing client receives the message or not.
clients and messages. The broker acts as a central hub for 3. Security: Security is a crucial aspect of any messaging
all messages, allowing it to handle many clients without system, and pub/sub is no exception. MQTT allows for
compromising performance. several security options, such as user authentication,
access control, and message encryption, to protect the
Increased fault tolerance: The decoupled nature of pub/ system from unauthorized access and data breaches.
sub architecture also provides improved fault tolerance. In a 4. Scalability: Pub/Sub architecture must be designed with
traditional client-server model, all connected clients lose their scalability in mind, as the number of subscribers can grow
connection if the server goes down. In contrast, in pub/sub, exponentially in a large-scale system. MQTT provides
the broker can store messages until the client reconnects, features such as multiple brokers, clustering, and load
ensuring no messages are lost. balancing to ensure that the system can handle a large
number of subscribers and messages.
Flexibility: The pub/sub architecture is flexible and can be 5. Message Ordering: In a pub/sub system, message
used in a variety of applications, ranging from low-bandwidth, ordering can be challenging to maintain. As messages
high-latency networks to high-speed, low-latency networks. are sent asynchronously, it’s difficult to ensure that
The MQTT protocol, which is based on pub/sub architecture, subscribers receive messages in the correct order.
supports various quality-of-service levels, providing the However, MQTT provides QoS levels that ensures
flexibility to choose the appropriate level for your application. messages are successfully delivered from the client to
the broker or from the broker to a client.
Common Challenges in Pub/Sub Architecture
Because MQTT works asynchronously, tasks are not
and How to Overcome Them
blocked while waiting for or publishing a message. Most
While pub/sub architecture offers several benefits, such
client libraries are based on callbacks or a similar model,
as scalability, flexibility, and decoupling of components, it
making the flow of messages usually asynchronous.
also presents some challenges that must be addressed to
In certain use cases, synchronization is desirable and
ensure a successful implementation. Below are some of the
possible, and some libraries have synchronous APIs to
most common challenges of using pub/sub and solutions to
wait for a specific message.
overcome them:
6. Real-time Constraints: In some use cases, real-time
1. Message Delivery: One challenge of using pub/sub is
constraints are critical, and pub/sub architecture may
ensuring that messages are delivered to subscribers.
not be the best choice. For example, a request/response
In some instances, no subscribers may be available to
architecture may be a better option if low latency is
receive a particular topic, resulting in the message being
essential.
lost. To overcome this, MQTT provides quality of service
(QoS) levels.
You can address the challenges of using pub/sub through
2. Message Filtering: Another challenge of pub/sub is
careful design and implementation. Developers can build
filtering messages effectively so that each subscriber
scalable, secure, and efficient messaging systems by
20
understanding these challenges and utilizing MQTT’s features have helped it gain widespread adoption in IoT, mobile, and
effectively. other distributed applications.
MQTT vs. Message Queues Using MQTT, architecture engineers, and companies can build
systems reliably and efficiently communicate data in various
There is a lot of confusion about the name MQTT and
real-world scenarios. With its decoupling by space and time,
whether the protocol is implemented as a message queue or
asynchronous messaging, subject-based filtering, and Quality
not. We will try to shed some light on the topic and explain
of Service (QoS) levels, MQTT provides a robust set of features
the differences. We mentioned earlier that MQTT refers to
to help developers overcome the challenges of building
the MQSeries product from IBM and has nothing to do with
distributed systems. Overall, the pub/sub architecture and
“message queue“. Regardless of where the name comes from,
MQTT protocol are valuable tools for developers who want to
it’s useful to understand the differences between MQTT and a
build efficient and scalable distributed systems.
traditional message queue:
A message queue stores messages until they are consumed. Chapter 5: MQTT Client and MQTT
When you use a message queue, each incoming message is Broker Connection Establishment
stored in the queue until it is picked up by a client (often called
a consumer). If no client picks up the message, the message The two main components of the MQTT protocol are the
remains stuck in the queue and waits to be consumed. In a client and the broker. An MQTT client can be any device that
message queue, it is not possible for a message not to be runs an MQTT library and connects to an MQTT broker over a
processed by any client, as it is in MQTT if nobody subscribes network. The publisher and subscriber labels refer to whether
A message is only consumed by one client. Another big all messages, filtering them, and sending them to subscribed
difference is that in a traditional message queue a message clients. The broker also handles client authentication
can be processed by one consumer only. The load is and authorization and holds all clients’ session data with
distributed between all consumers for a queue. In MQTT the persistent sessions. Let’s dive deeper into foundational MQTT
Queues are named and must be created explicitly. A queue is and lightweight approach to exchanging messages between
far more rigid than a topic. Before a queue can be used, the IoT devices. The foundation of this communication is
queue must be created explicitly with a separate command. the MQTT connection, which enables devices to securely
Only after the queue is named and created is it possible to and reliably exchange data with the MQTT broker. In this
publish or consume messages. In contrast, MQTT topics are section, we will explore the process of establishing an
extremely flexible and can be created on the fly. If you can MQTT connection and the different parameters involved. By
think of any other differences that we overlooked, we would understanding how MQTT connections work, you can optimize
love to hear from you in the comments. your IoT deployment for better performance, security, and
scalability.
systems that can handle many connected clients. MQTT’s the broker must have a TCP/IP stack.
21
[Link]
MQTT Essentials Ebook
22
While users of an MQTT library may find some of the Alternatively, some brokers like HiveMQ offer SSL certificate
information in a CONNECT message useful, certain details may authentication, eliminating the need for username and
be more relevant to implementers of the library. For a complete password credentials altogether. Taking these precautions
understanding of all the information contained in the message, ensures that your MQTT communication remains secure and
refer to the MQTT 3.1.1 specification. protected from potential security threats.
Let’s look at some of the elements the MQTT CONNECT What is Will Message in CONNECT MQTT Packet?
packet contains, such as ClientId, Clean Session, Username/
The MQTT Last Will and Testament (LWT) feature includes
Password, Will Message, Keep Alive, etc.
a last will message that notifies other clients when a client
disconnects unexpectedly. This message can be specified by
What is ClientId in CONNECT MQTT Packet?
the client within the CONNECT message as an MQTT message
The ClientId is a unique identifier that distinguishes each and topic. When the client disconnects abruptly, the broker
MQTT client connecting to a broker and enables the broker to sends the LWT message on the client’s behalf. Learn more
keep track of the client’s current state. To ensure uniqueness, about MQTT Last Will and Testament in Part 9 of this series.
the ClientId should be specific to each client and broker. MQTT
3.1.1 allows for an empty ClientId if no state needs to be What is Keep Alive in CONNECT MQTT Packet?
maintained by the broker. However, this connection must have
The MQTT Keep Alive feature allows the client to specify a
the clean session flag set to true, or the broker will reject the
time interval in seconds and communicate it to the broker
connection.
when establishing a connection. This interval determines the
longest period the broker and client can communicate without
What is CleanSession in CONNECT MQTT Packet?
sending a message. To ensure the connection remains active,
The CleanSession flag indicates whether the client wants the client sends regular PING Request messages, and the
to establish a persistent session with the broker. When broker responds with a PING response. This method allows
CleanSession is set to false (CleanSession = false), considered both sides to determine if the other is still available. Learn
a persistent session, the broker stores all subscriptions for the more about MQTT Keep Alive functionality in Part 10 of this
client and all missed messages for the client that subscribed series.
with a Quality of Service (QoS) level 1 or 2. In contrast, when
CleanSession is set to true (CleanSession = true), the broker When connecting to an MQTT broker from an MQTT 3.1.1
doesn’t retain any information for the client and discards any client, the Keep Alive interval is essential. However, some
previous state from any persistent session. MQTT libraries have additional configuration options,
such as the way queued messages are stored in a specific
What is Username/Password in CONNECT MQTT Packet? implementation.
23
[Link]
MQTT Essentials Ebook
respond with a CONNACK message. It’s essential to pay attention to the connect returnCode,
The CONNACK message contains two data entries: as it can help diagnose connection issues. For example,
if the returnCode indicates an authentication failure, the
• The session present flag
client can attempt to reconnect with the correct credentials.
• A connect return code
Understanding the sessionPresent flag and connect returnCode
is crucial for successful MQTT connections.
What is sessionPresent Flag in a CONNACK Message?
The sessionPresent flag informs the client whether a To summarize, understanding the roles of MQTT clients
previous session is still available on the broker. If the client and the broker and the connection establishment process
has requested a clean session, the flag will always be false, is essential for anyone interested in working with the MQTT
indicating there is no previous session. protocol. MQTT client libraries make adding MQTT support
to applications and devices easy without implementing the
However, if the client requests to resume a previous session, protocol from scratch. MQTT brokers are responsible for
the flag will be true if the broker still has stored session receiving, filtering, and sending messages to subscribed
information. This flag helps clients determine whether they clients and handling client authentication and authorization.
need to re-subscribe to topics or if the broker still has the With this knowledge, you can build scalable and efficient IoT
subscriptions from the previous session. systems using MQTT.
24
MQTT is data-agnostic, which means the
payload can be structured according to the 4. Publish Complete (PUBCOMP): The broker
specific use case of the client. The payload is finally sends a PUBCOMP message to
the message’s main content and is what the confirm that it has successfully received and
clients subscribe to, receive, and process. processed the message. This is the fourth and
final stage of the process.
name, quality of service, retain flag, payload, and DUP flag. of Service (QoS) mechanisms, which ensure reliable message
Let’s take a look at each. delivery. The QoS level determines the number of messages
exchanged between the client and the broker.
What is MQTT PacketId or Packet Identifier?
It’s worth noting that the packet identifier uniquely identifies
The Packet Identifier (PacketId) is an essential attribute in
a message as it flows between the client and broker. The
MQTT. It is used to identify the specific message and to
packet identifier is only relevant for QoS levels greater than
ensure that messages are delivered in the order they were
zero. This holds true not only for PUBLISH, but for SUBSCRIBE,
sent, particularly when QoS levels greater than zero are used.
UNSUBSCRIBE and CONNECT messages.
The Packet ID is assigned by the client and is included in the
PUBLISH, PUBREL, PUBREC, and PUBCOMP messages. When
The client library and/or the broker is responsible for setting
the broker receives a PUBLISH message, it assigns a Packet
this internal MQTT identifier. When a QoS level greater than
ID to the message and sends a PUBACK message to the client
zero is used, the client must wait for a PUBACK or PUBREC
containing the Packet ID of the PUBLISH message. The client
message from the broker before it can send the next message.
uses the PUBACK message to confirm that the broker has
The client should also keep track of the Packet IDs it has
received the message.
sent and received to ensure that messages are not lost or
duplicated. Overall, the Packet ID is essential to MQTT’s
In the MQTT protocol, messages related to publishing
reliability mechanism and helps ensure that messages are
and acknowledgment of messages are divided into
delivered correctly and efficiently.
several stages:
[Link]
MQTT Essentials Ebook
• "+" (plus sign) is used to match a single level in the that a message will be delivered. The message is sent
hierarchy. For example, a subscription to "sensors/+/ once, and if it is lost or not received by the recipient, it will
livingroom" would match “sensors/temperature/ not be resent.
livingroom” and “sensors/humidity/livingroom”, but not • QoS 1 (at least once): This level ensures that a message
“sensors/temperature/kitchen”. is delivered at least once, but it may be delivered multiple
• "#" (hash sign) is used to match multiple levels in the times in the case of network issues or failures.
hierarchy. For example, a subscription to “sensors/#” • QoS 2 (exactly once): This level provides the highest
would match “sensors/temperature/livingroom”, level of assurance for message delivery. The message
“sensors/humidity/kitchen”, and “sensors/power/ is guaranteed to be delivered exactly once, but this level
meter1”. requires more communication between the sender and
receiver, which can increase latency and network traffic.
Subscribing to a large number of topics can have a significant
impact on broker performance. This is because every message Choosing the appropriate QoS level depends on the specific
that is published to a topic subscribed by a client must be use case. For example, QoS 0 might be suitable for non-critical
delivered to that client. If many clients subscribe to many data, while QoS 2 might be necessary for critical data requiring
topics, this can quickly become a heavy burden on the broker. high-reliability levels.
Using wildcards to subscribe to multiple topics with a single It’s important to note that the QoS level can impact the
subscription can also impact performance. When a client performance of the broker and network, so it’s recommended
subscribes to a topic with a wildcard, the broker must evaluate to use the appropriate level for the specific use case.
every message published to a matching topic and determine
whether to forward it to the client. If the number of matching What is MQTT Retain Flag?
topics is large, this can strain the broker’s resources.
The Retained Flag is an important feature that determines
whether the broker saves a message as the last known good
To avoid performance issues, it’s important to use topic
value for a specified topic. When the retained flag is set to true,
subscriptions efficiently. One approach is to use more
the broker will save the most recent message that matches the
specific topic filters whenever possible, rather than relying on
specified topic, regardless of whether there are any subscribed
wildcards. Another approach is to use shared subscriptions,
clients.
which allow multiple clients to share a single subscription to
a topic. This can help reduce the number of subscriptions and
When a new client subscribes to a topic with a retained
messages that the broker must handle. Finally, monitoring
message, the broker sends the last retained message (on
the broker’s performance and adjusting its configuration as
that topic) to the client. This allows clients to receive the
necessary is important to ensure optimal performance.
most recent and relevant information even if they have not
subscribed to that topic before.
What is Quality of Service (QoS) in MQTT?
We touched upon Quality of Service Level (QoS) of an MQTT It’s important to note that, like many of the other elements,
message earlier. To refresh, QoS is indicated by a number that the use of retained messages can also impact the broker’s
ranges from 0 to 2. Each level provides a different level of performance, especially if there are many retained messages.
reliability and assurance for message delivery. Additionally, if a retained message is updated frequently, it can
result in increased network traffic and potentially affect the
network’s performance.
• QoS 0 (at most once): This level provides no guarantee What is MQTT Payload?
26
The payload is the actual content of the message and can 4. Feedback: The publishing client receives a confirmation
contain any kind of data. MQTT is data-agnostic, meaning message from the broker indicating that the message
it can handle different data types, including images, text in was published successfully. However, the client does not
any encoding, encrypted data, and binary data. However, receive feedback on how many subscribers received the
it’s important to note that the payload size can impact message or whether anyone is interested in it.
network performance and memory usage on the client and The client that initially publishes the message is only
broker. Therefore, keeping payloads as small as possible is concerned about delivering the PUBLISH message to the
recommended, especially when publishing messages with a
high frequency.
27
[Link]
MQTT Essentials Ebook
Packet Identifier: The packet identifier is unique and identifies Packet Identifier: The SUBACK message includes the
a message as it flows between the client and broker. The client same packet identifier that the client included in the
library or the broker is responsible for setting this internal SUBSCRIBE message, which enables the client to match the
MQTT identifier. acknowledgment to the original request.
List of Subscriptions: A SUBSCRIBE message can contain Return Code: The SUBACK message also includes one return
multiple subscriptions for a client. Each subscription includes code for each topic/QoS-pair specified in the SUBSCRIBE
a topic and a QoS level. The topic in the SUBSCRIBE message message. The return codes are binary values that indicate
can contain wildcards that make it possible to subscribe whether the broker has granted or rejected the subscription
to a topic pattern instead of a specific topic. If there are request for each topic.
overlapping subscriptions for one client, the broker delivers the
message with the highest QoS level for that topic. The return codes for QoS levels are as follows:
128 Failure
to unsubscribe. Let’s now explore the counterpart of the an UNSUBACK acknowledgment message to confirm the
SUBSCRIBE message, the UNSUBSCRIBE message, and removal of the client’s subscriptions. This message includes
the corresponding UNSUBACK message that confirms the the packet identifier of the UNSUBSCRIBE message and serves
29
[Link]
MQTT Essentials Ebook
temperature" and "MyHome/Temperature" are considered temperature can produce the following results:
31
[Link]
MQTT Essentials Ebook
MQTT Topic wildcard hash example 5. $SYS/broker/uptime: Reflects the duration the MQTT
broker has been running.
However, it’s important to consider that subscribing with These $SYS topics offer valuable insights into the
a multi-level wildcard alone can be an anti-pattern if high internal workings and performance of the MQTT
throughput is expected. Subscribing to a broad topic can result broker, enabling administrators and developers to
in a large volume of messages being delivered to the client, monitor and analyze crucial statistics.
potentially impacting system performance and bandwidth
usage. Follow best practices to optimize topic subscriptions By understanding the purpose and significance of topics
and avoid unnecessary message overload. starting with $, you can effectively leverage this convention
to gain deeper visibility into the behavior and performance of
MQTT Topics Beginning with $ their MQTT infrastructure.
33
[Link]
MQTT Essentials Ebook
By following these best practices, you can enhance your MQTT providing the client with the ability to select a service level that
infrastructure’s readability, maintainability, and security. aligns with both the network reliability and the application’s
requirements. MQTT’s inherent capability to handle message
To summarize, MQTT topics serve as the backbone of re-transmission and ensure delivery, even in unreliable network
flexible and efficient message communication in MQTT. By conditions, makes QoS essential for facilitating seamless
understanding the intricacies and applying best practices, communication in such challenging environments. By offering
you can optimize your MQTT implementations for maximum different QoS levels, MQTT empowers clients to optimize
performance and scalability. their network usage and achieve the desired balance between
reliability and efficiency.
Chapter 8: MQTT Quality of Service
(QoS) 0,1, & 2 Now that we understand the significance of Quality of Service
(QoS) in MQTT, let’s delve into the inner workings of QoS and
MQTT Quality of Service (QoS) is an agreement between the explore how it operates to ensure reliable message delivery in
message sender and receiver that defines the level of delivery varying network conditions.
guarantee for a specific message.
How does QoS 0 work in MQTT?
MQTT provides three levels of QoS: At the lowest level, QoS 0 in MQTT offers a best-effort
• At most once (QoS 0) delivery mechanism where the sender does not expect an
• At least once (QoS 1) acknowledgment or guarantee of message delivery. This
• Exactly once (QoS 2) means that the recipient does not acknowledge receiving the
message, and the sender does not store or re-transmit it. QoS
How to Examine Message Delivery in MQTT? 0, commonly called “fire and forget,” functions akin to the
underlying TCP protocol, where the message is sent without
When discussing QoS in MQTT, it’s important to consider
further follow-up or confirmation.
message delivery from the publishing client to the broker and
from the broker to the subscribing client. These two aspects of
message delivery have subtle differences.
QoS level than the publishing client, the broker will transmit the
How does QoS 1 work in MQTT?
message with the lower QoS level.
In QoS 1 of MQTT, the focus is on ensuring message delivery
Understanding how message delivery works in MQTT sets at least once to the receiver. When a message is published
the foundation for appreciating the significance of Quality with QoS 1, the sender keeps a copy of the message until it
of Service (QoS) levels in ensuring reliable communication receives a PUBACK packet from the receiver, confirming the
between the publishing client, broker, and subscribing client. successful receipt. If the sender doesn’t receive the PUBACK
packet within a reasonable time frame, it re-transmits the
Why is Quality of Service (QoS) Important? message to ensure its delivery.
34
How does QoS 2 work in MQTT?
This approach of QoS 1 strikes a balance between reliability MQTT PUBREC Packet
and efficiency, guaranteeing that the message reaches the
receiver at least once while allowing for potential duplicates to Once the sender receives a PUBREC packet from the receiver,
be handled appropriately. the sender can safely discard the initial PUBLISH packet.
The sender stores the PUBREC packet from the receiver and
responds with a PUBREL packet, the receiver discards all
stored states and replies with a PUBCOMP packet.
35
[Link]
MQTT Essentials Ebook
Downgrade of QoS
The QoS levels defined by the sender and receiver can differ.
The client sending the message to the broker defines the QoS
level, while the broker uses the QoS defined by the receiver
during subscription. For example, if the sender uses QoS 2
MQTT PUBREL Packet and the receiver subscribes with QoS 1, the broker delivers the
message to the receiver with QoS 1. This can result in multiple
After the receiver gets the PUBREL packet, it can discard deliveries of the same message to the receiver.
all stored states and answer with a PUBCOMP packet (the
same is true when the sender receives the PUBCOMP). Until Packet identifiers are unique per client
the receiver completes processing and sends the PUBCOMP
Packet identifiers used for QoS 1 and 2 are unique between
packet back to the sender, the receiver stores a reference to
a specific client and a broker within an interaction. However,
the packet identifier of the original PUBLISH packet. This step
they are not unique across all clients. Once a flow is complete,
is important to avoid processing the message a second time.
the packet identifier becomes available for reuse. This is why
the packet identifier does not need to exceed 65535 , as it
After the sender receives the PUBCOMP packet, the packet
is unrealistic for a client to send more messages than that
identifier of the published message becomes available for
without completing an interaction
reuse.
When the QoS 2 flow is complete, both parties are sure that the
Best Practices While Using MQTT Quality of
Service (QoS) 0,1, & 2
We are often asked for advice about how to choose the correct
QoS level. Selecting the appropriate QoS level depends on your
specific use case. Here are some guidelines to help you make
an informed decision:
36
Use QoS 1 when: What are Persistent Sessions?
• You need to get every message and your use case can To receive messages from an MQTT broker, a client
handle duplicates. QoS level 1 is the most frequently used establishes a connection and creates subscriptions to the
service level because it guarantees the message arrives desired topics. In a non-persistent session, if the connection
at least once but allows for multiple deliveries. Of course, between the client and broker is interrupted, the client loses its
your application must tolerate duplicates and be able to subscriptions and needs to re-subscribe upon reconnection.
process them accordingly. This can be burdensome for resource-constrained clients. To
• You can’t bear the overhead of QoS 2. QoS 1 delivers address this issue, clients can request a persistent session
messages much faster than QoS 2. when connecting to the broker.
All messages sent with QoS 1 and 2 are queued for In a persistent session, the broker stores the following
offline clients until the client is available again. However, information (even if the client is offline). This information
this queuing is only possible if the client has a persistent becomes immediately available to the client upon
session. reconnection:
Persistent sessions in MQTT allow a client to maintain its topics to which the client has subscribed. This ensures
subscription and message state across multiple connections. the client does not need to re-subscribe to the same
When a client establishes a persistent session with an MQTT topics every time it reconnects, saving valuable time and
and any undelivered messages intended for the client. This • Flow of all messages in a Quality of Service (QoS) 1 or 2
way, if the client disconnects and reconnects later, it can where the client has not yet confirmed: The broker keeps
resume communication seamlessly. This chapter will dive track of unacknowledged messages sent to the client at
deep into how persistent sessions in MQTT enhance QoS QoS 1 or QoS 2 levels. These messages are stored in the
by enabling reliable message delivery, providing QoS level broker’s message queue and will be delivered to the client
guarantees, and facilitating efficient reconnection for clients, upon reconnection, ensuring reliable message delivery.
even in the presence of intermittent connectivity or client • All new QoS 1 or 2 messages that the client missed
disconnections. while offline: If the client was offline when QoS 1 or QoS
2 messages were published to subscribed topics, the
37
[Link]
MQTT Essentials Ebook
broker stores these missed messages. Once the client examining the session present flag, the client can determine
reconnects, it receives the queued messages, preventing whether to establish a new session or reconnect to an existing
any loss of important information. one.
• All QoS 2 messages received from the client that
are awaiting complete acknowledgment: For QoS 2 Persistent Session on the Client Side:
messages sent by the client, the broker keeps track of Ensuring Local Message Persistence and
their acknowledgment status. If any of these messages Acknowledgment
are not fully acknowledged, the broker holds them until In addition to the broker storing a persistent session,
the acknowledgment is complete. each MQTT client also plays a role in maintaining session
continuity. When a client requests the server to retain session
Clean Sessions in MQTT and How to Use data, it assumes the responsibility and must store the
Them to Start or End a Persistent Session following information:
When establishing a connection to the broker, clients can • Flow of all messages in a QoS 1 or 2 where the broker
enable or disable a persistent session by setting the value has not yet confirmed: The client keeps track of
of the cleanSession flag. Here’s how it works: When the messages it has sent to the broker at QoS 1 or QoS 2
cleanSession flag is set to true, the client explicitly requests levels. These messages are stored locally until the broker
a non-persistent session. In this scenario, if the client acknowledges their receipt or completion. By maintaining
disconnects from the broker, all queued information and these unconfirmed messages, the client ensures that it
messages from the previous persistent session are discarded. can retransmit any messages if necessary and achieve
The client starts with a clean slate upon reconnection. the desired level of reliability.
• All QoS 2 messages received from the broker awaiting
On the other hand, when the cleanSession flag complete acknowledgment: When the broker sends
is set to false, the broker creates a persistent QoS 2 messages to the client, they are received
session for the client. This means that the broker and processed by the client. However, until the
preserves all relevant information and messages client acknowledges the successful processing of
even if the client goes offline. The session these messages, the client stores them locally. This
remains intact until the client explicitly requests ensures that the client can handle any interruptions
a clean session. If the cleanSession flag is set or disconnections while maintaining the reliability and
to false and the broker already has a session integrity of message delivery.
available for the client, it will utilize the existing • By storing these message-related details on the client
session and deliver any previously queued side, MQTT clients can actively maintain session
messages to the client upon reconnection. persistence and ensure the successful processing of
messages even in challenging network conditions.
How Does A Client Know if a Session is Already MQTT Session Management Best Practices:
Stored? Enhancing Message Delivery and Resource
Efficiency
In MQTT version 3.1.1 and beyond, the CONNACK message
sent by the broker in response to the client’s connection When working with MQTT, it’s essential to consider the
request includes a session present flag. This flag serves as best practices for session management to optimize your
an indicator for the client, informing it whether a previously implementation. Here are some guidelines to help you
established session is still available on the broker. By determine when to use a persistent session or a clean
session:
38
Best Practices for MQTT Persistent Sessions • Use Case Specifics: The appropriate solution for
managing message storage duration varies based on
• Ensure Message Reliability: If your client needs to receive
your use case. Consider factors such as the importance
all messages from a specific topic, even when they are
of retaining messages, message expiration policies, and
offline, a persistent session is the way to go. This ensures
regulatory or compliance requirements.
that the broker queues the messages for the client and
delivers them promptly when the client reconnects.
By following these best practices and considering the
• Resource Optimization: If your client has limited
above-mentioned factors, you can effectively manage MQTT
resources, leveraging a persistent session is beneficial.
sessions, optimize message persistence, and ensure reliable
Storing the client’s subscription information on the
communication in your MQTT-based solutions.
broker facilitates a quick restoration of interrupted
communication, reducing the burden on constrained
Remember to incorporate these guidelines into your
clients.
implementation based on your unique requirements and
• Resume Publishes: A persistent session is necessary if
application needs.
your client needs to resume publishing Quality of Service
(QoS) 1 and 2 messages after reconnecting. The broker
To summarize, understanding and effectively utilizing
retains these messages until the client returns online,
persistent sessions, queuing mechanisms, and proper session
ensuring their reliable delivery.
management practices, we can harness the full potential
of MQTT and build robust, scalable, and reliable IoT and
Best Practices for MQTT Clean Session
messaging applications.
• Publish-Only Clients: A clean session is suitable if your
client only needs to publish messages and does not
Chapter 10: MQTT Retained
require subscriptions to topics. In such cases, the broker
Messages
does not need to store session information or attempt to
transmit QoS 1 and 2 messages, simplifying the session A retained message is a normal MQTT message with the
management process. retained flag set to true. The broker stores the last retained
• Avoid Offline Message Retrieval: A clean session suffices message and the corresponding QoS for that topic. Each client
if your client does not need to receive missed messages that subscribes to a topic pattern that matches the topic of the
while offline. It eliminates the overhead of storing and retained message receives the retained message immediately
delivering messages that the client did not subscribe to after they subscribe. The broker stores only one retained
during its offline period. message per topic.
MQTT Message Storage Duration: How Long How are Retained Messages Different from
Does the MQTT Broker Store Messages? Normal MQTT Messages?
People often ask how long the broker stores session
information and messages. The answer depends on various Retained messages are a valuable feature in
factors and considerations: MQTT that mitigates uncertainty regarding
• Memory Limit Constraint: Typically, the primary constraint message publication. By enabling the retention
on message storage is the memory limit of the operating of the most recent message on a topic,
system hosting the broker. Monitoring and allocating subscribers can stay informed about the current
sufficient resources to handle the expected message state, even during periods of inactivity.
volume is crucial.
39
[Link]
MQTT Essentials Ebook
In MQTT, it is important to understand that the publisher to myhome/#, Client B will receive the retained message
of a message cannot guarantee that the subscribing client for myhome/livingroom/temperature immediately after
will receive the message. The publisher’s responsibility lies subscribing to myhome/#. By recognizing the retained flag
in ensuring the safe delivery of the message to the broker. set to true, the subscribing client can process the retained
Similarly, the subscribing client cannot determine when the messages according to its requirements.
publishing client will send a new message related to their
subscribed topics. The time interval between messages can Retained messages are crucial in providing newly-subscribed
vary significantly, ranging from a few seconds to several clients with immediate status updates upon subscribing to
minutes or even hours. As a result, the subscribing client a topic, eliminating the need to wait for subsequent updates
remains unaware of the current topic status until a new from publishing clients. Essentially, a retained message
message is published. This is where retained messages play a represents the last known valid value for a particular topic. It
vital role. does not necessarily have to be the latest value, but it must be
the most recent message with the retained flag set to true.
Retained messages provide a solution to the challenge
mentioned above. When a client publishes a message with Something else important to emphasize, retained messages
the “retained” flag set to true, the broker retains the message. operate independently of persistent sessions, which we
Consequently, any client subscribing to the corresponding discussed in Part 7 of the series. Once the broker stores a
topic will receive the most recent retained message, even if no retained message, there’s only one way to remove it. We will
recent publications have occurred. discuss that shortly in an upcoming section.
In essence, retained messages offer subscribers a snapshot of Now that you understand the structure of retained messages,
the last-known state of a topic, ensuring access to the latest let’s learn more about managing them.
relevant information regardless of publishing frequency.
How to Send a Retained Message in MQTT?
Structure of a Retained Message in MQTT As a developer, sending a retained message is a
An MQTT retained message is a standard message that straightforward process. To mark a message as retained, all
includes the retained flag set to true, signifying its importance. you need to do is set the retained flag of your MQTT publish
When a client publishes a retained message, the broker stores message to true. This flag signals the broker to retain the
it along with the corresponding Quality of Service (QoS) for message and make it available to subscribers. The good news
a specific topic. Subscribing clients immediately receive the is that most MQTT client libraries provide a convenient and
retained message when they subscribe to a topic pattern that user-friendly way to enable this flag, streamlining the process.
matches the retained message’s topic. By leveraging this feature, developers can ensure that critical
information persists and remains accessible to subscribers,
It is worth noting that the broker retains only one message per even if they join the network later or experience temporary
topic. connectivity issues. Retained messages offer a powerful
mechanism for sharing important data and enabling seamless
Even if a subscribing client uses wildcards in their topic communication in MQTT-based systems.
pattern, they can still receive a retained message that may not
be an exact match for the topic. How to Delete Retained Messages in MQTT?
There is only one way to delete the retained message of a
For instance, if Client A publishes a retained message to
topic. To achieve this, simply publish a retained message with
myhome/livingroom/temperature and later Client B subscribes
40
a zero-byte payload to the topic where the retained message Closing the Loop: The Lasting Impression of
is stored. When the broker receives this special retained Retained Messages in MQTT’s Ecosystem
message, it identifies it as a request for deletion and promptly
As you can see, retained messages play a crucial role in MQTT
removes the retained message associated with that topic. As
communication by addressing the challenge of uncertain
a result, new subscribers will no longer receive the previously
message delivery and providing immediate access to the last-
retained message for that particular topic.
known state of a topic. By enabling the retention of the most
recent message on a topic, subscribers can stay informed
It’s worth noting that in many cases, explicitly deleting retained
about the current status, even during periods of inactivity.
messages may not be necessary. This is because each new
retained message automatically overwrites the previous one
Retained messages are beneficial for providing status
for the same topic. Therefore, if you publish a new retained
updates, ensuring newly-subscribed clients receive relevant
message on a topic, it will replace and supersede any existing
information without having to wait for the subsequent
retained message, effectively achieving the same outcome
message publication. By leveraging retained messages, MQTT
as deleting the previous message. This behavior ensures
empowers efficient and reliable communication between
that subscribers receive the most up-to-date and relevant
clients and brokers, enhancing the overall effectiveness of IoT
information, eliminating the need for manual deletion in most
and messaging applications.
scenarios.
Why and When Should You Use Retained Chapter 11. MQTT Last Will and
Messages? Testament (LWT)
Retained messages offer valuable benefits in various Last Will and Testament (LWT) is a powerful feature in
scenarios, particularly when you need newly-connected MQTT that allows clients to specify a message that will be
subscribers to receive messages promptly without waiting for automatically published by the broker on their behalf, if or
the next message publication. when an unexpected disconnection occurs. It provides a
reliable means of communication and ensures that clients can
This is particularly advantageous for delivering real-time gracefully handle disconnections without leaving topics in an
status updates of components or devices on specific topics. inconsistent state. This feature is particularly valuable when
For instance, let’s consider the example of a device named clients must notify others of their unavailability or convey
device1, whose status is published on the topic “myhome/ important information upon an unexpected disconnection.
devices/device1/status”. By utilizing retained messages, new
subscribers to this topic instantly receive the device’s status What is the Purpose of Last Will and
(such as online or offline) immediately after subscribing. Testament (LWT) in MQTT?
In scenarios where unreliable networks are prevalent, it
Similarly, this applies to clients that transmit data periodically,
is common for MQTT clients to experience occasional
such as temperature readings, GPS coordinates, and other
unintended breaks, which can happen due to loss of
relevant information. Without retained messages, newly-
connection or depleted batteries. Understanding the type
subscribed clients would remain unaware of the latest updates
of disconnection (graceful - with a disconnect message, or
between message intervals. By leveraging retained messages,
ungraceful - without a disconnect message) is crucial for
you can seamlessly provide connecting clients with the most
taking appropriate actions.
recent and accurate value, ensuring they have immediate
access to critical information.
41
[Link]
MQTT Essentials Ebook
Understanding when and why the broker sends the Last Will
How to Configure a Last Will and Testament
and Testament (LWT) messages lays the groundwork for
(LWT) Message for an MQTT Client?
implementing best practices in leveraging this feature, which
To specify an LWT message for an MQTT client, you include we will delve into in the next section.
it in the CONNECT message, which is used to initiate the
connection between the client and the broker.
42
When to Use Last Will and Testament (LWT) in Chapter 12: MQTT Keep Alive and
MQTT? Client Take-Over
LWT proves invaluable for alerting subscribed clients about an
Keep Alive is a feature of the MQTT protocol that allows
abrupt disconnection of a client. It becomes a powerful tool
an MQTT client to maintain its connection with a broker by
for storing and communicating client state on specific topics
sending regular control packets called PINGREQ to the broker.
when combined with retained messages.
Let’s dive into MQTT Keep Alive’s critical role in mobile
networks, and in maintaining a robust and efficient MQTT
For instance, by setting a lastWillMessage with Offline
connection.
payload, enabling the lastWillRetain flag, and specifying the
lastWillTopic as client1/status, followed by publishing an
What is MQTT Keep Alive and Why It’s
Online retained message to the same topic, client1 can keep
Important?
newly-subscribed clients informed about its online status.
Should client1 disconnect unexpectedly, the broker publishes The Keep Alive mechanism in MQTT ensures the connection’s
the LWT message with Offline payload as the new retained liveliness and provides a way for the broker to detect if a client
message, ensuring that clients subscribing to the topic while becomes unresponsive or disconnected.
client1 is offline receive the LWT message and stay up to date
on its current status. When a client establishes a connection with an MQTT broker,
it negotiates a Keep Alive value, which is a time interval
LWT not only notifies subscribed clients about unexpected expressed in seconds. The client must send a PINGREQ packet
disconnections but also assists in maintaining the system’s to the broker at least once within this interval to indicate its
integrity by providing valuable information on client states. presence and keep the connection alive. Upon receiving a
Combining LWT with retained messages allows you to create PINGREQ packet, the broker responds with a PINGRESP packet,
a robust solution that stores and communicates the latest confirming that the connection is still active.
client state on specific topics, ensuring reliable updates for
all subscribers. This approach enables seamless integration The MQTT Keep Alive mechanism is important
and synchronization between clients, enhancing the overall for connection monitoring, efficient resource
resilience and functionality of the MQTT network. utilization, detecting network failures, graceful
disconnection, etc.
The Importance of Last Will and Testament in
MQTT Now, let’s establish why the kept alive feature is so important
by delving into the issue of half-open TCP connections and
The Last Will and Testament (LWT) feature in MQTT is crucial
how it poses a challenge within MQTT, particularly in mobile
in ensuring efficient communication and maintaining system
networks.
integrity in the event of unexpected client disconnections.
By combining LWT with retained messages, developers
The Problem of Half-Open TCP Connections in MQTT
can store and communicate client state on specific topics,
providing valuable information to subscribed clients. LWT The problem of half-open TCP connections arises within
empowers MQTT networks with enhanced resilience, seamless MQTT, which relies on the Transmission Control Protocol
integration, and reliable updates, making it a powerful tool (TCP) to ensure “reliable, ordered, and error-checked” packet
for various applications. By understanding the benefits and transfer over the internet. Despite TCP’s robustness, there are
best practices of LWT, you can leverage this feature to create instances where the synchronization between communicating
robust and effective MQTT solutions. parties can falter due to crashes or transmission errors.
43
[Link]
MQTT Essentials Ebook
In TCP, this state of an incomplete connection is referred to as "The Keep Alive ... is the maximum time
a half-open connection, where one side of the communication interval permitted to elapse between the point
remains unaware of the other side’s failure. The connected at which the Client finishes transmitting one
side persistently attempts to send messages while eagerly Control Packet and the point it starts sending
awaiting acknowledgments. the next. It is the responsibility of the Client
to ensure that the interval between Control
Andy Stanford-Clark, the inventor of the MQTT protocol, Packets being sent does not exceed the Keep
highlights that the issue of half-open connections becomes Alive value. In the absence of sending any
more pronounced in mobile networks. While TCP/IP other Control Packets, the Client MUST send a
theoretically notifies users when a socket breaks, practical PINGREQ Packet."
scenarios, particularly on mobile and satellite links, involve the
“faking” of TCP over the air with added headers at each end.
This practice can lead to a phenomenon known as a “black As long as messages are transmitted frequently within the
hole” TCP session, where the connection appears open but, in Keep Alive interval, there is no need to send an additional
reality, discards any transmitted data. message to verify the connection status. However, if the
client remains inactive during the Keep Alive period, it must
send a PINGREQ packet to the broker as a confirmation of its
availability and to ensure that the broker is still accessible.
"Although TCP/IP, in theory, notifies you when a socket
breaks, in practice, particularly on things like mobile and
If a client fails to send any messages or a PINGREQ packet
satellite links, which often “fake” TCP over the air and put
within one and a half times the Keep Alive interval, the broker
headers back on at each end, it’s quite possible for a TCP
is responsible for disconnecting the client. Likewise, the client
session to “black hole,” i.e. it appears to be open still, but
should close the connection if it does not receive a response
is just dumping anything you write to it onto the floor."
from the broker within a reasonable timeframe.
44
When a client wants to signal its continued presence and 12 minutes, and 15 seconds. Conversely, setting the Keep
activity to the broker, it sends a PINGREQ packet. This packet Alive interval to 0 effectively deactivates the Keep Alive
is a “heartbeat” message indicating that the client is still mechanism, removing its influence on the connection’s
alive. If the client has not sent any other type of packet, such stability and management.
as a PUBLISH or SUBSCRIBE packet, it must send a PINGREQ
packet to the broker. The client can choose to send a PINGREQ What is Client Take-Over in MQTT?
packet at any time to verify the ongoing vitality of the network
In the MQTT protocol, when a client becomes disconnected, it
connection. Notably, the PINGREQ packet does not contain any
typically attempts to reconnect. However, there are instances
payload.
when the broker still maintains a half-open connection for that
client. In such cases, when a client, which the MQTT broker
What is PINGRESP in MQTT Keep Alive?
perceives as online, initiates a reconnection and performs a
`client take-over, the broker takes necessary action. It promptly
terminates the previous connection associated with the same
client (identified by the client identifier) and establishes a fresh
connection with the client. This intelligent behavior ensures
that half-open connections do not impede the disconnected
client from successfully re-establishing its connection. By
seamlessly managing client take-over, MQTT guarantees
smooth connectivity and resilient communication in the face of
intermittent network interruptions.
45
[Link]
MQTT Essentials Ebook
MQTT clients must set appropriate Keep Alive values, considering factors such as signal strength and network conditions. This
allows for optimal management of the Keep Alive mechanism and ensures efficient resource utilization.
Understanding the intricacies of the Keep Alive feature and client take-over in MQTT empowers developers to build robust and
resilient MQTT applications. By leveraging these capabilities, MQTT facilitates the creation of reliable, real-time IoT and messaging
solutions that thrive even in challenging network environments.
MQTT has solidified its footprint by connecting numerous constrained devices across multiple deployments, establishing a vast
network of connected systems and standalone devices. From connected cars and manufacturing systems, logistics to enterprise
chat applications, and mobile apps, MQTT’s widespread use has spurred demands for its evolution. MQTT 5 answers this call,
promising an extensive array of exciting new features and improvements.
In this chapter, we provide an in-depth exploration of MQTT Unlocking the Power of IoT with MQTT 5: New
5. We discuss topics ranging from foundational changes in Features and Improvements
the protocol to user properties, shared subscriptions, payload
One of the significant improvements in MQTT 5 is the
format description, request-response pattern, topic alias,
introduction of enhanced authentication mechanisms. These
enhanced authentication, and flow control.
provide a more robust security framework critical in today’s
world, where the risk of cyber-attacks is always looming. With
Design Goals of MQTT 5
MQTT 5, users have more options for securing their devices
The OASIS Technical Committee (TC), tasked with defining and data, including using more sophisticated encryption
and standardizing MQTT, faced the complex challenge of algorithms and key management techniques.
adding long-desired features without increasing overhead or
decreasing ease of use. They sought to improve performance In addition, MQTT 5 introduced Shared Subscriptions, a feature
and scalability without inducing unnecessary complexity. After that allows load balancing of messages across multiple
much deliberation, the TC decided on a host of functional client instances. This ensures that people can manage many
objectives for MQTT 5, aimed at enhancing scalability, messages effectively without overloading individual clients.
formalizing common patterns such as capability discovery and The shared subscriptions feature is practical in scenarios
request-response, and enabling extensibility mechanisms like where many devices are transmitting data concurrently.
user properties.
46
Further, MQTT 5 also introduces the concept of message communication in the foreseeable future.
properties, allowing additional metadata to be included with
each message. This is useful for providing context about the As technology continues to evolve, and as the needs of IoT
transmitted data, such as timestamps, location information, or systems grow and change, so will MQTT. It’s critical for
device status. anyone working in the IoT space to stay abreast of these
developments to ensure they are leveraging the full power
As described, the transition from MQTT 3.1.1 to MQTT 5 was of MQTT and other related technologies to deliver the most
not a simple version number update but rather a significant effective, reliable, and secure IoT solutions possible.
leap in the protocol’s capabilities, addressing several areas
of improvement. The result is a more robust, reliable, and What Properties Are Available in MQTT 5 Header &
scalable protocol better suited to modern IoT applications’ Reason Codes?
needs. Undoubtedly, one of the most transformative and flexible
features introduced in MQTT 5 is the ability to incorporate
It’s important to understand these changes and improvements custom key-value properties in the MQTT header. This
when considering MQTT for IoT applications, as they can capability can potentially revolutionize many deployments,
significantly affect the performance and reliability of your IoT and is of such significance that it warrants an entire chapter
system. So, whether you’re a developer, a systems integrator, dedicated to it. Like protocols such as HTTP, MQTT clients and
or an end user, the step to MQTT 5 is a key advancement in brokers can add an unlimited number of custom or pre-defined
evolving IoT communication protocols. headers to carry metadata. This metadata is adaptable and
can be tailored to specific application data, with pre-defined
While MQTT 5 brings numerous benefits, it also headers predominantly employed in the execution of most new
demands careful implementation. Like any MQTT features.
technology upgrade, it is critical to evaluate
the implications on your specific use case Additionally, MQTT packets now include Reason Codes,
and ensure the transition is controlled and signifying the occurrence of a pre-defined protocol error.
managed. Understanding the benefits and Traditionally found on acknowledgment packets, these Reason
potential challenges of MQTT 5 will help ensure Codes facilitate error interpretation and potentially aid in
a successful implementation, maximizing devising remedies by both clients and brokers.
the potential benefits of this powerful IoT
communication protocol. Occasionally referred to as Negative Acknowledgments, these
Reason Codes can be found on a range of MQTT packets, such
As we look to the future, MQTT remains a vital player in IoT. as:
With MQTT 5, developers now have even more flexibility in
• CONNACK
designing and implementing solutions that can handle the
• PUBACK
diverse requirements of modern IoT systems.
• PUBREC
• PUBREL
The evolution of MQTT doesn’t stop at version 5. The
• PUBCOMP
MQTT Technical Committee is already working on further
• SUBACK
enhancements and features that will continue to advance the
• UNSUBACK
protocol’s capabilities. This ongoing work demonstrates the
• AUTH
strong commitment of the MQTT community to the continued
• DISCONNECT
growth and advancement of this technology, ensuring
that MQTT will remain a relevant and powerful tool for IoT
47
[Link]
MQTT Essentials Ebook
The range of Reason Codes for Negative Acknowledgments wholly conformed to the MQTT specification, comprehensively
varies widely, spanning from “Quota Exceeded” to “Protocol supporting all features. Pivoting toward the subject of
Error”. It is the joint responsibility of clients and brokers to unsupported features, MQTT 5 presents an ingenious
decode and comprehend these newly introduced Reason resolution. It enables those implementations that are not
Codes, further enriching the overall MQTT experience. entirely complete, often seen in SaaS offerings, to indicate the
broker’s inability to support certain features. The onus is then
Having established the custom key-value properties and placed upon the client to ensure these unsupported features
Reason Codes, let’s explore how MQTT handles unsupported are not utilized. The broker employs pre-defined headers in the
features and uses CONNACK return codes. CONNACK packet, sent in response to the client’s CONNECT
packet, to communicate the lack of support for specific
Responding to Unsupported Features with features. These headers can also be leveraged to notify the
CONNACK Return Codes client that they are not permitted to use certain features.
48
These return codes represent a significant stride forward persistent session concept. In MQTT 5, all sessions persist
in expressing the permissions of individual MQTT clients. unless the “Session Expiry Interval” is set to 0. Session
However, this new capability carries an inherent trade-off; deletion takes place either after the interval timeout or when
MQTT clients must implement the interpretation of these the client reconnects using Clean Start.
codes independently, and must ensure that application
developers avoid using features unsupported by the broker, or AUTH Packet in MQTT 5?
those the client lacks permission for.
MQTT 5 brings forth a new packet type: the AUTH
packet. Serving as a vital tool in implementing non-trivial
As a note of assurance, HiveMQ is fully
authentication mechanisms, we anticipate this packet to be
compliant with all MQTT 5 features, ensuring
integral in production environments. It’s crucial to understand
these custom headers would only be utilized
that this novel packet can be dispatched by both brokers and
at the administrator’s discretion for setting
clients post-connection establishment. It enables the use of
permissions in deployments.
intricate challenge/response authentication methods, such
as SCRAM or Kerberos as outlined in the SASL framework
and is also compatible with cutting-edge IoT authentication
Enhanced Session Management: From Clean methods like OAuth. Importantly, the AUTH packet allows for
Session to Clean Start in MQTT 5 the re-authentication of MQTT clients without requiring the
[Link]
MQTT Essentials Ebook
While the remaining data types might not be directly visible to already burdened MQTT clients. Consider a scenario where an
users, they are leveraged on the wire to construct valid MQTT MQTT client takes 11 seconds to process a message received
packets by MQTT client libraries and brokers. from a broker to acknowledge the packet after processing.
There is no tangible advantage if the broker retransmits the
How Does MQTT 5 Enhance Communication message after a 10-second timeout. This approach merely
with Bi-directional DISCONNECT Packets? consumes valuable bandwidth and further overwhelms the
Under the MQTT 3.1.1 framework, clients could gracefully MQTT client.
However, there was no provision for the MQTT broker to inform for healthy TCP connections is disallowed for both brokers
the client of a potential issue that necessitates closing the and clients. However, brokers and clients must resend
TCP connection. This shortcoming has been addressed in the unacknowledged packets when the TCP connection has been
new protocol version. severed. Thus, the QoS 1 and 2 guarantees remain as vital as
they were in MQTT 3.1.1.
This provision empowers the client to understand the cause instance, if your implementation fails to acknowledge packets
behind the disconnection and devise a suitable response under certain circumstances), we recommend reevaluating this
accordingly. While the broker is not mandated to disclose the strategy before upgrading to MQTT v5.
Under the MQTT 3.1.1 protocol, re-delivery of MQTT messages While the core of the MQTT protocol remains relatively
was permissible even when the TCP connection was healthy. unchanged, subtle refinements have been implemented under
This often proved detrimental in practice, risking overloading the surface, laying the groundwork for many new features
50
in version 5 of this widely adopted IoT protocol. As a user 6. Streamlined Migration
leveraging MQTT libraries, these modifications may seem 7. Improved features, including
minor and do not radically alter the way MQTT is utilized. 1. Negative Acknowledgments
However, for developers working on MQTT libraries and [Link] Aliases for Efficiency
brokers, these changes, particularly those related to protocol 3. User Properties for Customization
nuances, are critical and demand attention. 4. Payload Format Indicators
Some shifts in specified behavior, such as message Here is the rationale behind how and why MQTT 5 is better
retransmission, necessitate revisiting deployment design compared to MQTT 3.
decisions during the transition to MQTT 5.
1. How MQTT 5 is Better Than MQTT 3 in Error
Chapter 14: Key Reasons to Upgrade Handling
to MQTT 5 from MQTT 3.1.1 MQTT 5 significantly enhances error handling mechanisms,
contributing to the robustness and reliability of the system.
The Internet of Things (IoT) has evolved rapidly over the last One of the notable introductions is the session and message
decade, with MQTT emerging as the de facto protocol for the expiry feature. This allows developers to set a defined time
seamless and efficient communication of IoT devices. As the limit for each message and session. If a message isn’t
scale and complexity of IoT systems grow, the MQTT protocol delivered within this time frame, it’s automatically deleted. This
is also evolving to meet these demands. In this light, the MQTT feature is particularly useful in ensuring that network latency
5 upgrade presents significant improvements catering to or outages do not lead to delivering outdated or irrelevant
modern IoT applications’ expanding needs. commands to IoT devices.
Now, let’s explore why your organization or development team 2. How MQTT 5 is Better Than MQTT 3 in
should consider upgrading to MQTT 5 from MQTT 3.1.1. Offering Greater Scalability
Scalability is a crucial requirement in the modern world of
Why Upgrade to MQTT 5 from MQTT 3?
growing IoT networks. MQTT 5 addresses this by standardizing
MQTT 5 represents a substantial enhancement to the MQTT shared subscriptions. This allows multiple MQTT client
protocol, developed with valuable insights from MQTT instances to share the same subscription on the broker. It’s a
users. It incorporates features essential for contemporary powerful feature for load-balancing MQTT clients deployed on
IoT applications, tailor-made for cloud-based deployments. a cloud cluster. This scalability makes MQTT 5 a robust choice
These advancements promote resilience, dependable error for enterprises with large deployments and those planning to
management for executing crucial messaging, and facilitate scale up their IoT systems.
the straightforward integration of MQTT messages into
existing computational frameworks. 3. How MQTT 5 is Better Than MQTT 3
in Offering Greater Flexibility and Easier
In this chapter, we present seven reasons why your Integration
organization or development team should consider upgrading
Taking customization to a new level, MQTT 5 introduces
to MQTT 5; they include:
User Properties, a feature that allows for adding key-
1. Better Error Handling for More Robust Systems value properties to the headers of MQTT messages. This
2. More Scalability for Cloud Native Computing capacity means that application-specific information can
3. Greater Flexibility and Easier Integration be incorporated directly into each message, enhancing the
4. The Rise of a Single IoT Standard processing of these messages. For instance, a meta-tag that
5. Future-Proofing includes the unique identifier of the sending client or the
51
[Link]
MQTT Essentials Ebook
52
Payload Format Indicators
With the diverse types of data in IoT applications, it’s crucial
to have a mechanism that simplifies data handling and
processing. MQTT 5 meets this need with payload format
indicators. These indicators identify whether the payload is
binary or text and include a MIME-style content type. This
helps improve data processing efficiency and makes the
system more adaptable to various data use cases.
53
[Link]
MQTT Essentials Ebook
Why Were the Expiry Intervals Introduced in the session to be discarded would have to connect with the
MQTT 5? cleanSession=true flag set.
54
De-cluttering of state transition in MQTT 3.1.1 and MQTT 5 (courtesy of Ian Craggs)
MQTT 5’s Message Expiry Interval The longevity of message relevance to IoT devices can exhibit
significant fluctuations. Take the example of the connected
Much like its counterpart, the session expiry’s interval
car: traffic updates maintain their relevance only briefly. Yet,
inception was fueled by a need for an automated maintenance
when we consider over-the-air firmware upgrades, these need
mechanism. Consider the myriad of IoT devices, such as
to be executed even if the car remains offline for an extended
connected cars designed to weather extended periods of
period, stretching into several weeks.
internet disconnection. MQTT offers a lifeline for these
scenarios with persistent sessions and message queueing.
The Message Expiry Interval in MQTT 5 emerges
as the perfect tool to manage these differing
Messages crafted for offline devices are stored on the broker,
time frames, adding to the protocol’s versatility.
waiting for the device to regain connectivity for delivery.
In grand-scale deployments, where the count of connected
devices escalates into thousands or even millions, it’s
imperative to constrain the offline message queue for each
client individually.
55
[Link]
MQTT Essentials Ebook
Example: traffic jam alerts usually become obsolete after 1-2 hours, but a firmware update should be available for many weeks. Message
Expiry Interval is the perfect feature to define these different periods of time.
By assigning an optimal message expiry interval to messages Important Tips for MQTT 5 Session Expiry
with a limited relevance window and leaving perpetual Interval and the Message Expiry Interval
relevance messages without an expiry, we ensure effective
Here’re some important information:
utilization of broker resources for clients that could be
offline for a considerable duration. This strategy also spares • Both the Session Expiry Interval and the Message Expiry
clients from being inundated with irrelevant messages upon Interval are instrumental in refining resource management
reconnection. on the MQTT broker.
• Reflecting on the past, many MQTT 3 users expressed
For retained messages, the message expiry a need for expiry features. HiveMQ responded to this
interval operates similarly, guaranteeing that demand by introducing session and message expiry as
these messages are only dispatched to new supplementary features in our MQTT platform, predating
subscribers for a specified period. their standardization in MQTT 5.
• The equivalent for cleanSession=true in the CONNECT
packet of MQTT 3 is sessionExpiry=0 (or absent) and
One crucial aspect to note, however, is that when a client’s cleanStart=true in MQTT 5.
session expires, all queued messages for that client also • Likewise, cleanSession=false from the MQTT 3 CONNECT
expire in sync with the session, irrespective of their individual packet finds its counterpart in MQTT 5 as a sessionExpire
message expiry status. This “GOTCHA” is an important value exceeding zero, coupled with cleanStart=false.
reminder of the interconnectedness of sessions and their • MQTT brokers, such as HiveMQ, offer the capability to
queued messages in the MQTT protocol. configure a maximum value for these expiry intervals
on the server side. This feature is handy in multi-vendor
projects, particularly when the broker operator may lack
authority over the MQTT client settings.
56
The advent of MQTT 5 has ushered in a host of new features transparency and allow centralized system control by virtue of
designed to enhance the protocol’s usability, flexibility, and the broker.
efficiency. The Session Expiry Interval and the Message Expiry
Interval are prime examples of this, acting as invaluable tools Feedback on Connection Establishment in
in resource management and ensuring the smooth functioning MQTT 5
of your MQTT broker. Both these features truly embody the With MQTT version 5, it is now possible for MQTT brokers
user-centric evolution of the MQTT standard, demonstrating its to give additional feedback to MQTT clients on connection
responsiveness to the demands and challenges of its users. establishment. A number of different properties can be added
to the connection acknowledgment packet that tells the client
Chapter 16: MQTT 5’s Improved Client which features the broker supports or the client is allowed to
Feedback & Negative ACKs use. This includes the following MQTT features:
• Retained messages
In this chapter, we focus on MQTT 5’s Enhanced Client
• Wildcard subscriptions
Feedback and Negative ACKs(NACKs). We will dissect
• Subscription identifiers
this method that MQTT 5 implements for MQTT brokers to
• Shared subscriptions
acknowledge clients, examining their potential to simplify the
• Topic aliases
work of developers and operations teams, and making the
• Maximum quality of service level the client can use
protocol more robust and efficient.
while crafting the new MQTT 5 protocol standard. Among the • Session expiry interval
primarily due to the insufficient return codes and limited means • Maximum number of topic aliases the client can send
debugging complexity, particularly in multi-vendor projects. to configure a maximum for the limits and to disable MQTT
features that are not needed in your use case.
MQTT 5’s Specific Feature Set Moreover, MQTT 5 introduces the possibility of integrating
Let’s focus on several MQTT 5 features that provide more reason codes into packets that previously lacked this attribute
in versions 3.1 and 3.1.1. These packets include UNSUBACK,
57
[Link]
MQTT Essentials Ebook
PUBACK, PUBREC, PUBREL, DISCONNECT, and PUBCOMP. This a comprehensive understanding of why the connection was
enhancement further strengthens the protocol’s transparency severed.
and troubleshooting efficiency, illustrating the forward strides
made in MQTT 5. This streamlined method of connection termination not only
adds clarity for the client but also substantially simplifies
Enhancing Clarity with Contextual Reason pinpointing the reason behind a broker-initiated connection
Strings in MQTT 5 closure. This significant enhancement in MQTT 5 dramatically
While adding additional reason codes certainly bolsters the improves the communication transparency between broker and
58
Exceeding the maxPacketSize is one of the new reason codes for negative acknowledgments.
59
[Link]
MQTT Essentials Ebook
with additional metadata, facilitating a fluid transmission of initially seem minor, the practical implications of possessing
information between the publisher, broker, and subscriber. a mechanism to transfer metadata across the complete
MQTT ecosystem are indeed substantial. To illustrate this
Conceptually, this feature closely mirrors the transformative potential, let’s delve into three common
role of headers in HTTP. It’s this resemblance use cases that underscore the need for a feature like User
that allows User Properties to inject a level of Properties—a need articulated repeatedly by users eagerly
customizable complexity into MQTT 5, helping awaiting the introduction of this component in the MQTT
to create a protocol that is not only more robust specification.
but also more adaptable to user needs.
Saving Resources with Payload Metadata Using User
Properties in MQTT 5
Why Were User Properties Introduced in
MQTT 5? In environments where MQTT serves as a connector between
diverse systems developed by different teams or vendors,
MQTT 3 users identified two significant limitations: the
variability in payload structures is quite commonplace. Clients
protocol’s constrained extensibility and the complexity of
may transmit data in many formats, including JSON, XML, or
creating multi-vendor deployments. To rectify these concerns,
compressed formats such as Protobuf.
MQTT 5 introduced the User Properties feature, effectively
mitigating these challenges.
The advent of User Properties in MQTT 5 opens the door to
appending metadata to messages, encapsulating specific
User Properties provide an avenue for enhancing flexibility by
details such as the markup language and version employed
enabling users to transport virtually any piece of information
to encode the payload. This metadata provision obviates the
across the entire MQTT system. This capability ensures
need for the receiving client, or in certain instances, the broker,
that the MQTT protocol no longer restricts but promotes
to unpack the payload and cycle through an array of possible
customized enhancements. This leap forward in functionality
parsers until the appropriate one is located.
paves the way for users to augment standard protocol
features, tailoring them to meet their specific requirements.
Instead of this cumbersome process, each message
arrives equipped with its parsing information, streamlining
In doing so, MQTT 5 ensures that the protocol evolves in step
interpretation and significantly reducing the computational
with its users, facilitating greater adaptability and easing the
load across the entire system. This efficient use of resources
integration of multi-vendor deployments.
amplifies the overall performance and speed of the MQTT
Practical Use Case Examples of MQTT 5 User network, showcasing the transformative power of User
Properties Properties.
60 Including metadata about the used markup language in the payload can significantly relieve the system.
Increased Efficiency Through Application Level Routing Using User Properties in MQTT 5
With its proficiency in data transportation and routing, MQTT frequently serves as the backbone for large-scale data processing and
streaming deployments. Such deployments typically involve a multitude of devices, systems, and applications. It’s quite common for
different systems to receive identical messages but for distinct purposes. For instance, one system may display live data while other
archives the same data for long-term storage.
In such scenarios, User Properties can prove invaluable by serving as an additional application-level timestamp for the message.
This attribute lets the broker quickly ascertain whether certain messages should not be passed to a specific subset of subscribers
based on a predefined validity period. This feature introduces an added application-level layer that further refines message relevance
according to the Message Expiry Interval.
Therefore, User Properties in MQTT 5 not only bolster the system’s efficiency but also provide a finer level of control, thereby
maximizing the utility and relevancy of each transmitted message.
Example: user properties provide information for the broker, if messages should be routed to a storage or a display application.
In this regard, the advent of the User Property feature in MQTT 5 heralds a significant transformation. This innovative addition
enables publishers to effortlessly include relevant self-identifying information, such as a client ID or the region where the publishing
is conducted. Importantly, this information is relayed to all message recipients without necessitating any supplemental business
logic.
Incorporating information about the publisher’s region enhances the system’s traceability while attaching a unique system identifier
to MQTT messages enables comprehensive logging and tracking of the entire message flow from the sender to all subscribers.
Implemented effectively, these identifiers can extend across multiple MQTT message flows, introducing unprecedented transparency
and traceability.
61
[Link]
MQTT Essentials Ebook
Adding information about the publisher’s region adds traceability to the system.
Such capabilities unlock a new horizon of possibilities, User Properties in MQTT 5 represent a significant step forward
particularly for business-critical applications like premium in protocol extensibility and versatility, opening up exciting
paid services for end customers, where transparency and possibilities for future IoT applications.
traceability become indispensable.
Chapter 18: MQTT Shared
Additional Information on User Properties in Subscriptions
MQTT 5
• User properties serve as UTF-8 string key-value pairs that In this chapter, we’ll delve into an especially interesting
can seamlessly be incorporated into any MQTT message. feature: Shared Subscriptions.
• The implementation potential of User Properties in 5, enable multiple MQTT clients to share a
enhancing MQTT use cases is practically boundless. It single subscription on the broker. In essence,
offers a degree of customization that allows for many this feature allows messages on a topic to be
innovative applications, both in function and scope. distributed among multiple clients, thereby
• Deployments and projects that extend over multiple improving load balancing and fault tolerance in
62
expectations for the Internet of Things’ de facto standard that shared subscriptions employ a unique topic syntax for
protocol. With the integration of sought-after features such subscribing.
as Shared Subscriptions and Session and Message Expiry
Intervals, MQTT 5 is poised to consolidate MQTT’s standing as Shared subscriptions use the following topic structure:
the go-to IoT protocol for the foreseeable future.
$share/GROUPID/TOPIC
compatibility with the MQTT 5 specification must support this • A group identifier
How MQTT Shared Subscriptions Work A concrete example for such a subscriber would be:
Shared Subscriptions
63
[Link]
MQTT Essentials Ebook
For instance, a HiveMQ deployment might feature multiple How to Subscribe with Shared Subscriptions
shared subscription groups. These groups can have the in MQTT?
same subscription but different group identifiers. Whenever a
Engaging your clients with shared subscriptions is a
publisher sends a message with a matching topic, one unique
straightforward process. Below, we illustrate how to
client from each group receives the message. For example, the
accomplish this using the MQTT CLI. The given command line
following scenario is possible:
execution code allows two MQTT clients to subscribe to the
same subscription group and topic:
• Worker (backend) applications that ingest MQTT streams subscriptions, the official HiveMQ Documentation is an
64
subscribers as needed, distributing work in a push fashion. indicates an “unspecified byte stream” while a 1 represents a
Shared subscriptions prove immensely valuable for scaling and “UTF-8 encoded payload.” When the Payload Format Indicator
load-balancing MQTT clients. Furthermore, HiveMQ clusters isn’t provided, it automatically defaults to 0.
offer additional benefits in terms of latency and scalability
through internal optimization of message routing.
For UTF-8
What is Payload Format Indicator in MQTT?
encoded
The Payload Format Indicator is a fundamental component strings
of any MQTT packet that houses a payload. This includes a Content
CONNECT packet encapsulating a WILL message or a PUBLISH Types can be
packet. This optional byte value has two possible settings: a 0 defined.
Payload Format Description allows pre-parsing without the need to open the payload 65
[Link]
MQTT Essentials Ebook
Having the payload content description in the headers can What is Request-Response Pattern in MQTT?
prove incredibly beneficial in individual deployments. It
MQTT is rooted in asynchronous messaging, adopting the
ensures every message is correctly processed without delving
publish-subscribe paradigm. This design allows senders and
into the payload itself. Depending on the content type, different
receivers to function independently of one another, facilitating
messages within a system may need various parsing methods.
one-to-many relationships. It’s vital to grasp that MQTT’s
Moreover, in certain instances, message persistence could
request-response pattern tackles challenges differently from
hinge on the payload’s specific type. As the content-type
synchronous, one-to-one-based protocols like HTTP.
definitions hinge on user design, the potential applications of
this feature appear boundless.
In MQTT, a response typically doesn’t directly
answer a request’s “question”. Instead, the
In summary, the Payload Format Indicator discerns whether
request triggers a specific action in the receiver,
a payload is an undefined byte array or a UTF-8 encoded
and the response communicates the result of
message. When dealing with UTF-8 encoded messages, the
this action.
sender can use the content type to specify the payload’s
nature.
Sounds complicated? Don’t fret; a concrete example will soon
bring this into perspective!
These features set the stage for transparent payload content
definitions across large-scale systems and, potentially,
What is Response Topic in MQTT 5?
entire industries. As the need for pre-parsing actual payloads
diminishes, proper message processing can considerably A response topic is an optional UTF-8 string incorporated
enhance scalability. into any PUBLISH or CONNECT packet. If a value is present
in the response topic, the sender immediately classifies the
Although it’s anticipated that most users will rely on known associated PUBLISH as a request. The response topic field
MIME types to describe the content, they can also use arbitrary indicates the topics where the message receivers’ responses
UTF-8 Strings. are expected. The initial PUBLISH (request) and the response
topic can have multiple or no subscribers. Ideally, the original
Chapter 20: MQTT Request-Response PUBLISH (request) sender should subscribe to the response
topic before dispatching the request.
Pattern
In this chapter, we spotlight two standout elements: Response What is Correlation Data in MQTT 5?
Topic and Correlation Data. Correlation data is optional binary data that trails the
response topic. It helps the request’s sender identify which
Navigating the complexities of modern IoT projects demands specific request a later received response relates to. The
teamwork across diverse vendors and teams. As MQTT correlation data allows the original request sender to manage
has become the protocol par excellence for IoT, enhanced asynchronous responses potentially sent from multiple
interoperability and system transparency emerged as prime receivers. It’s important to note that this data is not relevant
requirements in the MQTT version 5 blueprint. The features to the MQTT broker but serves to identify the relationship
we’re delving into today answer these user needs by providing between sender and receiver.
a standard solution for implementing a request-response
pattern with MQTT. What is Response Information in MQTT 5?
To foster transparent implementation and improved
standardization, the MQTT 5 specification introduced the
66
Response Information property. Through a boolean field in the CONNECT, a client can request response information from the
broker. When set to true, the broker can dispatch an optional UTF-8 String field (response information) in the CONNACK packet to
communicate anticipated response topics.
This feature allows users to globally define a specific part of your topic tree on the broker, accessible by all clients indicating their
intention to use the request-response pattern at the connection establishment.
Example: Smart door opening with a mobile device using the MQTT 5 Request Response feature.
The inclusion of the request-response pattern in the MQTT directly into protocol fields, we’ve significantly enhanced
5 specification was largely driven by the need for these application development’s versatility, dynamism, and
“business ACKs.” MQTT users sought the capability to transparency under the request-response pattern.
provide end-to-end acknowledgments between an application
message’s sender and receiver. Adding response topics, Source Code Example of MQTT Request-
correlation data, and response information as protocol Response Workflow
fields significantly bolsters extensibility, dynamism, and Below is a quick code snippet showcasing the HiveMQ MQTT
transparency in application development using the request- Client’s capabilities, providing a taste of the request-response
response pattern. pattern workflow in MQTT. Note that it’s not a complete,
functioning excerpt.
MQTT 3 users previously embraced this pattern. By integrating
response topics, correlation data, and response information You can access a full example on GitHub.
67
[Link]
MQTT Essentials Ebook
68
Best Practices to Consider While Using Why Use Topic Aliases in MQTT?
Request-Response in MQTT
MQTT plays a pivotal role in your network with its efficiency
Here are a few best practices to consider while using request- in maintaining standing connections between your devices
response in MQTT: and the brokers. The Keep Alive mechanism guarantees the
longevity of connections between clients and the broker,
• Ensure the requester subscribes to the relevant response
swiftly detecting any connection loss that could occur in
topic before sending a request.
unstable networks. PING packets - of just two bytes - need
• Employ unique identifiers within the response topic to
to be sent only every few minutes, enabling MQTT to sustain
avoid confusion.
these connections with minimal power and bandwidth use.
• Ascertain that responders and requesters possess the
necessary permissions to publish and subscribe to
This brings us to the Topic Alias feature, which is particularly
response topics.
beneficial in deployments involving a vast array of connected
• Dedicate a specific section of the topic tree for response
devices transmitting smaller, frequent messages. So, let’s
purposes, and utilize the response information field to
dive into this feature and see how it can optimize your MQTT 5
relay it to clients.
utilization.
69
[Link]
MQTT Essentials Ebook
70
Response Authentication Mechanism (SCRAM) or the Kerberos protocol. These widely recognized protocols further bolster the
security of your IoT infrastructure by adding a layer of verification.
The core of the authentication flow revolves around two message properties: the Authentication Method (identified by byte 21) and
the Authentication Data (identified by byte 22). These properties are set on every message involved in the enhanced authentication
flow.
Authentication Flow
The Authentication Method assigns significance to the In this code snippet, we utilize the HiveMQ extension SDK to
exchanged data during enhanced authentication and should implement enhanced authentication. The purpose is to verify
remain constant throughout the process, ensuring consistency the support of the Authentication Method and determine the
and integrity. state of an MQTT client that is connecting after the exchange
of two AUTH messages.
Authentication Data in MQTT
Authentication Data refers to binary information utilized during
71
[Link]
MQTT Essentials Ebook
The significance of Enhanced Authentication cannot be overstated. In a world where the proliferation of interconnected devices has
amplified the importance of secure communication, MQTT 5 steps up to the challenge. This advanced authentication mechanism
empowers organizations to safeguard their IoT infrastructure, sensitive data, and the privacy of their users.
72
Chapter 23: MQTT Flow Control Similarly, an IoT device might connect to multiple MQTT
brokers, each with distinct limitations on managing in-flight
Flow Control is a dynamic feature introduced in MQTT 5 messages from an MQTT client. To seamlessly manage such
designed to regulate message traffic between IoT devices and diversified conditions among MQTT clients and brokers, MQTT
brokers for efficient and stable communication. 5 introduces the Flow Control feature.
IoT deployments encapsulate a wide range of device types. How Flow Control Works in MQTT 5?
For instance, an MQTT client embedded in a compact
The Flow Control feature functions through a negotiation
sensor varies significantly from one incorporated in a high-
between the client and broker to establish in-flight windows
performance backend server regarding processing speed
during the connection. This process involves setting an
and storage capabilities. Consequently, these MQTT clients
optional property known as "Receive Maximum" in the
demonstrate varying tolerance levels for managing in-flight
CONNECT packet, indicative of the maximum number
messages. Here, an in-flight message refers to a PUBLISH
of unacknowledged PUBLISH messages the client can
command with a Quality of Service level of one or two
accommodate. The broker reciprocates with a similar value for
awaiting acknowledgment.
“Receive Maximum” in the CONNACK packet. If the "Receive
Maximum" value is not specified, the default value of 65,535 is
employed.
What Are the Advantages of Flow Control in sends more unacknowledged messages than what the Server
MQTT 5? Receive Maximum permits, the broker sends DISCONNECT
with Reason Code 0x93 (Receive Maximum exceeded). This
Flow Control enhances dynamic message flow adjustment for
flexibility allows the client and broker to send fewer in-flight
use cases involving diverse systems and devices, fostering
messages than the corresponding Receive Maximum permits.
transparency and adaptability when multiple teams or vendors
collaborate on a project. No longer is it necessary for all
parties to pre-establish in-flight windows. If an MQTT 5 client
73
[Link]
MQTT Essentials Ebook
What to Do and What Not to Do among millions of subscribers is a challenge and how an
MQTT broker can overcome this challenge.
• Implementing “Receive Maximum”
remains an optional, yet beneficial
What is Topic Matching in MQTT?
choice.
• Both client and broker can establish MQTT is a publish/subscribe protocol where devices act as
their unique in-flight windows during the MQTT Clients and exchange messages over an MQTT Broker.
connection initiation. MQTT Clients send their data in the PUBLISH control packets
• Flow Control is designed to maintain to the specific topic. The topic is separate from the packet’s
balanced message processing, payload, which allows the broker to avoid analyzing the
preventing the overloading of any packet’s payload. The broker delivers the published message
objectives - enhancing transparency and topic filter is that the topic is used for publishing and cannot
fostering increased flexibility. contain wildcard characters whereas the topic filter can. The
wildcard characters are used to aggregate multiple streams
of data into one and are thus used on the subscriber’s side. It
Chapter 24: MQTT Topic Tree & is possible to create a topic filter without wildcard characters,
then it would only match at most one topic. That case is often
Topic Matching: Challenges and Best
referred to as an exact subscription.
Practices Explained
As both the size and complexity of IoT projects continues to In a nutshell, topic filter can be thought of as a selector for
grow, we talk to many IT architects who are working to solve topics that the PUBLISH packets are sent to. The broker must
the technical challenges to design a data foundation built for be able to find the matching subscriptions for each published
74
Subscriptions can contain wildcard characters to match a • +/house/kitchen
broad range of topics. Subscriptions with wildcards are often • town/+/kitchen
used when there is uncertainty about the topics that publishing • town/house/+
clients will use. For example, when the publishing clients • +/+/kitchen
include their ID as one topic level, it may be impossible to • town/+/+
reliably receive messages for all such topics without the usage • +/+/+
of wildcard characters. While this is useful for the clients, • town/house/kitchen
finding all the matching subscriptions presents a technical • …
challenge. In some real-life scenarios, brokers check millions
of subscriptions for every published message. The broker must also check the map for all these topic filters.
In production workloads, the broker has to find the matching
MQTT Wildcard Topic Matching Challenge subscriptions for published messages thousands of times
Explained per second, so it needs a specialized data structure for a fast
Since there are many use cases for wildcards, let’s examine the lookup.
[Link]
MQTT Essentials Ebook
The broker can continue delivering the published message In summary, MQTT’s topic matching is crucial to its publish/
to the subscribed clients once it has found all matching subscribe protocol, enabling MQTT clients to exchange
subscriptions. This way of storing the subscriptions also messages with the MQTT broker with minimal effort. Topic
reduces memory usage because topic levels shared across filters help select the topics to which PUBLISH packets are
multiple subscriptions are only stored once. sent, and subscriptions with wildcard characters enable broad
topic matching. However, finding all matching subscriptions
presents a technical challenge, it can be solved using a
specialized data structure called the Topic Tree. It is essential
Best Practices to use best practices when designing topics to make them
For your application to filter messages to your agnostic of the implementation that the particular MQTT
specifications regardless of how the MQTT Broker broker may have for topic matching.
defines topic matching, there are a few topic design
considerations that you may leverage. Chapter 25: Additional Reading for
Mastering MQTT
It is good practice to avoid topic levels that do not
add additional information, like using the same topic MQTT Over WebSockets
level across all subscriptions. The most common
We’ve seen that MQTT is ideal for constrained devices and
example of such abuse is using the company name
unreliable networks and that it is perfect for sending messages
as the first level for every subscription. While some
with a very low overhead. Naturally, it would be quite nice
topic levels typically have less variety than others,
to send and receive MQTT messages directly in a browser.
you should omit topic levels that are the same for
For example, on a mobile phone. MQTT over WebSockets is
every topic. Similarly, leading with forward slashes
the answer. MQTT over WebSockets enables the browser to
must be matched, so should be avoided if you don’t
leverage all MQTT features. You can use these capabilities for
want them present on all topics.
many interesting use cases:
• /livingarea/kitchen What Does All This Mean from a Technical Point of View?
• /livingarea/bathroom
Every modern browser that supports WebSockets can be a
• /garage
full-fledged MQTT client and offer all the features described in
the MQTT Essentials. The Keep Alive, Last Will and Testament,
Good practice:
Quality of Service, and Retained Messages features work
• livingarea/kitchen the same way in the browser as in a native MQTT client.
• livingarea/bathroom All you need is a JavaScript library that enables MQTT
• Garage over WebSockets and a broker that supports MQTT over
WebSockets. Of course, HiveMQ Broker offers this capability
straight out-of-the-box.
76
How Does It Work? • If you want to integrate MQTT into your existing web
application, check out this step-by-step guide on how to
WebSocket is a network protocol that provides bi-directional
build your own MQTT WebSockets client.
communication between a browser and a web server. The
• To learn more about how to set up your own broker with
protocol was standardized in 2011 and all modern browsers
WebSockets support, read MQTT over WebSockets.
provide built-in support for it. Similar to MQTT, the WebSocket
protocol is based on TCP.
Secure WebSockets
You can leverage Transport Layer Security (TLS) to use
secure WebSockets with encryption of the whole connection.
This method works seamlessly with HiveMQ. However, there
are a few points that you need to keep in mind. For more
information, see the Gotchas section of our user guide.
Get Started
If you want to get started with MQTT over WebSockets, here
are some useful resources:
[Link]
MQTT Essentials Ebook
Open Source vs. Commercial MQTT Brokers On-premises MQTT Brokers vs. Fully-Managed Cloud
MQTT Brokers
Open-source MQTT brokers have limited scalability, limited
security options, cannot cluster for higher availability, are On-premises MQTT brokers offer more control but require
hard to manage when coded in difficult libraries, and have no substantial setup and maintenance. Fully managed cloud-
overload protection from overactive publishers. based MQTT brokers are easier to deploy, cost-effective, and
have zero maintenance requirements.
On the other hand, commercial MQTT brokers are customizable
according to your needs, provide increased scalability, security, HiveMQ offers an on-prem MQTT broker as well as a fully-
flexibility & reliability, and have overload protection. managed cloud MQTT broker. Explore them now!
MQTT HTTP
78
MQTT HTTP
2 bytes minimum. Header data can 8 bytes minimum (header data is text - compression
Message overhead
be binary possible)
MQTT AMQP
Publish subscribe (MQTT does have Queues, multicast (fanout), publish subscribe,
Architecture
a request/reply mode as well) request reply
79
[Link]
MQTT Essentials Ebook
MQTT AMQP
Connection
No Yes - channels
“multiplexing”
80
MQTT vs. ZeroMQ
MQTT ZeroMQ
81
[Link]
MQTT Essentials Ebook
MQTT ZeroMQ
Message
No Yes
fragmentation
Pub/sub topic
Level separator: / Wildcards: + # Prefix only
matching
MQTT CoAP
82
MQTT CoAP
Message
Reliant on TCP No (RFC 7959 proposes an extension for this)
fragmentation
[Link]
MQTT Essentials Ebook
MQTT CoAP
Pub/sub topic
Level separator: /Wildcards: + # No pub/sub (but extensions have been proposed)
matching
Message distribution One to many, one to one One to many, one to one
Implementing MQTT in C
To get started with the Paho MQTT C Client library, clone the repo and use the following code to install it.
Developers can use different client libraries to implement MQTT in different languages, such as Java, C, C#, etc.
<repositories>
<repository>
<id>Eclipse Paho Repo</id>
<url>[Link]
</repository>
</repositories>
84
Then add the following code to your dependencies section.
<dependency>
<groupId>[Link]</groupId>
<artifactId>[Link].mqttv3</artifactId>
<version>1.0.2</version>
</dependency>
Implementing MQTT in C#
You can install the HiveMQ C# MQTT client by using the code:
85
[Link]
MQTT Essentials Ebook
To learn more, read our blog Building New Connected Car Features on the Back of a Robust Connectivity Platform.
Here’s an example of how an MQTT broker, like HiveMQ, can help real-time communication between several vehicles and backend
systems.
86
MQTT Broker Architecture in the Energy Industry
MQTT brokers can play a crucial role in optimizing the operations and OT/IT interoperability in the energy industry. MQTT brokers
can be used in energy use cases such as Asset Performance Management, Predictive Maintenance, Asset Tracking, etc.
Here’s an example of how an MQTT broker, like HiveMQ, can be used for Remote Asset Management in Oil & Gas.
Chapter 26: Next Steps – Choosing • Filtering and routing messages: MQTT brokers can filter
messages based on the subscription topic and determine
the Right MQTT Broker
which client(s) should receive the message.
Choosing the right MQTT broker depends on your architectural • Session management: MQTT brokers can maintain
requirements (a.k.a. Non-Functional Requirements). An MQTT session data for all connected clients, including
broker comparison based on these architectural requirements subscriptions and missed messages, for clients with
should give you insight into how to find the best MQTT broker persistent sessions.
for your needs. • Authentication and authorization: The broker is
responsible for authenticating and authorizing clients
Here are some of the functionalities you should look for in an based on credentials provided by the client. The broker
MQTT broker if you want to use it in large-scale production: is extensible, facilitating custom authentication,
authorization, and integration into backend systems. In
• Handling large numbers of concurrent connections
addition to authentication and authorization, brokers may
reliably: Depending on its implementation, a broker
provide other security features, such as encryption of
has the capability to manage millions of MQTT client
messages in transit and access control lists.
connections reliably and securely, with a robust load
• Scalability and monitoring: An MQTT broker must
balancing feature. This facilitates communication
be scalable to handle large volumes of messages
between diverse devices, networks, and software systems
and clients, integrate into backend systems, be easy
in near real-time.
to monitor, and be failure-resistant. To meet these
87
[Link]
MQTT Essentials Ebook
requirements, the MQTT broker must use state-of-the- Here are some of the functionalities you should look for in an
art event-driven network processing, an open extension MQTT Broker if you want to use it in small-scale projects:
system, and standard monitoring providers. Brokers
• Ease-of-use: For small-scale projects, especially DIY
may also provide advanced features for managing and
projects and PoCs, an MQTT broker should be easy to
monitoring the MQTT system, such as message filtering,
deploy. There are several open-source and fully-managed
message persistence, and real-time analytics.
MQTT brokers that help you connect devices with minimal
• Clustering: MQTT brokers can support clustering, allowing
effort.
multiple instances of the broker to work together to
• Scalability: MQTT brokers should be able to help you
handle large numbers of clients and messages.
scale as you go so your PoCs turn into production-level
• Provides control over how IoT data flows within your data
projects soon.
pipeline: Prominent MQTT brokers can provide you with
• Cost-efficiency: There is pay-as-you-go pricing available
the ability to have control over your data with visualization
for MQTT brokers so you don’t have to constrain yourself
and management tools, powerful security features, and
while working on a PoC.
an integrated policy engine that can validate, enforce, and
• Security: It goes without saying how important it is
transform data in motion.
to secure your device connectivity from end to end,
irrespective of your scale of the project.
HiveMQ Self-Managed Enterprise Broker offers all the above
features. Hundreds of active customers, across the globe trust
HiveMQ Cloud is a fully managed MQTT broker that offers all
HiveMQ MQTT Broker for reliable, flexible, observable, secure,
the features above. Do check it out.
and scalable data communication.
88
HiveMQ is active in the open source community, working with several
organizations to advance the adoption of the MQTT protocol for IoT.
89
[Link]
About HiveMQ
Visit [Link]
to learn more
[Link]