Save offline messages to PostgreSQL

TIP

Support PostgreSQL 13 and below versions

Set up the PostgreSQL database, and take MacOS X as an example:

  1. $ brew install postgresql
  2. $ brew services start postgresql

Create the mqtt database:

  1. # Create a database named'mqtt' with the username postgres
  2. $ createdb -U postgres mqtt
  3. $ psql -U postgres mqtt
  4. mqtt=> \dn;
  5. List of schemas
  6. Name | Owner
  7. --------+-------
  8. public | postgres
  9. (1 row)

Create the mqtt_msg table:

  1. $ psql -U postgres mqtt
  2. CREATE TABLE mqtt_msg (
  3. id SERIAL8 primary key,
  4. msgid character varying(64),
  5. sender character varying(64),
  6. topic character varying(255),
  7. qos integer,
  8. retain integer,
  9. payload text,
  10. arrived timestamp without time zone
  11. );

TIP

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

Create rules:

Open EMQX DashboardSave offline Message to PostgreSQL - 图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 PostgreSQL

$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 PostgreSQL - 图2

Related actions:

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

Save offline Message to PostgreSQL - 图3

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

Save offline Message to PostgreSQL - 图4

The “Create Resource” dialog box pops up

Save offline Message to PostgreSQL - 图5

Fill in the resource configuration:

Fill in the real PostgreSQL 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 PostgreSQL - 图6

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

Save offline Message to PostgreSQL - 图7

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

Save offline Message to PostgreSQL - 图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 PostgreSQL - 图9

After the message is sent, you can see the message is saved in PostgreSQL through psql:

Save offline Message to PostgreSQL - 图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 PostgreSQL - 图11

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

Save offline Message to PostgreSQL - 图12

Offline messages will be deleted in PostgreSQL after being received:

Save offline Message to PostgreSQL - 图13