# Store Device Offline Messages to Redis Using the Rule Engine

In this article, we will simulate temperature and humidity data and report these data to EMQ X Cloud via the MQTT protocol and then use the EMQ X Cloud rules engine to dump the offline messages into Redis.

WARNING

QoS > 0 required to save offline messages

Before you start, you need to complete the following operations:

  • Deployments have already been created on EMQ X Cloud (EMQ X Cluster).
  • For professional deployment users: Please complete Peering Connection Creation first, all IPs mentioned below refer to the intranet IP of the resource.
  • For basic deployment users: No peering connection is required, all IPs below refer to the public IP of the resource.

# Redis configuration

  1. Install Redis

    docker run -itd --name redis -p 6379:6379 redis
    
    1

# EMQ X Cloud rules engine configuration

Go to Deployment Details and click on EMQ X Dashbaord to go to Dashbaord.

  1. New Resource

    Click on Rules on the left menu bar → Resources, click on New Resource and drop down to select the Redis single-node mode resource type. Fill in the Redis information you have just created and click Test. If there is an error, instantly check that the database configuration is correct. create resource

  2. Rule Testing Click on Rules on the left menu bar → Rules, click on Create and enter the following rule to match the SQL statement. We will read out its message information, if the topic is temp_hum/emqx.

    SELECT
        *
    FROM
        "temp_hum/emqx",
        "$events/session_subscribed",
        "$events/message_acked"
    WHERE
        topic =~ 'temp_hum/emqx'
    
    1
    2
    3
    4
    5
    6
    7
    8

    test sql

  3. Add a response action

    Click on Add Action in the bottom left corner, drop down and select → Offline Message → Save Offline Message to Redis, select the resource created in the first step.

    Tip

    Here you need to plan for the Redis Key expiration time, and it is recommended to save no more than 100 messages offline

    create action

  4. Click on New Rule and return to the list of rules rule list

  5. View rules monitoring monitor

# Test

  1. Use MQTT X (opens new window) to simulate temperature and humidity data reporting

    You need to replace broker.emqx.io with the created deployment connection address, and add client authentication information to the EMQ X Dashboard.

    Tip

    QoS > 0 required to send messages

    MQTTX

  2. View data dump results

    $ docker exec -it redis bash
    $ redis-cli
    $ keys *
    
    1
    2
    3

    redis

  3. Use MQTT X (opens new window) to consume offline data In MQTT X, subscribe to topic temp_hum/emqx to get offline data.

    mqttx mqttx

  4. View the data consumed by redis

    $ docker exec -it redis bash
    $ redis-cli
    $ keys *
    
    1
    2
    3

    redis