Save data to Redis

Setup a Redis database, taking Mac OSX for instance:

  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 a rule:

Go to EMQX DashboardSave data to Redis - 图1 (opens new window), select the “rule” tab on the menu to the left.

Select “message.publish”, then type in the following SQL:

  1. SELECT
  2. *
  3. FROM
  4. "message.publish"

image

Bind an action:

  1. Click on the "+ Add" button under "Action Handler", and then select
  2. "Data to MySQL" in the pop-up dialog window.

image

Fill in the parameters required by the action:

Two parameters is required by action “Data to Redis”:

1). Redis CMD. The redis command you’d like to run when the action is triggered. In this example we’ll insert a message into redis, so type in the following command:

  1. HMSET mqtt:msg:${id} id ${id} from ${clientid} qos ${qos} topic ${topic} payload ${payload} retain ${retain} ts ${timestamp}

image

2). Bind a resource to the action. Since the dropdown list “Resource” is empty for now, we create a new resource by clicking on the “Create” to the right, and then select “Redis Single Mode”:

Configure the resource:

  1. Fill in the "Redis Server", and keep all other configs as default,
  2. and click on the "Testing Connection" button to make sure the
  3. connection can be created successfully, and then click on the
  4. "Create" button.

image

Back to the “Actions” dialog, and then click on the “Confirm” button.

Back to the creating rule page, then click on “Create” button. The rule we created will be show in the rule list:

image

We have finished, testing the rule by sending an MQTT message to emqx:

  1. Topic: "t/1"
  2. QoS: 0
  3. Retained: false
  4. Payload: "hello"

Then inspect the Redis table, verify a new record has been inserted:

  1. \$ redis-cli
  2. 127.0.0.1:6379\> KEYS mqtt:msg\*
  3. 127.0.0.1:6379\> hgetall <key\>

image

And from the rule list, verify that the “Matched” column has increased to 1:

image