Detailed explanation of mqtt client and server construction and basic usage

Introduction: Mainly introduces the use of mqtt client testing tools, server construction steps, technology selection, etc. The client testing tool uses MQTTX. The server uses emqx middleware for deployment and publishing. And how to configure the emqx server. And how MQTTX establishes connections and communicates with the server.

Download MQTTX client

Download address: http//mqttx.app/
As shown below:
Insert image description here

Server-side emqx construction: deployment in linux environment

Description: Operating system centos7

Installation method: rpm installation

cpu architecture: amd64

To download emqx, execute the following command:
wget https://www.emqx.com/zh/downloads/broker/4.4.1/emqx-4.4.1-otp24.1.5-3-el7-amd64.rpm

To install, execute the following command:
sudo yum install emqx-4.4.1-otp24.1.5-3-el7-amd64.rpm

To run and start, execute the following commands:
sudo emqx start

To check the startup status, execute the following command:
emqx_ctl status

If the following results appear, the startup is considered successful, as shown in the following command list

$ emqx start
emqx v4.0.0 is started successfully!
$ emqx_ctl status
Node ‘[email protected]’ is started emqx 4.0.0 is running

Insert image description here
This installation method may report dependency errors such as:

Error: Please make sure openssl-1.1.1 (libcrypto) and libncurses are installed.Also ensure it's running on the correct platform,this EMQ X release is built for 23.2.7.2-emqx-3-x86_64-unknown-linux-gnu- 64-centos7
solution: This is because dependency packages are missing: openssl1.1.1 is missing

Install and compile: openssl
cd /usr/local/src/
download wget --no-check-certificate https://www.openssl.org/source/openssl-1.1.1d.tar.gz
tar xf openssl-1.1.1d .tar.gz
compile
cd openssl-1.1.1d
./config
make && make install
Path after installation is completed: /usr/local/bin

After the installation is complete, you can start emqx normally

Things to note

The reason why the client fails to connect is that the firewall is not turned off. This problem: the host windows can ping but cannot access the tomcat service in the virtual machine Linux. This problem is generally caused by the firewall filtering the port. )

To modify the Linux system firewall configuration, you need to modify the /etc/sysconfig/iptables file. If you want to open which port, add a line in it.

-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT

That’s it, 8080 is the port number to be opened, and then service iptables restart restarts the Linux firewall service.

emqx server supports multiple protocols:

1. ws: websocket.

2. tcp protocol

3. mqtt protocol

The default configuration of the server can be viewed in the server configuration file. The path where the configuration file is located:
/etc/emqx/emqx.conf
Looking at the configuration file,
you can find that there are 3 default port numbers: 8083 is the default port for websocket, 18083 is the management console port (default username and password: admin/public) 1883 is tcp port. As shown in the figure below,
Insert image description here
Insert image description here
the management console login interface: access address: ip:18083. If you cannot access it, be sure to turn off the firewall or add a port.
Insert image description here
After everything above is ready, start using the client tool mqttx to establish a connection with the server. mqttx can both publish and subscribe to messages. As shown in the figure below,
Insert image description here
publish and subscribe

Insert image description here

おすすめ

転載: blog.csdn.net/superzhang6666/article/details/123550386