Save offline messages to MySQL

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

  1. $ brew install mysql
  2. $ brew services start mysql
  3. $ mysql -u root -h localhost -p
  4. ALTER USER 'root'@'localhost' IDENTIFIED BY 'public';

Initialize the MySQL database:

  1. $ mysql -u root -h localhost -ppublic
  2. create database mqtt;

Create the mqtt_msg table:

  1. DROP TABLE IF EXISTS `mqtt_msg`;
  2. CREATE TABLE `mqtt_msg` (
  3. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  4. `msgid` varchar(64) DEFAULT NULL,
  5. `topic` varchar(180) NOT NULL,
  6. `sender` varchar(64) DEFAULT NULL,
  7. `qos` tinyint(1) NOT NULL DEFAULT '0',
  8. `retain` tinyint(1) DEFAULT NULL,
  9. `payload` blob,
  10. `arrived` datetime NOT NULL,
  11. PRIMARY KEY (`id`),
  12. INDEX topic_index(`id`, `topic`)
  13. ) ENGINE=InnoDB DEFAULT CHARSET=utf8MB4;

TIP

The message table structure cannot be modified. Please use the above SQL statement to create

Create rules:

Open EMQX DashboardSave offline Message to MySQL - 图1 (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 MySQL

$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

  1. SELECT * FROM "t/#", "$events/session_subscribed", "$events/message_acked" WHERE topic =~ 't/#'

Save offline Message to MySQL - 图2

Related actions:

Select “Add Action” on the “Response Action” interface, and then select “Save offline messages to MySQL” in the “Add Action” drop-down box

Save offline Message to MySQL - 图3

Now that the resource drop-down box is empty, and you can click “New” in the upper right corner to create a MySQL resource:

Save offline Message to MySQL - 图4

The “Create Resource” dialog box pops up

Save offline Message to MySQL - 图5

Fill in the resource configuration:

Fill in the real MySQL 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.

Save offline Message to MySQL - 图6

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

Save offline Message to MySQL - 图7

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

Save offline Message to MySQL - 图8

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):

Save offline Message to MySQL - 图9

After the message is sent, you can see the message is saved in MySQL through mysql:

Save offline Message to MySQL - 图10

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):

Save offline Message to MySQL - 图11

After subscribing, you will receive the offline message saved in MySQL immediately:

Save offline Message to MySQL - 图12

Offline messages will be deleted in MySQL after being received:

Save offline Message to MySQL - 图13