Configure with Rule Engine
This section provides a comprehensive guide on setting up the rule engine according to your specific needs using a configuration file.
Rule Engine Configuration
By default, the rule engine function is disabled. To enable it, please compile with the -DENABLE_RULE_ENGINE=ON option. Once the rule engine is enabled, the repub function is supported by default.
Republish Rule
Republish is a feature in NanoMQ that enables republishing of MQTT messages. The following parameters control its behavior:
| Name | Type | Description |
|---|---|---|
| rules.repub.rules[0].server | String | repub address (mqtt-tcp://host:port) |
| rules.repub.rules[0].topic | String | repub topic |
| rules.repub.rules[0].username | String | repub username |
| rules.repub.rules[0].password | String | repub password |
| rules.repub.rules[0].proto_ver | Integer | repub protocol version, default is 4 |
| rules.repub.rules[0].clientid | String | repub clientid |
| rules.repub.rules[0].keepalive | Duration | repub keepalive |
| rules.repub.rules[0].clean_start | Boolean | repub clean_start flag, default is True |
| rules.repub.rules[0].sql | String | Rule engine SQL clause |
Create the Rule
Suppose you want to create a repub rule, upon receipt of a message from the abc topic, NanoMQ will encapsulate the topic and payload fields into a JSON structure and then forward this JSON-structured message to the topic/repub1 topic.
Test the Rule
This section will use nanomq_cli to test the newly created rule.
Start
nanomqin the first terminal window.sh$ nanomq startStart
nanomq_cliin the second terminal window, subscribe to the topictopic/repub1from the specified server address.sh$ nanomq_cli sub -t topic/repub1 connect_cb: mqtt-tcp://127.0.0.1:1883 connect result: 0 topic/repub1: {"topic":"abc","payload":"aaa"}Start a new
nanomq_cliin the third terminal window, publish the messageaaato the topicabcwith the following command:sh$ nanomq_cli pub -t abc -m aaa
You should see the message received in the second window from the topic topic/repub1.
Data Persistence with SQLite
NanoMQ supports data persistence with SQLite, see below for the configuration items. To enable SQLite, please compile with the -DNNG_ENABLE_SQLITE=ON option. For detailed operation steps, see Build from Source Code.
| Name | Type | Description |
|---|---|---|
| rules.sqlite.path | String | SQLite3 database path, default is /tmp/rule_engine.db |
| rules.sqlite.rules[0].table | String | SQLite3 database table name |
| rules.sqlite.rules[0].sql | String | Rule engine SQL clause |
Create the Rule
Suppose you want to create a data persistence rule with SQLite. When a message is received from the topic abc, the rule engine of NanoMQ will be triggered to store the contents of the topic and payload fields into a database table named broker.
Test the Rule
Start
nanomqin the first terminal window.sh$ nanomq startStart
nanomq_cliin the second terminal window, publish the messageaaato the topicabcin the second terminal window with the following command:sh$ nanomq_cli pub -t abc -m aaaThen, in the second terminal window, view the messages saved in SQLite:
sh$ sqlite3 /tmp/sqlite_rule.db SQLite version 3.11.0 2016-02-15 17:29:24 Enter ".help" for usage hints. sqlite> .header on sqlite> .table broker sqlite> select * from broker; RowId|Topic|Payload 1|abc|aaa
📢Note: Make sure that sqlite3 command is installed before using it. If it is not installed, you can install it using the following command:
apt update
apt install sqlite3Data Persistence with MySQL
NanoMQ supports data persistence with MySQL, see below for the configuration items. To enable MySQL, please install the dependencies first:
- ubuntu
apt-get install pkg-config
apt install libmysqlclient-dev- macOS
brew install pkg-config
brew install mysqlConfiguration Item
To enable MySQL, please compile with the -DENABLE_MYSQL=ON option. For detailed operation steps, see Build from Source Code.
| Name | Type | Description |
|---|---|---|
| rules.mysql.name.conn.database | String | MySQL database name, default is mysql_rule_db |
| rules.mysql.name.conn.host | String | MySQL database host |
| rules.mysql.name.conn.username | String | MySQL database username |
| rules.mysql.name.conn.password | String | MySQL database password |
| rules.mysql.name.rules[0].table | String | MySQL database table name |
| rules.mysql.name.rules[0].sql | String | Rule engine SQL clause |
Create the Rule
Suppose you want to create a data persistence rule with MySQL. When a message is received from the topic abc, the rule engine of NanoMQ will be triggered to store the contents of all fields into a database table named broker1.
Test the Rule
Start
nanomqin the first terminal window with the following command:sh$ nanomq startStart
nanomq_cliin the second terminal window, publish the messageaaato the topicabcin the second terminal window with the following command:sh$ nanomq_cli pub -t abc -m aaaThen, in the second terminal window, view the messages saved in MySQL.
sh$ mysql -u username -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 18 Server version: 5.7.33-0ubuntu0.16.04.1 (Ubuntu) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> use db_name Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from broker1; +-----+------+------+-------+-----------------+----------+----------+------------+-----------------+ | idx | Qos | Id | Topic | Clientid | Username | Password | Timestamp | Payload | +-----+------+------+-------+-----------------+----------+----------+------------+-----------------+ | 1 | 0 | 0 | abc | nanomq-fcfd2f11 | (null) | (null) | 1688437187 | aaaaaaaaaaaaaaa | +-----+------+------+-------+-----------------+----------+----------+------------+-----------------+ 1 row in set (0.00 sec)
📢Note: Make sure that all parameters in the conn configuration item are valid, and that the database parameter needs to be created in advance.