December 6, 2021
MQTT 101: The basics of an IoT messaging protocol
Overview of MQTT history, terminology and benefits
The Internet of Things (IoT) has only just begun, with new solutions and use cases found every day and in every industry – from manufacturing and energy to transportation and medicine.
A basic, if not the de facto, standard messaging protocol used to transport IoT data is MQTT.
MQTT is a lightweight messaging protocol for machine-to-machine (M2M) communication, and ideal for connected devices with low or unreliable network bandwidth and constrained resources. It consists of publishers sending data (e.g., machines and other connected devices) and subscribers receiving data (e.g., machines or applications).
MQTT was created in 1999 as a protocol to connect satellites and oil pipelines. Among the requirements that necessitated the protocol include simple implementation, minimal bandwidth, minimal power requirements and quality data delivery. These requirements are still prominent for today’s versions of MQTT.
Here is an overview of basic MQTT technology and a high-level synopsis of the characteristics of MQTT.
Basic MQTT terminology
Client: Any device that connects to an MQTT broker over a network. Clients can range from micro controllers up to fully-fledged servers and can both send and receive messages. Since a client can be embedded in sensors, actuators, gadgets, appliances and machines, – it can enable everything from smart homes and smart cities to smart factories and smart farms.
Broker (or server): Software that receives messages from clients published to a topic and forwards them to all clients subscribed to said topic. A broker tracks the state of clients, sessions, topics, subscriptions and messages. Brokers can be located anywhere in an edge-computing environment or in the cloud.
Bridge: The connection between two MQTT brokers.
Topic: A UTF-8 string that serves a message title (or subject line) that is used for filtering messages to clients. Messages are sent to clients that subscribe to that topic.
Subscriber: A client that connects to a broker and is registered to receive messages to a particular topic.
Publisher: A connected client that sends topic messages to a broker.
Quality of Service (QoS): Describes the reliability of message delivery with three defined QoS levels. QoS is an agreement between sender and receiver on the guarantee of delivering a message. QoS is considered to be one of the most important aspects of MQTT. There are three QoS levels:
- 0 – at most once. The message will be delivered once with no guarantee of receipt.
- 1 – at least once. The message is guaranteed to be delivered to the broker. The client will continue sending the message until it is confirmed that the broker received the message.
- 2 – exactly once. The message delivery is guaranteed, as guaranteed confirmation. The process includes a handshake — a sequence of four messages between the sender and the receiver to confirm that the main message has been sent and the acknowledgement has been received.
Persistent Sessions: The broker remembers a client’s subscriptions if it disconnects during the session. When it reconnects, the client’s subscriptions re-establish automatically, and the client receives all subscribed messages published during the time it was disconnected.
Clean Session (also non-persistent session): The broker deletes the client’s subscriptions if it disconnects during the session, and the client will not receive messages published during the time it was disconnected. The client must re-create the subscriptions the next time it connects.
Last Will and Testament (LWT): A message that notifies other clients about an ungracefully (unexpected) disconnected client, which is a client that is unexpectedly disconnected while an MQTT connection is active.
Benefits associated with MQTT:
Communications: MQTT allows bi-directional communications between the device and broker.
Scalability: MQTT allows you to connect to millions of IoT devices.
Reliability: MQTT provides three levels of reliable message delivery services.
Security: MQTT enables easy messages encryption via TLS and client authentication using modern authentication protocols.
Resource consumption: MQTT is lightweight, requires minimal bandwidth and uses little power.
Compatibility: MQTT is data agnostic.
Currently there are two versions of MQTT in use v. 3.1.1 and v5.0 and v3.1.1, which are both OASIS standards.
Additional MQTT Resources
FairCom EDGE MQTT Broker (PDF)
Browsing live sensor data with MQTT Explorer (Video)
How to collect and query IoT data with MQTT + SQL (On-demand webinar)
What’s best for IIoT: An integration hub or an MQTT broker? (Blog)