Rule engine example

    • The filter SQL is: SELECT * FROM “message.publish” WHERE topic = ‘t/a’;
    • The action is: “print the content of the message and all the args of the action”, the action we need is ‘inspect’.

    The CLI above created a rule with ID=’Rule rule:803de6db’.

    The first two args are mandatory:

    • SQL: SELECT * FROM “message.publish” WHERE topic = ‘t/a’
    • Action List: [{“name”:”inspect”, “params”: {“a”: 1}}]. Action List is of type JSON Array. “name” is the name of the action, “params” is the parameters of the action. Note that the action does not need a resource.

    The last arg is an optional description of the rule: ‘Rule for debug’.

    If a MQTT message “hello” is sent to topic ‘t/a’, the rule “Rule rule:803de6db” will be matched, and then action “inspect” will be triggered, the following info will be printed to the emqx console:

    1. $ tail -f log/erlang.log.1
    2. (emqx@127.0.0.1)1> [inspect]
    3. Selected Data: #{clientid => <<"shawn">>,event => 'message.publish',
    4. flags => #{dup => false,retain => false},
    5. id => <<"5898704A55D6AF4430000083D0002">>,
    6. peername => <<"127.0.0.1:61770">>,qos => 1,
    7. timestamp => 1558587875090,topic => <<"t/a">>,
    8. username => undefined}
    9. Envs: #{event => 'message.publish',
    10. from => <<"shawn">>,
    11. headers =>
    12. #{allow_publish => true,
    13. peername => {{127,0,0,1},61770},
    14. username => undefined},
    15. id => <<0,5,137,135,4,165,93,106,244,67,0,0,8,61,0,2>>,
    16. payload => <<"hello">>,qos => 1,
    17. topic => <<"t/a">>}
    18. Action Init Params: #{<<"a">> => 1}
    • Selected Data listed the fields that selected by the SQL.All available fields will be listed here, as we used .
    • Envs is the environment variables that can be used internally in the action.
    • Action Init Params is the params we passed to the action.

    Send data to WebHook

    Setup a Web Service, here we setup a simple web service using the linux tool nc:

    Create a rule:

    Go to EMQX Dashboard (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"

    Bind an action:

    Click on the “+ Add” button under “Action Handler”, and then select “Data to Web Server” in the pop-up dialog window.

    image

    Bind a resource to the action:

    Since the dropdown list “Resource” is empty for now, we create a new resource by clicking on the “New Resource” to the top right, and then select “WebHook”:

    Configure the resource:

    Fill in the “Request URL” and “Request Header”(Optional):

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

    Then inspect the Web Service table, verify a new record has been received:

    image