What Is MQTT? Principles and Workflow

1. What Is MQTT

MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe based message transport protocol that is suitable for resource-constrained devices and low-bandwidth, high-latency, or unstable network environments. It is widely used in IoT applications, enabling efficient communication between sensors, actuators, and other devices.

2. How MQTT Works

To understand how MQTT works, you first need to grasp the following concepts: MQTT Client, MQTT Broker, publish-subscribe model, topics, and QoS.

1. MQTT Client

An MQTT client is a device or software that can communicate using the MQTT protocol. It can publish messages or receive messages. For example, your smart home device or an application on your phone can be an MQTT client.

2. MQTT Broker (MQTT Server)

The MQTT Broker is like a “post office.” MQTT clients send messages to it, and it forwards them to clients that have subscribed to the relevant content. This way, clients do not need to “talk” directly to each other.

3. Publish-Subscribe Model

This is a method of message transmission. An MQTT client can choose to publish messages or subscribe to a category of messages. For example, if you publish a message about the lighting in your home, all clients that have subscribed to the “lighting” category will receive that message.

What Is MQTT? Principles and Workflow

4. Topics

Topics are like classification tags for messages. When you publish a message, you must choose a topic; when you subscribe to messages, you also need to choose the topic you want. For example, if you have many appliances in your home, you can choose “air conditioning” as a topic to publish or subscribe to messages about air conditioning.

What Is MQTT? Principles and Workflow

What Is MQTT? Principles and Workflow

5. QoS (Quality of Service)

QoS is a parameter used in the MQTT protocol to control the reliability of message delivery. The MQTT protocol defines three different QoS levels:

  • QoS 0: At most once. Message delivery completely relies on the underlying TCP/IP network. If the client is unavailable at that time, the publication fails. Messages may be lost.

  • QoS 1: At least once. Ensures that messages arrive, but duplicates may occur.

  • QoS 2: Exactly once. This is the highest level of transmission quality, where message loss and duplication are unacceptable.

You can choose the “quality” level of message delivery. For instance, you can choose “ensure the message definitely arrives” or “message loss is acceptable.” Different choices will affect the reliability and speed of message delivery.

3. MQTT Workflow

The workflow of MQTT is actually as simple as sending and receiving a package.

Sending a Package (Publishing a Message): If you want to send a package to a friend, you prepare the package, write the recipient’s address and contact information, and then take it to the courier company (MQTT server) to send. Here, “you” are the MQTT client, the courier company is the MQTT server (Broker), and the contents of the package are the message you want to publish, while the address and contact information represent the message topic.

Delivering the Package (Message Forwarding): Once the courier company receives your package, it will deliver it to your friend based on the address and contact information you provided. Just like the MQTT server forwards messages to clients that have subscribed to that topic.

Receiving the Package (Receiving Messages): Your friend waits at home for the package, and when the courier delivers it, they can sign for it and check the contents. Here, the friend is another MQTT client that subscribed to the address (topic) you used when sending the package, so they can receive the message you published. At the same time, they can also send out packages again, which means publishing messages and becoming a new sender.

Throughout the process, you can choose different courier services (QoS levels), such as standard delivery or express delivery, to ensure the package is delivered safely and on time. This is similar to the QoS levels in MQTT, which ensure the reliability and speed of message delivery.

What Is MQTT? Principles and Workflow

Leave a Comment