The Basics of the MQTT Protocol
EMQ Technologies Inc.EMQ Technologies Inc.
We will discuss the basics and how to get started with the MQTT protocol, with code examples. Beginners of the IoT and MQTT can utilize this information to understand MQTT-related concepts and quickly start developing MQTT services and applications.
MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe-based messaging protocol designed for resource-constrained devices and low-bandwidth, high-latency, or unreliable networks. It is widely used in Internet of Things (IoT) applications, providing efficient communication between sensors, actuators, and other devices.
"MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe based messaging protocol designed for resource-constrained devices and low-bandwidth, high-latency, or unreliable networks."
MQTT has emerged as one of the best IoT protocols due to its unique features and capabilities tailored to the specific needs of IoT systems. Some of the key reasons include:
To understand how MQTT works, you need to first master the basics for each concept: MQTT Client, MQTT Broker, Publish-Subscribe mode, Topic, and QoS:
MQTT Client
Any application or device running the MQTT client library is an MQTT client. For example, an instant messaging app that uses MQTT is a client, various sensors that use MQTT to report data are a client, and various MQTT testing tools are also a client.
MQTT Broker
The MQTT Broker handles client connection, disconnection, subscription, and unsubscription requests, and routing messages. A powerful MQTT broker can support massive connections and million-level message throughput, helping IoT service providers focus on business and quickly create a reliable MQTT application.
Publish–subscribe Pattern
The publish-subscribe pattern differs from the client-server pattern in that it separates the client that sends messages (publisher) from the client that receives messages (subscriber). Publishers and subscribers do not need to establish a direct connection, and the MQTT Broker is responsible for routing and distributing all messages.
The following diagram shows the MQTT publish/subscribe process. The temperature sensor connects to the MQTT server as a client and publishes temperature data to a topic (e.g., Temperature
), and the server receives the message and forwards it to the client subscribed to the Temperature
 topic.
Topic
The MQTT protocol routes messages based on topic. The topic distinguishes the hierarchy by slash /
, which is similar to URL paths, for example:
chat/room/1sensor/10/temperature
sensor/+/temperature
MQTT topic supports the following wildcards: +
 and #
.
+
: indicates a single level of wildcards, such as a/+
 matching a/x
 or a/y
.#
: indicates multiple levels of wildcards, such as a/#
 matching a/x
, a/b/c/d
.For more details on MQTT topics, please check the blog Understanding MQTT Topics & Wildcards by Case.
Quality of Service (QoS)
MQTT provides three kinds of Quality of Service and guarantees messaging reliability in different network environments.
Now that we understand the basics of MQTT, let’s see how the general workflow works:
New Podcast Episode
Recent Articles