Save offline messages to Redis

Set up the Redis environment, and take MacOS X as an example:

  1. $ wget http://download.redis.io/releases/redis-4.0.14.tar.gz
  2. $ tar xzf redis-4.0.14.tar.gz
  3. $ cd redis-4.0.14
  4. $ make && make install
  5. # Start redis
  6. $ redis-server

Create rules:

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

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

Related actions:

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

Save offline Message to Redis - 图3

Fill in the action parameters:

The action of saving offline messages to Redis requires two parameters:

1). Redis Key expired TTL

2). Associated resources. Now that the resource drop-down box is empty, and you can click “New Resource” in the upper right corner to create a Redis resource:

Save offline Message to Redis - 图4

Select Redis single-node mode resources.

Save offline Message to Redis - 图5

Fill in the resource configuration:

Fill in the real Redis server address and keep other configurations as default, and then click the “Test Connection” button to ensure that the connection test is successful.

Finally click the “OK” button.

Save offline Message to Redis - 图6

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

Save offline Message to Redis - 图7

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

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

After the message is sent, you can see the message is saved in Redis through Redis CLI:

  1. $ redis-cli
  2. KEYS mqtt:msg\*
  3. hgetall Key

Save offline Message to Redis - 图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. Topic wildcard subscription is not supported to get offline messages):

Save offline Message to Redis - 图11

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

Save offline Message to Redis - 图12

Offline messages will be deleted in Redis after being received:

Save offline Message to Redis - 图13