Webhook

# Use EMQ X Cloud rule engine to connect to MQTT Broker

In order to facilitate the message bridging of multiple MQTT Brokers, you can use the rule engine to operate.

This guide will create a rule engine bridged by MQTT Broker to achieve the following functions:

  • Forward all messages sent to the greet topic to another MQTT Broker

In order to achieve this function, we will complete the following 4 tasks:

  1. Start Mosquitto service
  2. Set the filter conditions of the rule engine
  3. Create a resource and an action
  4. Complete the rule engine creation and test

Tip

Before using the rule engine, create a deployment first. For dedicated deployment users: please complete Peering Connection first, and ensure that the servers involved in the following are established in the VPC under the peering connection. All the IP mentioned below refer to the intranet IP of the resource For free trial and shared deployment users: There is no need to complete peering connection, and the IP mentioned below refers to the public IP of the resource

# 1. Create Mosquitto service

In your cloud server, create a mosquitto service. For the convenience of demonstration, we use docker to build quickly here. (Do not use it in production environment)

sudo docker run -it -p 1883:1883 adek06/mqtt:mosquitto

After that, open the server's 1883 port

# 2. Set the filter conditions of the rule engine

Enter EMQ X Cloud Console (opens new window), and click to enter the deployment to use MQTT Broker bridge.

On the deployment page, select the rule engine and click Create.

规则引擎页

Our goal is to trigger the engine when the topic of greet receives messages. Certain SQL processing is required here:

  • Only target 'greet/#'

According to the above principles, the SQL we finally get should be as follows:

SELECT
  payload.msg as msg
FROM
  "greet/#"

# 3. Create resources and actions

Click Add Action. On the Select Action page, select Bridge Data to MQTT Broker, click Next, and on the Configure Action page, click Create Resource. 添加动作

选择 桥接数据到 MQTT Broker

On the Create Resource page, select MQTT Bridge as the resource type, fill in the private address of the server in the remote broker address, place the mount point on emqx/, and click Test. If "test available" returns, it means the test was successful.

Tip

If the test fails, please check whether the VPC peering connection is completed and whether the IP address is correct.

填写 MQTT 配置

Click OK to return to the configuration action page. The resource just created is selected by default. Fill in "${msg} FROM EMQ X CLOUD" in the message content template, and click OK.

填写消息内容模版

The created action will be displayed in the response action column. After confirming that the information is correct, click Create in the lower right corner to complete the configuration of the rule engine.

确认

# 4. Test

If you are using EMQ X Cloud for the first time, you can go to Deployment Connection Guide to view the MQTT client connection and test guide

When configuring the action in the third step, we set the mount point to emqx/. Therefore, we use the client to subscribe to the topic emqx/# of Mosquitto.

At the same time, we send "hello" to the greet topic of EMQ X Cloud, and the rule engine will be triggered. We can see that Mosquitto has received the message of "hello FROM EMQ X CLOUD"

收到转发的消息