Mqtt development notes: Mqtt server setup

Posted Jun 4, 20209 min read

If the article is an original article, it may not be reprinted without permission
Original blogger blog address:
Original blogger blog navigation:
This article blog address:
Dear readers, knowledge is endless and manpower is poor, either change the demand, or find a professional, or research by yourself
Red Fat Man(Red Imitation) blog post Daquan:Development technology collection(including Qt practical technology, Raspberry Pi, 3D, OpenCV, OpenGL, ffmpeg, OSG, microcontroller, soft and hard combination, etc.) Continuous update...(click on the portal)

Qt development column:development technology(click on the portal)
================================================== ================================================== ================================================== ==============

" Qt Development Technology:Introduction to mqtt, QtMqtt Compilation and Development Environment Construction "
" Mqtt Development Notes:Mqtt Server Construction "


Business needs, need to use mqtt protocol(middleware).

MQTT protocol


MQTT(Message Queuing Telemetry Transport, Message Queuing Telemetry Transport Protocol) is a "lightweight" communication protocol based on the publish/subscribe(publish/subscribe) mode. The protocol is built on the TCP/IP protocol and was developed by IBM in 1999 Yearly release. The biggest advantage of MQTT is that it can provide real-time and reliable message service for connecting remote devices with very little code and limited bandwidth. As a low-cost, low-bandwidth instant messaging protocol, it has a wide range of applications in the Internet of Things, small devices, and mobile applications.

Design Principles

  • Streamlined, without adding optional functions;
  • Pub/Sub(Pub/Sub) mode to facilitate the transfer of messages between sensors;
  • Allow users to create topics dynamically with zero operation and maintenance costs;
  • Minimize transmission volume to improve transmission efficiency;
  • Taking into account factors such as low bandwidth, high latency, and unstable network;
  • Support continuous session control;
  • Understand that the computing power of the client may be very low;
  • Provide service quality management;
  • Assuming that the data is unknown, do not force the type and format of the transmitted data to maintain flexibility.


  1. Use the publish/subscribe message model to provide one-to-many message publishing and decouple application

    This is very similar to XMPP, but the information redundancy of MQTT is much smaller than XMPP, because XMPP uses XML format text to transfer data.

  2. Message transmission shielding the load content

  3. Use TCP/IP to provide network connection

    The mainstream MQTT is based on TCP connection for data push, but there is also a UDP-based version called MQTT-SN. Since these two versions are based on different connection methods, the advantages and disadvantages are naturally different.

  4. There are three types of message publishing service quality

"At most once", the message release completely depends on the underlying TCP/IP network. Message loss or duplication will occur. This level can be used in the following situations. It doesn't matter if the environmental sensor data is lost once the reading record is lost, because there will be a second transmission in the near future. This method is mainly for the push of ordinary APPs. If your smart device is not connected to the Internet when the message is pushed, the push has not been received in the past, and you will not receive it when you connect to the Internet again.
"At least once", to ensure that the message arrives, but message duplication may occur.
"Only once", make sure the message arrives once. This level can be used in some demanding billing systems. In a billing system, duplicate or missing messages can lead to incorrect results. This highest quality message publishing service can also be used to push instant messaging apps to ensure that users receive and will only receive it once.

  1. Small transmission with low overhead

    (The fixed-length header is 2 bytes), protocol exchange is minimized to reduce network traffic. It is very suitable for "in the field of Internet of Things, communication between sensors and servers, information collection", the computing power and bandwidth of embedded devices are relatively weak, and the use of this protocol to pass messages is no better.

  2. The mechanism of abnormal client interruption.

Last Will:The last will mechanism, used to notify other devices under the same subject that the device sending the last word has been disconnected.
Testament:Testament mechanism, function similar to Last Will.

Publish/subscriber model

MQTT is a client-server based message publish/subscribe transfer protocol. MQTT protocol is lightweight, simple, open and easy to implement, these characteristics make it very applicable. In many cases, including restricted environments, such as:machine-to-machine(M2M) communication and Internet of Things(IoT). It has been widely used in communication sensors via satellite links, occasionally dialed medical devices, smart homes, and some miniaturized devices.
insert picture description here
From the picture, MQTT has three roles:

  • Broker agent:Many people understand middleware, of course, think of it this way. He is a middleware. Used to process information and send it to the corresponding subscribers.
  • Publisher:Used to publish information to agents. Note:The publisher can also be a subscriber.
  • Subscriber:the client used to receive information.

MQTT server

MQTT server is called "Broker" and it can be an application or a device. It is located between the publisher and subscriber of the message. It can:

  • Accept network connection from customers;
  • Accept application information released by customers;
  • Handle subscription and unsubscription requests from clients;
  • Forward application messages to subscribed customers.

Method in MQTT protocol

MQTT protocol defines some methods(also known as actions), which are used to express the operations performed on certain resources. This resource can represent pre-existing data or dynamically generated data, depending on the server implementation. In general, resources refer to files or output on the server. The main methods are:

  • Connect:Waiting to establish a connection with the server
  • Disconnect:Wait for the MQTT client to complete the work and disconnect the TCP/IP session from the server
  • Subscribe:Waiting to complete the subscription
  • UnSubscribe:Wait for the server to cancel the client's subscription and topics subscription
  • Publish:MQTT client sends a message request and returns to the application thread after sending

Apache Apoll environment setup on Windows(mqtt)

Download Apache Apoll

Official download address:
CSDN download address:

Install jdk

Official download address:
CSDN download address:
Download the corresponding version of the system, windows x64
Insert picture description here
Add java path to system Path variable

C:\Program Files\Java\jdk-12.0.1

PATH(added at the end)


Bold style CLASSPATH


Use cmd to run java naming, the jdk environment configuration is successful, as shown below:
Insert picture description here

Install Apache Apoll


Unzip Apache Apoll to the C drive(custom), as shown below:
insert picture description here

Create an instance

Use cmd to enter the folder to create an instance:

cd C:\apache-activemq-5.15.9
cd bin
activemq-admin.bat create mybroker

insert picture description here
Insert picture description here

View login username and password

Enter mybroker/conf, check, you can see the user name
insert picture description here
insert picture description here
View tcp listening port(can be modified by myself, the author does not modify it)

Insert picture description here
View the web management page port(can be modified by myself, the author does not modify it)
insert picture description here
insert picture description here

Run apache apoll

cd C:\apache-activemq-5.15.9\bin\mybroker\bin
mybroker.bat start

insert picture description here
insert picture description here
Insert picture description here

Test installation

Step 1:Open ie(browser)

Step 2:Enter the URL http://localhost :8161/admin

insert picture description here

Step 3:Enter admin, admin

insert picture description here

Step 4:Create a queue test

Insert picture description here
insert picture description here
insert picture description here
insert picture description here

Step 5(Supplement):Remote management

insert picture description here
So far, the apache apoll middleware environment and services on the windows server2008 r2 system have been successfully built.

Common error handling

Qt returned error code 257 when connecting

The client encountered a protocol violation, so the connection was closed.
Check server error
Insert picture description here
the reason
ActiveMQ sometimes reports an error similar to Frame size of 257 MB larger than max allowed 100 MB, meaning that a single message exceeds the preset maximum value, in the configuration file

 <transportConnector name="openwire" uri="tcp://

We can configure this value, but sometimes a large single message suddenly appears, such as 1G.
QtMqtt is connected to the service, and the maximum value of the attribute passed may be 258MB, so modify the server configuration directly.
Restart the service

Original blogger blog address:
Original blogger blog navigation:
This article blog address: