Mosquitto is a small and lightweight open source MQTT server, written in C/C++ language, using a single-core single-threaded architecture, supports deployment in embedded devices with limited resources, access to a small number of MQTT device terminals, and implements MQTT 5.0 and 3.1. 1 version of the agreement. Mosquitto fully supports the MQTT protocol features, but the basic functions of Mosquitto cluster are weak, and the cluster solutions implemented by official and third parties are difficult to support the performance requirements of large-scale and massive connections of the Internet of Things.
Therefore, Mosquitto is not suitable for MQTT servers for large-scale services, but because it is lightweight enough, it can run on any low-power microcontroller including embedded sensors, mobile devices, and embedded microprocessors. It is one of the better technology options, and combined with its bridging function, it can realize local processing and cloud transparent transmission of messages.
EMQX is a large-scale distributed IoT MQTT message server, which can efficiently and reliably connect massive IoT devices, process and distribute messages and event stream data in real time. EMQX nodes can be bridged by other types of MQTT servers and MQTT cloud services to realize cross-platform message subscription and sending. In this article, we will use a configuration example to demonstrate how to bridge Mosquitto MQTT messages to EMQX.
scene description
Suppose we have a cluster of EMQX servers emqx1
, and a Mosquitto server, we need to create a bridge on Mosquitto and forward all sensor topics sensor/#
to emqx1
Cluster, and subscribe to all control topics control/#
from EMQX.
EMQX
Thanks to the EMQX standard MQTT protocol support, Mosquitto can bridge to any version of EMQX, here we use the free online MQTT server provided by EMQX Cloud for testing:
Mosquitto
The Mosquitto version used in this article is 2.0.14. Please refer to Mosquitto Download for the download and installation method:
Simple Mosquitto MQTT bridging example
To configure Mosquitto's bridge, you need to modify the mosquitto.conf
file after installation. For each bridge, the basic content that needs to be configured is:
- The address and port of the remote EMQX server
- MQTT protocol parameters, such as protocol version, keepalive, clean_session, etc. (if not configured, use the default value)
- Client login information required by EMQX
- The subject of the message that needs to be bridged
- Configure bridge topic mapping (default no mapping)
The following is the final configuration file, and the explanation of each part of the configuration will be explained in detail below:
connection emqx1
address broker.emqx.io:1883
bridge_protocol_version mqttv50
remote_clientid emqx_c
remote_username emqx_u
remote_password public
topic sensor/# out 1
topic control/# in 1
Create a new MQTT bridge
Open the mosquitto.conf
file, add an MQTT bridge configuration at the end of the configuration file, and use emqx1 as the connection name:
connection emqx1
Configure the address and port of the remote node of the bridge
address broker.emqx.io:1883
Configure MQTT protocol version
The MQTT protocol version used by Mosquitto bridging is 3.1.1 by default. EMQX fully supports MQTT 5.0 features. Here, MQTT 5.0 version is used for bridging:
bridge_protocol_version mqttv50
Configure the remote node client ID
remote_clientid emqx_c
Configure the remote node username
remote_username emqx_u
Configure the remote node password
remote_password public
Specify the MQTT topic that needs to be bridged
The configuration format of bridging topic is topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]
, which defines the rules for bridging forwarding and receiving, where:
-
<topic>
Specifies the subject that needs to be bridged, supports wildcards The direction can be out, in or both
- out: send local topic data to remote broker
- in: Subscribe to the topic of the remote Broker and publish the data to the local
- both: two-way bridging on the same topic
-
qos-level
is the QoS level of the bridge, if not specified, the original QoS of the forwarded message will be used -
local-prefix
andremote-prefix
correspond to local and remote prefixes. When used for topic mapping, add the corresponding prefix to the forwarded and received message topics so that the application can identify the source of the message.
The following two bridging rules can be added to correspond to the scenario in this article:
topic sensor/# out 1
topic control/# in 1
After the configuration is complete, Mosquitto needs to be restarted for the MQTT bridge configuration to take effect.
Configure EMQX Server
No parameters need to be configured when using the public server. In practical applications, in order to make the Mosquitto MQTT message bridge successful, it is necessary to decide whether to configure the corresponding client authentication and authorization information according to the security configuration of the user's EMQX.
Test configuration
We can use the MQTT client tool to test whether the configuration of the MQTT bridge is successful. Here we use MQTT X CLI , a powerful and easy-to-use MQTT 5.0 command line tool developed by EMQ.
Test the out direction of the bridge
Subscribe to the topic sensor/#
on the remote EMQX, and wait to receive the data reported by the Mosquitto bridge:
mqttx sub -t "sensor/#" -h broker.emqx.io
Publish a message on the topic of sensor/1/temperature
in the local Mosquitto, which will be published in Mosquitto while bridging to the remote EMQX:
mqttx pub -t "sensor/1/temperature" -m "37.5" -q 1
At this point, the remote EMQX should be able to receive the message reported by the Mosquitto bridge:
payload: 37.5
Test the in direction of the bridge
Subscribe to the topic control/#
on the local Mosquitto, which will receive messages published on the remote EMQX:
mqttx sub -t "control/#"
Publish a message on the control/t/1
topic of the remote EMQX, and the message will be delivered in the EMQX cluster while bridging to Mosquitto local:
mqttx pub -t "control/t/1" -m "I'm EMQX" -h broker.emqx.io
The message should be received on Mosquitto at this point:
payload: I'm EMQX
In addition to Mosquitto, NanoMQ, an ultra-lightweight MQTT message server open sourced by EMQ, is also suitable for IoT edge access scenarios. We will bring a tutorial on bridging NanoMQ messages to EMQX in subsequent articles.
Copyright statement: This article is original by EMQ, please indicate the source when reprinting.
Original link: https://www.emqx.com/zh/blog/bridging-mosquitto-to-emqx-cluster
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。