Get subscription relationship from MongoDB

Set up the MongoDB database and set the user name and password to root/public. Take MacOS X as an example:

  1. $ brew install mongodb
  2. $ brew services start mongodb
  3. ## Add root/public user
  4. $ use mqtt;
  5. $ db.createUser({user: "root", pwd: "public", roles: [{role: "readWrite", db: "mqtt"}]});
  6. ## Modify the configuration and disable anonymous authentication
  7. $ vi /usr/local/etc/mongod.conf
  8. security:
  9. authorization: enabled
  10. $ brew services restart mongodb

Create the mqtt_sub table:

  1. $ mongo 127.0.0.1/mqtt -uroot -ppublic
  2. db.createCollection("mqtt_sub");

Create rules:

Open EMQX DashboardGet subscription from MongoDB - 图1 (opens new window) and select the “Rules” tab on the left.

Then fill in the rule SQL:

  1. SELECT * FROM "$events/client_connected"

image-20211214142714391

Related actions:

Select “Add Action” on the “Response Action” interface, and then select “Get Subscription List from MongoDB” in the “Add Action” drop-down box

image-20211214142813615

Fill in the action parameters:

The action of “Get subscription list from MongoDB” requires one parameter:

1). Associated resources. The resource drop-down box is empty now, and you can click “New” in the upper right corner to create a MongoDB resource:

image-20211214142840878

The “Create Resource” dialog box pops up

image-20211214142951416

Fill in the resource configuration:

Fill in the real MongoDB server address and the values corresponding to other configurations, and then click the “Test Connection” button to ensure that the connection test is successful.

Finally click the “OK” button.

image-20211214143043056

Return to the response action interface and click “OK”.

image-20211214143347354

Return to the rule creation interface and click “Create”.

image-20211214143412422

The rule has been created, and you can insert a subscription relationship into MongoDB through “mongo”:

  1. db.mqtt_sub.insert({clientid: "test", topic: "t1", qos: 1})

Get subscription from MongoDB - 图9

Log in to the device whose clientid is test via Dashboard:

image-20211214143449950

Check the “Subscription” list, and you can see that the Broker obtains the subscription relationship from MongoDB and subscribes as the agent device:

image-20211214143528939