Article directory
-
- Preface
- 1. Building Mosquitto on Linux
- 2. Install Cpolar on Linux
- 3. Create the MQTT service public network connection address
- 4. The client remotely connects to the MQTT service
- 5. Code calls MQTT service
- 6. Fixed connection TCP public network address
- 7. Fixed address connection test
Foreword
Mosquitto is an open source message broker that implements MQTT protocol versions 3.1 and 3.1.1. It can run on different platforms, including Windows, Linux, macOS, etc. Mosquitto can be used in scenarios such as the Internet of Things, sensors, and mobile applications, providing a lightweight, reliable, and publish/subscribe model-based messaging mechanism.
The benefit of remote access via the MQTT protocol is that it enables remote communication across the network, allowing devices to be controlled and monitored in different places. At the same time, the MQTT protocol is lightweight, efficient, and reliable, and can run stably in low-bandwidth and unstable network environments. In addition, the MQTT protocol also supports multiple programming languages and platforms, making it convenient for developers to carry out secondary development and integration.
The following describes how to build the Mosquitto MQTT protocol message server in Linux, and combine it with the Cpolar intranet penetration tool to achieve remote connection!
1. Building Mosquitto on Linux
This example is on centos 7 system, using yum to install with one click:
yum install mosquitto -y
After the installation is complete, here are some commands to operate the Mosquitto service
sudo systemctl start mosquitto # Start the Mosquitto service sudo systemctl stop mosquitto # Stop Mosquitto service sudo systemctl restart mosquitto # Restart the Mosquitto service sudo systemctl status mosquitto #Check server status
After executing the startup command, enter to view the status. We can see that the service has run successfully and the local installation has been successful. Next, install the cpolar tool to penetrate the local 1883
port of the Mosquitto service to achieve remote access connection.
2. Install Cpolar on Linux
Above we have successfully installed the Mosquitto MQTT server. Next we install the cpolar intranet penetration tool on Linux and forward the public network address of the local port mapping through cpolar. We can easily achieve remote access without registering a domain name and purchasing a cloud server. Below Here are the steps to install cpolar
cpolar official website address: https://www.cpolar.com
- Use one-click script installation command
curl -L https://www.cpolar.com/static/downloads/install-release-cpolar.sh | sudo bash
- Add services to the system
sudo systemctl enable cpolar
- Start cpolar service
sudo systemctl start cpolar
After cpolar is successfully installed, access Linux port 9200 on an external browser: [http://LAN ip:9200]. Log in using your cpolar account. After logging in, you can see the cpolar web configuration interface, which is connected to the web management interface. Just configure
3. Create the MQTT service public network connection address
After logging in to the cpolar web UI management interface, click Tunnel Management – Create Tunnel on the left dashboard:
- Tunnel name: It can be customized. Be careful not to duplicate the existing tunnel name.
- Protocol: tcp
- Local address: 1883 (local default port)
- Domain name type: Temporary random TCP port
- Region: Select China VIP
Click Create
Then open the online tunnel list on the left and view the TCP address generated after the tunnel was just created. This address is the public network connection address. We can use this address to connect on any device.
4. Client remotely connects to MQTT service
The public network address was created in cpolar above. Next we use the MQTT client tool mqttfx to conduct remote connection testing. If you do not have this client, you can enter the official download: https://softblade.de/en/download-2/, download After the installation opens, click the settings icon
Connect through the cpolar public network address. The Address value is the public network address, and the Port value is the corresponding port number after the public network address. After inputting, click Apply.
Then return to the main interface and click connect. The light turns green, indicating that the connection is successful.
We can test whether we can publish messages and subscribe. First, fill in a topic subscription ID in the Subscribe subscription interface, and then click the button Subscribe to start the subscription.
Then in the Publish publishing interface, enter the same topic ID, then enter the message to be published, and click the button Publish
Returning to the Subscribe interface, we can see that the information was successfully subscribed.
5. Code calls MQTT service
Similarly, we connect at the code level, taking Java as an example, and import related packages
<dependency> <groupId>org.eclipse.paho</groupId> <artifactId>org.eclipse.paho.client.mqttv3</artifactId> <version>1.1.0</version> </dependency>
Then use the Cpolar public network address to connect, enter the ClientID, and publish a message to the same topic. Of course, for the test here, the address is written internally. For example, in a production environment, it is recommended to configure the address externally to facilitate subsequent modifications.
After the program is run, we can see that on the client Subscribe interface tool, we can see the message data published in our code, and the connection test can also be successful.
6. Fixed connection TCP public network address
It should be noted that the above steps use a random temporary TCP port address, and the generated public network address is a random temporary address, which will change randomly within 24 hours. Next, we configure a fixed TCP port address for it. This address will not change. After setting it, there will be no need to modify the address repeatedly every day.
Configuring a fixed TCP port address requires upgrading cpolar to the Professional Edition package or above.
To reserve a fixed TCP address, log in to the cpolar official website, click Reserve on the left, and find the reserved TCP address. Let’s reserve a fixed TCP address for Minecraft:
- Region: Select China
- Description: Notes, customizable
Click Keep
After the address is successfully reserved, the system will generate the corresponding fixed public address and copy it.
Open the cpolar web ui management interface, click Tunnel Management – Tunnel List on the left dashboard, find the TCP tunnel we created above, and click Edit
on the right
Modify the tunnel information and configure the successfully reserved fixed tcp address into the tunnel.
- Port type: modified to fixed tcp port
- Reserved tcp address: fill in the address successfully reserved by the official website,
Click Update
After the tunnel is successfully updated, click the status on the left dashboard – Online Tunnel List, and you can see that the public network address has been updated to a fixed TCP address.
7. Fixed address connection test
After the address is fixed, the program is modified to our fixed TCP address for connection. Of course, in the test here, the address is written internally. For example, in a production environment, it is recommended to configure the address externally to facilitate subsequent modifications.
You can see that the received subscription has received the sent message, and a permanent fixed address has been set.