Everything About MQTT Protocol — Message Queue Telemetry Transport
Building a chat app isn’t easy.
One of the most important technical decisions you have to make is choosing which messaging protocol you’ll implement. One of the protocols you can choose is MQTT. What is MQTT protocol? Well, that's what we're here to tell you.
In this article, we'll discuss what the MQTT protocol is, the communication protocol's history and background, how it works, the beneficial features, and when you should use it.
Go pour yourself a coffee and let’s dive in:
What is MQTT? A Quick Overview
MQTT (MQ Telemetry Transport) is a lightweight messaging protocol designed for high-latency, low-bandwidth, and unreliable networks. It consumes very little power on the device it’s running on. Due to its low overhead, MQTT is most often used in an embedded environment for machine-to-machine communication. Its use is prevalent in industries such as automotive, manufacturing, telecommunications, and smart homes. In fact, it has become the de-facto open-source protocol for connecting Internet of Things (IoT) devices.
History & Background of MQTT
MQTT protocol was created in 1999 by Andy Stanford-Clark and Arlen Nipper for IBM. Their original goal was to create a messaging protocol that could be used to monitor oil pipelines and send telemetry data back and forth. It had to be extremely bandwidth-efficient and reliable because the devices they were using were battery-operated and connected via satellite.
MQTT’s naming has an interesting history. It was originally named “MQ Telemetry Transport” after the IBM MQ (Message Queueing) line of products. However, MQTT uses a publish/subscribe messaging architecture and doesn’t use queues at all. So the often-used name, “Message Queuing Telemetry Transport”, is a misnomer. When IBM submitted the protocol to OASIS, it’s formal name was MQ Telemetry Protocol (MQTP). But in the documents themselves, the MQTT abbreviation was most often used and is now what it is known as.
How MQTT Chat Works
As we just mentioned, MQTT protocol uses a publish/subscribe pattern. Here's what that means:
The MQTT chat architecture consists of MQTT clients and MQTT brokers. Multiple clients can connect to the same broker. A client can either publish or subscribe to a “message topic”. When a client publishes a message to a broker it contains a message topic. When the broker receives these messages, it uses the message topic to determine which other clients are subscribed to that specific message topic and routes the message to those clients.
Features of MQTT Protocol
There are a few key features that MQTT instant messaging protocol provides which makes it appealing for use in chat apps.
Quality of Service Levels
A Quality of Service (QoS) level is simply an agreement between the sender and receiver of a message that guarantees the deliverability of said message. The more QoS levels, the more reliable the protocol is.
MQTT offers three levels of QoS:
At most once (0): The lowest QoS level that MQTT offers, here the recipient does not acknowledge receiving the message and the message is not stored and resent by the sender. There is no guarantee of delivery.
At least once (1): Level 1, the sender stores the message until it receives a ping from the receiver acknowledging delivery. Level 1 guarantees that the message is delivered at least once. But, on level 1 it is possible that a message gets sent and delivered multiple times.
Exactly once (2): The highest QoS that MQTT offers, this is the safest QoS level. It is also the slowest QoS level. It guarantees that the recipient will receive the message just once. This is achieved with a four-part handshake between sender and recipient.
This is one of the key features that make the MQTT protocol an efficient protocol for unreliable networks. It allows for persistent sessions between the client and broker. Unlike many other protocols, if the connection is broken for any reason, the broker retains the information required to reconnect the client and does so automatically.
When an MQTT client subscribes to a new “message topic”, it usually has zero insight into when to expect the first message. A retained message solves that issue. A broker can store the “last good message” on that topic as a retained message—which is then automatically sent to a client when it creates a new subscription. This allows a client to immediately know the status of a topic without having to wait.
This feature allows an MQTT client to define a “last will” message. A "last will" message is automatically sent to the MQTT broker if the client ungracefully disconnects. This allows the broker to easily and efficiently notify the other clients when one disconnects.
Should you use the MQTT Communication Protocol for Your Chat App?
To determine whether you should use the MQTT protocol for your chat app over something like WebSockets or XMPP, you’ll want to take a look at your project’s requirements.
Top reasons companies choose MQTT:
Lightweight and efficient, requiring minimal resources to run
Supports bi-directional messaging
Can easily scale to millions of connected devices
Allows for security through TLS and other common authentication protocols
Offers 3 QoS levels to support reliable messaging
Works well over unreliable networks
So, if you need your chat app to consume very little device battery power or work on low-bandwidth or unreliable networks—the MQTT instant messaging protocol is the one for you. However, if neither of those is a priority for you, you’re better off using a different messaging protocol that's specifically designed for chat apps, such as WebSocket or XMPP.
Want to learn more about WebSocket or XMPP? Check out these guides:
If you do decide to use MQTT for your chat app, you’ll be in good company.
Facebook Messenger uses MQTT for its mobile chat functionality. Their decision to use MQTT was heavily influenced by the lower bandwidth usage and longer device battery life that MQTT provides.
Instagram’s direct messaging functionality, their only functionality requiring real-time communication, also uses MQTT.
Outside of chat and social apps, MQTT is used extensively in the IoT world. Amazon Web Services, IBM Watson, and Microsoft Azure (the three most popular platforms for IoT services) all use MQTT as their main communication protocol.
Conclusion & Additional Resources
With the knowledge from this article, we hope you have a better understanding of what MQTT protocol is, how it works and what it’s best suited for.
Building a chat app is a huge undertaking. CometChat is here to provide you with all the resources required to make the tough decisions. The easiest decision to make is to use our Chat APIs and SDKs to make the development of your chat app a breeze.
Cosette Cressler is a passionate content marketer specializing in SaaS, technology, careers, productivity, entrepreneurship and self-development. She helps grow businesses of all sizes by creating consistent, digestible content that captures attention and drives action.