Have you ever wondered how devices communicate with each other? Whether it’s your phone syncing data to the cloud or your activity data being sent to a phone application, you’ve experienced machine-to-machine communications in some way. One example of using these communications is the Message Queuing Telemetry Transport (MQTT) protocol.
In this article, we will share more about the basics of MQTT protocol in IoT, how MQTT works in IoT, the key components that exist in every MQTT project, and some of the most used features in the MQTT protocol.
What is MQTT protocol in IoT?
As mentioned, the MQTT protocol is used in machine-to-machine communications, particularly in Internet of Things (IoT) devices. Some common examples include smart sensors, wearable trackers, and even global positioning systems. The MQTT protocol uses a publish/subscribe model and is most commonly used in networks with low latency or unreliable networks.
How did the MQTT protocol come about?
MQTT was created in 1999 by IBM and designed for monitoring oil pipelines within an industrial control system. At that time, there was a need to send data from far away pipeline sensors to remote servers in a reliable and cost-effective manner. Since then, the MQTT protocol has evolved significantly and anyone can adapt and implement it according to their needs.
The MQTT protocol specification has also undergone several upgrades since its initial creation. As of 2019, the MQTT specification version 5 was released, which includes features like better error reporting, shared subscriptions, message and session expires.
What are the benefits of using the MQTT protocol?
We’ve already mentioned a few advantages of using MQTT like cost-effectiveness and reliability. The MQTT protocol’s binary format and support for Quality of Service (QoS) levels helps to minimize message size, reduce network traffic, and optimize device performance. But what other benefits are there?
- Easy to implement and use
The MQTT protocol is user-friendly, and has an intuitive syntax that helps make it easy for new users to understand and send/process messages. On top of that, there are many libraries, tools, and other resources available for building your next IoT project and testing the MQTT protocol in action.
- Flexible and scalable
With the MQTT protocol, devices can publish and subscribe to multiple topics, as well as handle connections and messages quickly and reliably. Users can customize devices, subscriptions, and connections according to their projects’ needs. This enables users to customize their own setup of the MQTT protocol in IoT projects and handle millions of connections without compromising speed and reliability.
Now that you have a better understanding of the MQTT protocol and the advantages of using it, let’s dive deeper into the key components involved in MQTT.
Components of MQTT
While MQTT projects will vary depending on the needs, there are several common components that will exist in every project like:
- Broker: Establishes and maintains connections with clients and processes messages.
- Client: Device that publishes or receives messages from the MQTT broker.
- Topic: A way to filter messages and ensure that the right clients receive the right messages.
- Payload: The message content that is sent and delivered in MQTT message transmissions.
- Publish/subscribe: Messages are delivered from a client (publisher) to a broker, then from the broker to all clients subscribed to the topic the message was sent to (subscriber).
- Broker
The MQTT broker is at the center of all MQTT message transmissions. It is in charge of establishing and maintaining secure connections with clients and routing messages to the right clients.
If you’d like more details about the MQTT broker and how it works, you should check out this article.
- Client
Clients are devices that operate an MQTT library and establish a connection to a broker. There are two roles an MQTT client can have:
- Publisher: a client that publishes a message to a topic.
- Subscriber: a client that receives all messages from the subscribed topic.
For more information about MQTT clients, read this.
- Topic
Topics are a kind of filter that the MQTT broker uses in message transmissions. A typical MQTT message transmission looks like this:
- A publisher sends a message to the broker on a topic.
- The broker filters all connected clients for that particular topic.
- Broker forwards the message to the clients subscribed to that topic.
If you’d like a more detailed explanation of MQTT topics, read this.
- Payload
This refers to the actual message content that is being sent in MQTT message transmissions. There is no fixed format for payloads, but they are usually in JSON, XML, or text.
Read this article for more information about MQTT payloads.
- Publish/subscribe
The MQTT protocol employs a publish/subscribe model. This means that the publisher never interacts directly with a subscriber. Instead, all communications are sent to a broker, and the broker routes the messages to the respective subscribers.
If you’d like to start using the MQTT protocol in IoT projects, you should explore the Management Center (MMC) MQTT client. It’s a web application that makes it easier to manage Pro Mosquitto broker instances and clusters. It also provides access to other features like user and access control management, REST APIs, and topic free visualisation.
Key features of MQTT
With a better idea of what MQTT message transmissions typically entail, let’s dive deeper into some of the key features of using the MQTT protocol.
- Topics and Wildcards
In the MQTT protocol, wildcards are used together with topics. As mentioned earlier, topics are filters that enable the broker forward the messages from a topic to the right clients. This means that only the clients subscribed to that topic, would receive these messages.
Wildcards are special characters that let the MQTT broker know that a client wants to subscribe to topics matching the specified pattern. It is important to note that wildcards can only be used when subscribing to topics.
- Quality of Service (QoS)
In the MQTT protocol, QoS is the level of service that serves as a consensus to successfully deliver a message. There are at least two sides in every message delivery – a publisher and a broker, and the broker and a subscriber. There are three possible QoS levels: QoS 0 (at most once), QoS 1 (at least once), and QoS 2 (exactly once).
When publishers and subscribers have the same QoS, MQTT message transmissions are straightforward, but with different QoS, things can get a bit complicated. To find out more about QoS and when to use each QoS, read this.
- Last Will and Testament (LWT)
Similar to how wills are used in the real world, MQTT’s LWT is used to inform parties of abnormal behavior in MQTT connections. For example, if a client unexpectedly disconnects or goes offline, the broker publishes a last will message to a topic. This helps to ensure that subscribers are informed of the disconnection.
The message and topic are predefined in the LWT. Other parameters in LWT include lastWillRetain, and lastWillQoS.
- Persistent sessions
As the name suggests, persistent sessions enable messages and data to be stored for a longer period of time. This means that messages can be stored for multiple network connections in the MQTT protocol.
Persistent sessions help to avoid loss of messages during offline periods or sudden disconnections, reduce the need to subscribe repeatedly across different sessions, and also ensure that QoS 1 and QoS 2 messages are not affected by any network outages.
Wrap up
MQTT is typically used for machine-to-machine communications and a common use case is in IoT devices. This means devices like mobile devices, smart sensors, wearable trackers. Machine-to-machine communications happen more often than you’d think. A common example is wearing an activity tracker like FitBit and checking your activity on your phone application.
The MQTT protocol is a lightweight, cost-effective, and reliable way to monitor devices remotely. Besides that, there are other benefits of using the MQTT protocol like ease of implementation and use, as well as flexibility and scalability.
MQTT uses a publish/subscribe model and is highly customizable. MQTT projects can vary significantly, there will always be the broker, clients, topic, and payload. Depending on your needs, your MQTT project could employ one or more of these features in the MQTT protocol:
- Topics and wildcards: Topics are filters the broker uses to forward messages to the right clients, and wildcards let the MQTT broker know that clients want to subscribe to topics matching the specified pattern.
- Quality of service (QoS): A consensus of service level between an MQTT broker and a client (publishing or subscribing). There are three different QoS: QoS 0 (at most once), QoS 1 (at least once), or QoS 2 (exactly once).
- Last Will and Testament (LWT): Used to inform clients of abnormal behavior in MQTT connections like network outages and sudden disconnections.
- Persistent sessions: A way of enabling messages to be retained across multiple network connections.
Lastly, leverage the Management Center (MMC) MQTT client to easily manage Pro Mosquitto broker instances and clusters. You can also get access to other key MQTT features such as user and access control management, REST APIs, topic free visualization, and more.
Top comments (0)