What is MQTT?
MQTT is a standards-based messaging protocol, or set of rules, used for machine-to-machine communication. Smart sensors, wearables, and other Internet of Things (IoT) devices typically have to transmit and receive data over a resource-constrained network with limited bandwidth. These IoT devices use MQTT for data transmission, as it is easy to implement and can communicate IoT data efficiently. MQTT supports messaging between devices to the cloud and the cloud to the device.
Why is the MQTT protocol important?
The MQTT protocol has become a standard for IoT data transmission because it delivers the following benefits:
Lightweight and efficient
MQTT implementation on the IoT device requires minimal resources, so it can even be used on small microcontrollers. For example, a minimal MQTT control message can be as little as two data bytes. MQTT message headers are also small so that you can optimize network bandwidth.
MQTT implementation requires a minimal amount of code that consumes very little power in operations. The protocol also has built-in features to support communication with a large number of IoT devices. Hence, you can implement the MQTT protocol to connect with millions of these devices.
Many IoT devices connect over unreliable cellular networks with low bandwidth and high latency. MQTT has built-in features that reduce the time the IoT device takes to reconnect with the cloud. It also defines three different quality-of-service levels to ensure reliability for IoT use cases— at most once (0), at least once (1), and exactly once (2).
MQTT makes it easy for developers to encrypt messages and authenticate devices and users using modern authentication protocols, such as OAuth, TLS1.3, Customer Managed Certificates, and more.
Several languages like Python have extensive support for MQTT protocol implementation. Hence, developers can quickly implement it with minimal coding in any type of application.
What is the history behind MQTT protocol?
The MQTT protocol was invented in 1999 for use in the oil and gas industry. Engineers needed a protocol for minimal bandwidth and minimal battery loss to monitor oil pipelines via satellite. Initially, the protocol was known as Message Queuing Telemetry Transport due to the IBM product MQ Series that first supported its initial phase. In 2010, IBM released MQTT 3.1 as a free and open protocol for anyone to implement, which was then submitted, in 2013, to Organization for the Advancement of Structured Information Standards (OASIS) specification body for maintenance. In 2019, an upgraded MQTT version 5 was released by OASIS. Now MQTT is no longer an acronym but is considered to be the official name of the protocol.
What is the principle behind MQTT?
The MQTT protocol works on the principles of the publish/subscribe model. In traditional network communication, clients and servers communicate with each other directly. The clients request resources or data from the server, then the server processes and sends back a response. However, MQTT uses a publish/subscribe pattern to decouple the message sender (publisher) from the message receiver (subscriber). Instead, a third component, called a message broker, handles the communication between publishers and subscribers. The broker’s job is to filter all incoming messages from publishers and distribute them correctly to subscribers. The broker decouples the publishers and subscribers as below:
The publisher and subscriber are not aware of each other’s network location and do not exchange information such as IP addresses or port numbers.
The publisher and subscriber don’t run or have network connectivity at the same time.
Both publishers and subscribers can send or receive messages without interrupting each other. For example, the subscriber does not have to wait for the publisher to send a message.
What are MQTT components?
MQTT implements the publish/subscribe model by defining clients and brokers as below.
An MQTT client is any device from a server to a microcontroller that runs an MQTT library. If the client is sending messages, it acts as a publisher, and if it is receiving messages, it acts as a receiver. Basically, any device that communicates using MQTT over a network can be called an MQTT client device.
The MQTT broker is the backend system which coordinates messages between the different clients. Responsibilities of the broker include receiving and filtering messages, identifying clients subscribed to each message, and sending them the messages. It is also responsible for other tasks such as:
- Authorizing and authenticating MQTT clients
- Passing messages to other systems for further analysis
- Handling missed messages and client sessions
Clients and brokers begin communicating by using an MQTT connection. Clients initiate the connection by sending a CONNECT message to the MQTT broker. The broker confirms that a connection has been established by responding with a CONNACK message. Both the MQTT client and the broker require a TCP/IP stack to communicate. Clients never connect with each other, only with the broker.
How does MQTT work?
An overview of how MQTT works is given below.
- An MQTT client establishes a connection with the MQTT broker.
- Once connected, the client can either publish messages, subscribe to specific messages, or do both.
- When the MQTT broker receives a message, it forwards it to subscribers who are interested.
Let’s break down the details for further understanding.
The term ‘topic’ refers to keywords the MQTT broker uses to filter messages for the MQTT clients. Topics are organized hierarchically, similar to a file or folder directory. For example, consider a smart home system operating in a multilevel house that has different smart devices on each floor. In that case, the MQTT broker may organize topics as:
MQTT clients publish messages that contain the topic and data in byte format. The client determines the data format such as text data, binary data, XML, or JSON files. For example, a lamp in the smart home system may publish a message on for the topic livingroom/light.
MQTT clients send a SUBSCRIBE message to the MQTT broker, to receive messages on topics of interest. This message contains a unique identifier and a list of subscriptions. For example, the smart home app on your phone wants to display how many lights are on in your house. It will subscribe to the topic light and increase the counter for all on messages.
What is MQTT over WSS?
Is MQTT secure?
MQTT communication uses SSL protocol to protect sensitive data transmitted by IoT devices. You can implement identity, authentication, and authorization between clients and the broker using SSL certificates and/or passwords. The MQTT broker typically authenticates clients using their passwords as well as unique client identifiers it allocates to each client. In most implementations, the client authenticates the server with certificates or DNS lookups. You can also implement encryption protocols with MQTT.
Is MQTT RESTful?
MQTT is not RESTful. Representational state transfer (REST) is an architectural approach to network communication that uses the request-response pattern of communication between message senders and receivers. In contrast, MQTT uses the publish/subscribe model of communication in the application layer and requires a standing TCP connection to transmit messages in a push manner. However, MQTT version 5 adds a new request/response method to act in a way similar to REST, wherein the publisher can attach a special response topic, which the receiver processes and generates an appropriate response.
How can AWS support your MQTT implementation?
AWS IoT Core is a fully managed service that lets you connect billions of IoT devices and route trillions of messages to AWS services. You can:
- Connect, manage, and scale your device fleets easily and reliably without provisioning or managing servers.
- Choose your preferred communication protocol, including MQTT, HTTPS, MQTT over WSS, and LoRaWAN.
- Secure device connections and data with mutual authentication and end-to-end encryption.
- Filter, transform, and act upon device data on the fly, based on your defined business rules.
Get started with MQTT on AWS by creating a free account today!