Easy Tutorial
❮ Android Tutorial Powermanager Android Tutorial Patheffect ❯

MQTT Introduction for Beginners

Category Programming Technology

I. Brief Overview

MQTT (Message Queuing Telemetry Transport) is a "lightweight" messaging protocol based on the publish/subscribe model, built on top of the TCP/IP protocol suite and released by IBM in 1999. The greatest advantage of MQTT is its ability to provide real-time and reliable messaging services for remote devices with minimal code and limited bandwidth. As a low-overhead, low-bandwidth messaging protocol, it has found broad application in the Internet of Things (IoT), small devices, and mobile applications.

MQTT is a client-server based messaging protocol for publish/subscribe transmission. The protocol is lightweight, simple, open, and easy to implement, making it widely applicable. It is particularly suited for constrained environments, such as Machine-to-Machine (M2M) communication and the Internet of Things (IoT), and has been extensively used in scenarios like satellite-linked sensors, dial-up medical devices, smart homes, and small devices.


II. Design Principles

Due to the unique nature of the IoT environment, MQTT adheres to the following design principles:


III. Main Features

Designed for communication between remote sensors and control devices over low-bandwidth, unreliable networks, MQTT has the following main features:

-

(1) It uses the publish/subscribe messaging model, providing one-to-many message publishing and decoupling applications.

This is similar to XMPP, but MQTT has much less message redundancy than XMPP because it uses XML format text for data transmission.

-

(2) Message transmission that shields the payload content.

-

(3) Utilizes TCP/IP to provide network connections.

The mainstream MQTT is based on TCP connections for data pushing, but there is also a version based on UDP, called MQTT-SN. These two versions have their own advantages and disadvantages due to their different connection methods.

-

(4) There are three levels of message publishing quality of service:

"At most once," where message publishing fully depends on the underlying TCP/IP network. Message loss or duplication may occur. This level can be used in situations such as environmental sensor data, where losing a single reading is not critical because another will be sent soon. This method is mainly used for push notifications in ordinary apps; if your smart device is offline when the message is pushed and misses it, it will not receive it again when it reconnects.

"At least once," ensures message delivery, but message duplication may occur.

"Exactly once," ensures that the message is delivered once. This level can be used in strict billing systems where message duplication or loss can lead to incorrect results. This highest quality of message publishing service can also be used for push notifications in instant messaging apps, ensuring that users receive and only receive the message once.

-

(5) Small transmission with minimal overhead (fixed-length header is 2 bytes), protocol exchanges are minimized to reduce network traffic.

This is why it is said to be very suitable for "communication between sensors and servers in the IoT field, collection of information." Embedded devices have relatively weak computing power and bandwidth, making this protocol very suitable for message transmission.

-

(6) The Last Will and Testament feature notifies relevant parties of the client's abnormal disconnection mechanism.

Last Will: The will mechanism, used to notify other devices on the same topic that the device sending the will has disconnected.

Testament: The testament mechanism, which functions similarly to the Last Will.


IV. MQTT Protocol Principles

4.1 MQTT Protocol Implementation

Implementing the MQTT protocol requires communication between the client and server. During communication, there are three roles in the MQTT protocol: Publisher, Broker (server), and Subscriber. The message publisher and subscriber are clients, while the message broker is the server. A message publisher can also be a subscriber.

The messages transmitted by MQTT are divided into two parts: Topic and payload:

❮ Android Tutorial Powermanager Android Tutorial Patheffect ❯