Save offline messages to MongoDB
Create the mqtt_msg table:
Create rules:
Open EMQX Dashboard (opens new window) and select the “Rules” tab on the left.
Then fill in the rule SQL:
FROM description
t/#: The publisher publishes a message to trigger the action of saving of offline messages to MongoDB
$events/session_subscribed: The subscriber subscribes to topics to trigger the action of getting offline messages
$events/message_acked: The subscriber replies to the message ACK to trigger the action of deleting the offline message that has been received
Related actions:
Now that the resource drop-down box is empty, and you can click “Create” in the upper right corner to create a MongoDB resource:
The “Create” dialog box pops up:
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 “Confirm” button.
Return to the action interface and click “Confirm”.
Return to the rule creation interface and click “Create”.
The rule has been created, and you can send a piece of data through the WebSocket client of Dashboard (The QoS of the published message must be greater than 0):
After the message is sent, you can see the message is saved in MongoDB through mongo:
Use another client to subscribe to the topic “t/1” (the QoS of the subscribed topic must be greater than 0, otherwise the message will be received repeatedly):
After subscribing, you will receive the offline message saved in MongoDB immediately: