Author: Free and Open Lake AI
What Is MQTT Used For?
MQTT is a lightweight messaging protocol typically used for IoT devices that are designed to be energy-efficient and consume minimal bandwidth.
The MQTT protocol runs over TCP/IP and is based on a lossless two-way publish-subscribe model, ensuring that MQTT messages are transmitted efficiently and without loss.
MQTT is a low-overhead protocol that strongly considers bandwidth and CPU constraints. Its design allows it to operate in embedded environments, providing a reliable and effective communication pathway.
Essentially, MQTT is a publish/subscribe (pub/sub) protocol. It allows clients to connect to specific topics as publishers, subscribers, or both. You connect to a broker that handles all messaging, with each client identified by a unique client ID.
Examples and Use Cases of MQTT in Life
MQTT is commonly used in IoT applications to connect and communicate with low-power devices such as sensors, actuators, and home appliances, effectively managing the payload of transmitted data.
In agriculture, MQTT is used to monitor environmental factors such as soil moisture, temperature, and humidity, enabling farmers to take appropriate actions. In industrial automation, MQTT can monitor end-to-end manufacturing processes, ensuring consistent quality and identifying systemic issues in the production chain. In transportation, MQTT is used to monitor traffic lights, parking meters, and the real-time location of public transport while considering ecosystem and low bandwidth requirements.
Why Use MQTT Instead of HTTP?
HTTP and MQTT are two distinct communication protocols used for different purposes.
HTTP is a request/response protocol that transmits data only in response to client requests. It is designed for large amounts of data, typically used for transmitting web pages, images, and videos. MQTT is a publish/subscribe protocol that supports asynchronous messaging, allowing multiple clients to subscribe to a single topic and receive messages from one or more publishers. MQTT is optimized for small amounts of data but is more efficient than HTTP in terms of network overhead and device power consumption, making it a suitable choice for applications requiring low bandwidth and minimal power consumption (e.g., applications managed by AWS IoT services).
What Language Is MQTT Written In?
MQTT is just a protocol that can be implemented in various languages. However, MQTT client libraries supporting communication using the MQTT protocol are available in multiple languages, such as C/C++, Java, Python, and JavaScript, including Node.js, Ruby, Go, PHP, and Swift. MQTT client libraries communicate through MQTT brokers, which also have versions in various programming languages.
How Does MQTT Work?
MQTT is a very flexible protocol but has only two basic entities: MQTT clients and MQTT brokers.
How Do MQTT Clients Work?
An MQTT client can be any endpoint that implements the MQTT protocol. In the context of IoT, clients are connected devices, such as sensors, displays, or Arduino boards, but MQTT is not limited to IoT; clients can also be smartphones or laptops.
MQTT clients do not communicate directly with each other. Nonetheless, all interactions are mediated by a server component called an MQTT broker, which sits between clients and handles message routing.
What Is an MQTT Topic?
All communication in MQTT is divided into “topics.” MQTT clients can publish messages to topics or subscribe to receive messages from others. Topics can be any string, intended to group messages of common interest, such as sensor updates being published to a topic or messages in a group chat, depending on the use case.
What Is an MQTT Broker?
The broker is responsible for managing which clients are subscribed to which topics, receiving messages published on specific topics, and sending those messages to any clients subscribed for updates. The broker is also responsible for caching messages when the connection between the client and broker is lost and delivering them to the client when the connection is re-established.
What MQTT Brokers Are Available?
MQTT brokers are the foundation of the MQTT protocol and can be viewed as MQTT servers. As mentioned earlier, brokers sit between all clients and facilitate communication.
There are many MQTT brokers available, both open-source and proprietary. One of the most critical architectural decisions in any MQTT-based deployment is how to host the broker. Since MQTT is designed to operate at a very large scale, any broker architecture needs to scale accordingly. Most enterprise deployments will rely on cloud-hosted proprietary products, so they have no infrastructure issues, although self-hosted options are also available.
Between all communications, the broker can retain messages and maintain a secure record of all sessions; clients are isolated from each other, so insecurity and vulnerabilities of one client can be sandboxed, and depending on your network topology, the broker can reduce traffic across the entire network and allow for more efficient routing.
How to Use MQTT and Pub/Sub
Any client that supports the MQTT protocol can subscribe to and publish topics, but all communications occur through the broker, as shown in the example model below.
Features of the MQTT Protocol
Although MQTT is often considered a solution for IoT, it is merely the protocol supporting many well-known IoT products. Many features of MQTT make it particularly suitable for efficient communication between devices:
Easy to Start and Run
MQTT has been around for a long time, and there are many robust, reliable, and scalable proprietary and open-source solutions. These pre-existing MQTT components can be used for all projects, from large solutions to passion projects, without modification, regardless of developer experience.
Reliability and Configuration
MQTT has the concept of Quality of Service (QoS), which will be discussed in detail later. It queues messages and caches them on the MQTT broker, delivering them to clients when the connection is re-established. This is especially useful for devices with partial connectivity or clients with intermittent connections, such as IoT devices.
Clients Don’t Have to Consider Solution Architecture
With MQTT, you can publish messages to topics and receive them when available. As a client, you don’t have to worry about establishing or re-establishing connections, nor do you have to worry about whether the recipient is listening for your message; it “just works.”
Designed for Scale
MQTT can handle any scale your solution requires, from startups to global companies. Notably, Facebook Messenger uses MQTT for communication.
MQTT Session Lifecycle
MQTT relies on TCP/IP for connections, following a similar lifecycle.
Connection
The MQTT client initiates a connection with the MQTT broker. This is typically done through standard MQTT ports (1883/883 for secure and insecure connections, respectively).
Authentication
Aside from the TLS for the underlying TCP/IP connection, the MQTT protocol has no authentication concerns, which is sufficient for most use cases.
Publish/Subscribe
Once connected, clients can publish or subscribe to topics to send and receive messages.
Disconnect
Disconnection can be initiated by either the client or the broker and will result in the MQTT session ending after processing any ongoing requests.
MQTT Quality of Service (QoS)
Quality of Service allows solution designers to specify the reliability of MQTT connections. Generally, the more reliable the connection, the greater the potential memory overhead for retries and message retention.
QoS 0
Messages are sent only once, and neither the client nor the broker takes additional steps to confirm delivery. This is also known as “fire and forget.”
This applies only to messages sent by the client. Once received by the broker, they are considered QoS 0.
QoS 1
The sender and receiver handshake to ensure that only one copy of the message is received. This ensures delivery and avoids multiple copies of the message being sent.
QoS 2
Messages will be resent multiple times until the recipient confirms receipt. This is best suited for time-sensitive messages but may result in duplicate messages being received.
MQTT Use Cases and IoT
MQTT is a lightweight and energy-efficient protocol that has been used for solutions where these factors are key considerations for many years.
The Internet of Things is one of the most popular examples where MQTT is very well suited. IoT devices are often battery-powered, and maintenance costs can be very high, so it is best for them to last a long time between battery changes. While it is not difficult to change batteries occasionally in a home environment, in an industrial setting with thousands of sensors spread over vast areas and often in very hard-to-reach locations, maintenance can quickly become expensive.
MQTT IoT use cases include fire detectors, theft tracking, location monitoring, sensors, engine status, and more. MQTT also has very low data overhead, making it an ideal choice in environments where data costs are high or where thousands of devices are deployed.
PubNub and MQTT can work well together in IoT. See our IoT demos and IoT tutorials for more details on how PubNub and MQTT provide reliable, scalable, and efficient IoT solutions.
MQTT is also very popular in real-time communications, with Facebook Messenger being the most notable example.
Why Did Facebook Choose to Use MQTT?
Because it does not drain the phone’s battery, does not use excessive data, runs on secure protocols, allows for scaling, facilitates group chats easily, and distributes all data through a central MQTT broker backbone infrastructure.