MQTT (Message Queuing Telemetry Transport) is a “lightweight” communication protocol based on the publish/subscribe model. This protocol is built on top of the TCP/IP protocol and was released by IBM in 1999. The biggest advantage of MQTT is that it can provide real-time and reliable messaging services for connecting remote devices with very little code and limited bandwidth.
Recent movements by internet giants have listed MQTT as a standard cloud service for the Internet of Things (IoT) access suite. Services such as Alibaba Cloud IoT Suite, Baido Open Cloud IoT Service, Tencent QQ IoT Platform, and Amazon IoT Service…
MQTT has gradually become a standard configuration for IoT platforms and is trending towards becoming the most important protocol in the IoT field.
MQTT communication is achieved through a publish/subscribe method where the message publisher and subscriber are decoupled; they are not directly connected but require an intermediary. In MQTT, this is referred to as the Broker, which is responsible for message storage and forwarding.
A typical MQTT message communication process is as follows:
The publisher sends a message to the Broker —> After the Broker receives the message, it checks which subscribers have subscribed to this type of message and then sends the message to those subscribers —> The subscriber retrieves the message from the Broker.
Here, the message publisher and subscriber are both clients, while the message broker is the server. The message publisher can also be a subscriber at the same time.
The messages transmitted by MQTT are divided into two parts: Topic and payload:
-
Topic can be understood as the type of message. After the subscriber subscribes, it will receive the message content (payload) of that topic;
-
Payload can be understood as the content of the message, referring to the specific content that the subscriber needs to use.
MQTT Client
A program or device using the MQTT protocol always establishes a network connection to the server. The client can:
(1) Publish information that other clients may subscribe to;
(2) Subscribe to messages published by other clients;
(3) Unsubscribe or delete application messages;
(4) Disconnect from the server.
MQTT Server
The MQTT server, also known as the message broker, can be an application or a device. It is located between the message publisher and subscriber and can:
(1) Accept network connections from clients;
(2) Accept application information published by clients;
(3) Process subscription and unsubscription requests from clients;
(4) Forward application messages to subscribed clients.
Next, we will demonstrate the implementation of MQTT message subscription and publishing using MQTT client and server tools:
Server Preparation
Currently, common MQTT server tools for the Windows platform include EMQ, Mosquitto, and Apollo. This example uses EMQ, download link:
http://www.emqtt.com/downloads
After downloading and extracting, switch to the bin directory in CMD
Input the command emqttd install to register the server
Start the server
Enter in the browser: http://localhost:18083
Account: admin Password: public
The login interface is as follows:
Client Preparation:
This example uses the mqttfx tool, download link: http://www.jensd.de/apps/mqttfx/1.7.1/
After installation, you can directly open the client software
Click the gear icon to configure the server
Select the corresponding connection name and then click Connect
After establishing the connection, you can see one connection in the EMQ server console
The client subscribes to a message with the topic named topic_1
The server will receive this subscription
If other clients publish a message with the same topic named topic_1, the previous client will successfully receive the message, as shown below
The above publishing topics and receiving subscription messages all rely on the MQTT server to complete the “forwarding” function. All devices under this server only need to publish or subscribe.