Skip to content

BLOG

What Is MQTT in IoT?

Internet of Things (IoT) and machine-to-machine (M2M) technologies require a messaging and connectivity protocol to exchange information from remote locations.

When choosing such a protocol, several desirable features come into play:

  1. Small code footprint (to make it easy to implement in small devices)
  2. Low power consumption
  3. Low bandwidth consumption
  4. Low latency
  5. Use of a publish/subscribe (“pub/sub”) pattern

MQTT (Message Queuing Telemetry Transport) fulfills all of these requirements and has gained momentum with the support of major public clouds — Amazon Web Services, Microsoft Azure, and Google Cloud Platform. In this article, we’ll explore why MQTT is the most popular choice of messaging protocol for IoT products.

What is MQTT?

MQTT is a lightweight messaging protocol that was developed by IBM and first released in 1999. It uses the pub/sub pattern and translates messages between devices, servers, and applications.

Originally, MQTT was created to connect sensors on oil pipelines with communications satellites, with a focus on minimizing battery usage and bandwidth consumption.

MQTT has seen ongoing development since its inception. Version 5.0, released in May 2018, brought significant changes and enhancements to the MQTT protocol.

How Does MQTT Work?

MQTT Architecture

MQTT architecture graphic

In the MQTT protocol, connected devices are known as “clients,” which communicate with a server referred to as the “broker.” And what is an MQTT broker? The broker handles the task of data transmission between clients.

Whenever a client (also known as the “publisher”) wants to distribute information, it will publish to a particular topic. The broker then forwards this information to any clients that have subscribed to that topic (known as “subscribers”).

Publishers don’t need data on the number or the locations of subscribers, and subscribers don’t require any data about the publisher. Any client can act as a publisher, subscriber, or both. Typically, clients aren’t aware of each other — only of the broker that serves as the intermediary. This setup is commonly known as the “pub/sub model”.

Types of MQTT Messages

In MQTT protocol, clients use two main types of messages: publish and subscribe.

When a client wants to send data to the broker, this is known as a “publish.” Conversely, when a client wants to receive data from the broker, it will “subscribe” to a topic or topics. Upon subscribing to a topic, the client receives all messages published on that topic going forward.

We commonly refer to published data as “messages.” Messages can be anything one part of a system needs to communicate to other parts. Messages might be raw sensor readings, log messages, telemetry data, or heartbeats. Some messages might be more like commands, prompting a specific action in the receiving clients.

Along with the message itself, the publisher also sends a QoS (Quality of Service) level. This level defines the guarantee of delivery of the message. These QoS levels are as follows:

  1. At most once: When the message is published, the broker will only receive the message “at most once.” This level shouldn’t be used for mission-critical information since it carries the risk that the subscribers won’t receive the message.
  2. At least once: The publisher resends the message until it receives an acknowledgment from the broker. In other words, it’s more important that the message is received than it is to ensure it’s only received once. This is the most commonly used QoS level.
  3. Exactly once: The publisher and broker work together to ensure the message is received and acted upon exactly once. This level involves additional overhead in the form of a four-part handshake. Although this is the safest QoS level, it is also the slowest and therefore only used when necessary.

How Does MQTT Work in IoT Projects?

Let’s explore how you can use MQTT in IoT projects, using one of our clients as an example.

An automotive battery company wanted to provide “fresher” batteries to customers across the country by implementing a “first in, first out” strategy to avoid batteries sitting on the shelf for too long.

Of course, this required the company to track the dates of arrival of the stock on their shelves. In need of a trusted IoT partner, the company reached out to Very.

Very helped install IoT sensors on the company’s batteries and on their shelves. These sensors transmit information via MQTT to Amazon Web Services (AWS) in the cloud. Each battery has a signal-emitting device that sends a Bluetooth signal to convey its presence on the rack. Additionally, a single battery-powered hub wakes up once per day in order to transmit information to AWS using MQTT (as well as the TLS protocol for secure transmission).

This example showcases how MQTT is commonly used to gather sensor data from field-deployed devices and centralizing it for processing with machine learning algorithms. It also shows how handy MQTT can be for coordinating the behavior of devices in different locations by sending command messages to them.  

MQTT Use Cases

MQTT is a versatile messaging protocol with numerous use cases in the IoT landscape. Here are some common applications:

  1. Telemetry and Sensor Networks: MQTT is ideal for collecting data from sensors in environmental monitoring, industrial automation, and smart agriculture.
  1. Home Automation: MQTT enables seamless communication between smart devices for controlling and monitoring home systems like lighting, security, and thermostats.
  1. Industrial IoT: MQTT facilitates efficient data exchange between machines, enabling predictive maintenance, remote monitoring, and process optimization.
  1. Asset Tracking: MQTT’s ability to handle intermittent connectivity and low power consumption makes it valuable for real-time location tracking of vehicles and valuable assets.
  1. Energy Management: MQTT is employed in smart energy systems for efficient communication between smart meters, energy management systems, and devices, enabling load balancing and energy optimization.
  1. Healthcare and Wearables: MQTT facilitates remote patient monitoring, real-time health data transmission, and integration with wearable devices in healthcare settings.


MQTT finds extensive application in various IoT scenarios, demonstrating its versatility, efficiency, and reliability. Now let’s explore the benefits of MQTT in more detail.

What Are the Benefits of MQTT?

MQTT protocol offers several benefits for IoT applications:

  1. Lightweight code footprint: Devices need only a few lines of code in order to get up and running with the MQTT protocol.
  2. Minimized data packets: MQTT is very energy-efficient, making it ideal for battery-powered devices or those with limited CPU power.
  3. Speed: MQTT operates in real-time, ensuring no delays outside of QoS.
  4. Ease of implementation: MQTT already has libraries in programming languages such as Elixir and Python.
  5. Last will and testament: In case a client unexpectedly disconnects, you can set message instructions to be sent to all subscribers in order to remedy the situation.
  6. Retained messages: Each topic can have one retained message that a client automatically receives when it subscribes (like a pinned post on social media).

What Are the Drawbacks of MQTT?

Here are things that might be problematic for your use case:

  1. Message Ordering: Ordering guarantees are not absolute in MQTT protocol. Although brokers generally send messages on the same topic in the order received, the specified QoS level can affect this. The protocol specification allows brokers to treat individual topics as unordered, and ordering across topics is not part of the specification.
  2. Maintaining Healthy Connections: While the protocol supports reestablishing connections after timeouts or crashes, it’s essential to verify if your client requires application code to implement this functionality.
  3. Throttling: Overloading a client with excessive messages is possible. If neither your client nor your broker handles this automatically, you’ll need to implement application code to manage it.

Common Alternatives to MQTT

While MQTT protocol is widely used in IoT, there are alternatives to consider:

XMPP

XMPP (Extensible Messaging and Presence Protocol) is a communications protocol based on the XML language for storing and transporting data. It’s frequently used to power instant messaging services such as Jabber.

Some of the primary differences between XMPP and MQTT are:

  • The XMPP code footprint is slightly heavier, and you need an XML parser to encode and decode information.
  • XMPP doesn’t have support for the pub/sub model by default (although it can with an extension).
  • XMPP takes up more bandwidth than MQTT.

HTTP(S)

HTTP (Hypertext Transfer Protocol) and its extension HTTPS (Hypertext Transfer Protocol Secure) are foundational communications protocols for the World Wide Web. However, they are stateless and carry more overhead per transmission than MQTT. Additionally, HTTPS has a lower throughput than MQTT, meaning that you can’t send as many messages in the same period of time.

The Impact of MQTT on IoT

MQTT plays a crucial role in IoT projects by providing a more “low-lift” solution in terms of technical specifications while achieving the desired connections among devices, servers, and applications. Its lightweight nature, energy efficiency, real-time operation, and ease of implementation make it a popular choice in the IoT landscape.

KEEP READING: Find out how Very helped an alcohol monitoring company develop a powerful IoT connectivity solution, delivering real-time results for their customers.

IoT insights delivered to your inbox