plugins.rst 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. .. _plugins:
  2. =======
  3. Plugins
  4. =======
  5. The *EMQ* broker could be extended by plugins. Users could develop plugins to customize authentication, ACL and functions of the broker, or integrate the broker with other systems.
  6. The plugins that *EMQ* 2.0-rc.2 released:
  7. +------------------------+-------------------------------+
  8. | Plugin | Description |
  9. +========================+===============================+
  10. | `emq_dashboard`_ | Web Dashboard |
  11. +------------------------+-------------------------------+
  12. | `emq_auth_clientid`_ | ClientId Auth Plugin |
  13. +------------------------+-------------------------------+
  14. | `emq_auth_username`_ | Username/Password Auth Plugin |
  15. +------------------------+-------------------------------+
  16. | `emq_auth_ldap`_ | LDAP Auth |
  17. +------------------------+-------------------------------+
  18. | `emq_auth_http`_ | HTTP Auth/ACL Plugin |
  19. +------------------------+-------------------------------+
  20. | `emq_auth_mysql`_ | MySQL Auth/ACL Plugin |
  21. +------------------------+-------------------------------+
  22. | `emq_auth_pgsql`_ | PostgreSQL Auth/ACL Plugin |
  23. +------------------------+-------------------------------+
  24. | `emq_auth_redis`_ | Redis Auth/ACL Plugin |
  25. +------------------------+-------------------------------+
  26. | `emq_auth_mongo`_ | MongoDB Auth/ACL Plugin |
  27. +------------------------+-------------------------------+
  28. | `emq_mod_presence`_ | Presence Module |
  29. +------------------------+-------------------------------+
  30. | `emq_mod_retainer`_ | Retainer Module |
  31. +------------------------+-------------------------------+
  32. | `emq_mod_rewrite`_ | Topic Rewrite Module |
  33. +------------------------+-------------------------------+
  34. | `emq_mod_subscription`_| Subscription Module |
  35. +------------------------+-------------------------------+
  36. | `emq_coap`_ | CoAP Protocol Plugin |
  37. +------------------------+-------------------------------+
  38. | `emq_sn`_ | MQTT-SN Protocol Plugin |
  39. +------------------------+-------------------------------+
  40. | `emq_stomp`_ | STOMP Protocol Plugin |
  41. +------------------------+-------------------------------+
  42. | `emq_sockjs`_ | STOMP over SockJS Plugin |
  43. +------------------------+-------------------------------+
  44. | `emq_recon`_ | Recon Plugin |
  45. +------------------------+-------------------------------+
  46. | `emq_reloader`_ | Reloader Plugin |
  47. +------------------------+-------------------------------+
  48. | `emq_plugin_template`_ | Template Plugin |
  49. +------------------------+-------------------------------+
  50. -------------------------------------
  51. emq_plugin_template - Template Plugin
  52. -------------------------------------
  53. A plugin is just a normal Erlang application which has its own configuration file: 'etc/<PluginName>.conf|config'.
  54. emq_plugin_template is a plugin template.
  55. Load, unload Plugin
  56. -------------------
  57. Use 'bin/emqttd_ctl plugins' CLI to load, unload a plugin::
  58. ./bin/emqttd_ctl plugins load <PluginName>
  59. ./bin/emqttd_ctl plugins unload <PluginName>
  60. ./bin/emqttd_ctl plugins list
  61. ----------------------------------------
  62. emq_auth_clientid - ClientID Auth Plugin
  63. ----------------------------------------
  64. Released in 2.0-rc.2: https://github.com/emqtt/emq_auth_clientid
  65. Configure ClientID Auth Plugin
  66. ------------------------------
  67. etc/plugins/emq_auth_clientid.conf:
  68. .. code-block:: properties
  69. ## auth.client.$clientid = $password
  70. ## Examples
  71. ## auth.client.id = passwd
  72. ## auth.client.dev:devid = passwd2
  73. ## auth.client.app:appid = passwd2
  74. Load ClientId Auth Plugin
  75. -------------------------
  76. .. code-block:: bash
  77. ./bin/emqttd_ctl plugins load emq_auth_clientid
  78. ----------------------------------------
  79. emq_auth_username - Username Auth Plugin
  80. ----------------------------------------
  81. Released in 2.0-rc.2: https://github.com/emqtt/emq_auth_username
  82. Configure Username Auth Plugin
  83. ------------------------------
  84. etc/plugins/emq_auth_username.conf:
  85. .. code-block:: properties
  86. ##auth.username.$name=$password
  87. ## Examples:
  88. ##auth.username.admin=public
  89. ##auth.username.feng@emqtt.io=public
  90. Add username/password by `./bin/emqttd_ctl users` CLI:
  91. .. code-block:: bash
  92. $ ./bin/emqttd_ctl users add <Username> <Password>
  93. Load Username Auth Plugin
  94. -------------------------
  95. .. code-block:: bash
  96. ./bin/emqttd_ctl plugins load emq_auth_username
  97. --------------------------------
  98. emq_dashboard - Dashboard Plugin
  99. --------------------------------
  100. The Web Dashboard for *EMQ* broker. The plugin will be loaded automatically when the broker started successfully.
  101. +------------------+---------------------------+
  102. | Address | http://localhost:18083 |
  103. +------------------+---------------------------+
  104. | Default User | admin |
  105. +------------------+---------------------------+
  106. | Default Password | public |
  107. +------------------+---------------------------+
  108. .. image:: _static/images/dashboard.png
  109. Configure Dashboard Plugin
  110. --------------------------
  111. etc/plugins/emq_dashboard.conf:
  112. .. code-block:: properties
  113. ## HTTP Listener
  114. dashboard.listener.http = 18083
  115. dashboard.listener.http.acceptors = 2
  116. dashboard.listener.http.max_clients = 512
  117. ## HTTPS Listener
  118. ## dashboard.listener.https = 18084
  119. ## dashboard.listener.https.acceptors = 2
  120. ## dashboard.listener.https.max_clients = 512
  121. ## dashboard.listener.https.handshake_timeout = 15
  122. ## dashboard.listener.https.certfile = etc/certs/cert.pem
  123. ## dashboard.listener.https.keyfile = etc/certs/key.pem
  124. ## dashboard.listener.https.cacertfile = etc/certs/cacert.pem
  125. ## dashboard.listener.https.verify = verify_peer
  126. ## dashboard.listener.https.fail_if_no_peer_cert = true
  127. -------------------------------
  128. emq_auth_ldap: LDAP Auth Plugin
  129. -------------------------------
  130. LDAP Auth Plugin: https://github.com/emqtt/emq_auth_ldap
  131. .. NOTE:: Released in 2.0-beta.1
  132. Configure LDAP Plugin
  133. ---------------------
  134. etc/plugins/emq_auth_ldap.conf:
  135. .. code-block:: properties
  136. auth.ldap.servers = 127.0.0.1
  137. auth.ldap.port = 389
  138. auth.ldap.timeout = 30
  139. auth.ldap.user_dn = uid=%u,ou=People,dc=example,dc=com
  140. auth.ldap.ssl = false
  141. Load LDAP Plugin
  142. ----------------
  143. ./bin/emqttd_ctl plugins load emq_auth_ldap
  144. ------------------------------------
  145. emq_auth_http - HTTP Auth/ACL Plugin
  146. ------------------------------------
  147. MQTT Authentication/ACL with HTTP API: https://github.com/emqtt/emq_auth_http
  148. .. NOTE:: Supported in 1.1 release
  149. Configure HTTP Auth/ACL Plugin
  150. ------------------------------
  151. etc/plugins/emq_auth_http.conf:
  152. .. code-block:: properties
  153. ## Variables: %u = username, %c = clientid, %a = ipaddress, %P = password, %t = topic
  154. auth.http.auth_req = http://127.0.0.1:8080/mqtt/auth
  155. auth.http.auth_req.method = post
  156. auth.http.auth_req.params = clientid=%c,username=%u,password=%P
  157. auth.http.super_req = http://127.0.0.1:8080/mqtt/superuser
  158. auth.http.super_req.method = post
  159. auth.http.super_req.params = clientid=%c,username=%u
  160. ## 'access' parameter: sub = 1, pub = 2
  161. auth.http.acl_req = http://127.0.0.1:8080/mqtt/acl
  162. auth.http.acl_req.method = get
  163. auth.http.acl_req.params = access=%A,username=%u,clientid=%c,ipaddr=%a,topic=%t
  164. auth.http.acl_nomatch = deny
  165. HTTP Auth/ACL API
  166. -----------------
  167. Return 200 if ok
  168. Return 4xx if unauthorized
  169. Load HTTP Auth/ACL Plugin
  170. -------------------------
  171. .. code:: bash
  172. ./bin/emqttd_ctl plugins load emq_auth_http
  173. ----------------------------------------
  174. emq_plugin_mysql - MySQL Auth/ACL Plugin
  175. ----------------------------------------
  176. MQTT Authentication, ACL with MySQL database.
  177. MQTT User Table
  178. ---------------
  179. .. code-block:: sql
  180. CREATE TABLE `mqtt_user` (
  181. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  182. `username` varchar(100) DEFAULT NULL,
  183. `password` varchar(100) DEFAULT NULL,
  184. `salt` varchar(20) DEFAULT NULL,
  185. `is_superuser` tinyint(1) DEFAULT 0,
  186. `created` datetime DEFAULT NULL,
  187. PRIMARY KEY (`id`),
  188. UNIQUE KEY `mqtt_username` (`username`)
  189. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  190. MQTT ACL Table
  191. --------------
  192. .. code-block:: sql
  193. CREATE TABLE `mqtt_acl` (
  194. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  195. `allow` int(1) DEFAULT NULL COMMENT '0: deny, 1: allow',
  196. `ipaddr` varchar(60) DEFAULT NULL COMMENT 'IpAddress',
  197. `username` varchar(100) DEFAULT NULL COMMENT 'Username',
  198. `clientid` varchar(100) DEFAULT NULL COMMENT 'ClientId',
  199. `access` int(2) NOT NULL COMMENT '1: subscribe, 2: publish, 3: pubsub',
  200. `topic` varchar(100) NOT NULL DEFAULT '' COMMENT 'Topic Filter',
  201. PRIMARY KEY (`id`)
  202. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  203. INSERT INTO `mqtt_acl` (`id`, `allow`, `ipaddr`, `username`, `clientid`, `access`, `topic`)
  204. VALUES
  205. (1,1,NULL,'$all',NULL,2,'#'),
  206. (2,0,NULL,'$all',NULL,1,'$SYS/#'),
  207. (3,0,NULL,'$all',NULL,1,'eq #'),
  208. (5,1,'127.0.0.1',NULL,NULL,2,'$SYS/#'),
  209. (6,1,'127.0.0.1',NULL,NULL,2,'#'),
  210. (7,1,NULL,'dashboard',NULL,1,'$SYS/#');
  211. Configure MySQL Auth/ACL Plugin
  212. -------------------------------
  213. etc/plugins/emq_plugin_mysql.conf:
  214. .. code-block:: properties
  215. ## Mysql Server
  216. auth.mysql.server = 127.0.0.1:3306
  217. ## Mysql Pool Size
  218. auth.mysql.pool = 8
  219. ## Mysql Username
  220. ## auth.mysql.username =
  221. ## Mysql Password
  222. ## auth.mysql.password =
  223. ## Mysql Database
  224. auth.mysql.database = mqtt
  225. ## Variables: %u = username, %c = clientid
  226. ## Authentication Query: select password only
  227. auth.mysql.auth_query = select password from mqtt_user where username = '%u' limit 1
  228. ## Password hash: plain, md5, sha, sha256, pbkdf2
  229. auth.mysql.password_hash = sha256
  230. ## %% Superuser Query
  231. auth.mysql.super_query = select is_superuser from mqtt_user where username = '%u' limit 1
  232. ## ACL Query Command
  233. auth.mysql.acl_query = select allow, ipaddr, username, clientid, access, topic from mqtt_acl where ipaddr = '%a' or username = '%u' or username = '$all' or clientid = '%c'
  234. ## ACL nomatch
  235. auth.mysql.acl_nomatch = deny
  236. Load MySQL Auth/ACL plugin
  237. --------------------------
  238. .. code-block:: bash
  239. ./bin/emqttd_ctl plugins load emq_auth_mysql
  240. -------------------------------------------
  241. emq_auth_pgsql - PostgreSQL Auth/ACL Plugin
  242. -------------------------------------------
  243. MQTT Authentication/ACL with PostgreSQL Database.
  244. Postgre MQTT User Table
  245. -----------------------
  246. .. code-block:: sql
  247. CREATE TABLE mqtt_user (
  248. id SERIAL primary key,
  249. is_superuser boolean,
  250. username character varying(100),
  251. password character varying(100),
  252. salt character varying(40)
  253. );
  254. Postgre MQTT ACL Table
  255. ----------------------
  256. .. code-block:: sql
  257. CREATE TABLE mqtt_acl (
  258. id SERIAL primary key,
  259. allow integer,
  260. ipaddr character varying(60),
  261. username character varying(100),
  262. clientid character varying(100),
  263. access integer,
  264. topic character varying(100)
  265. );
  266. INSERT INTO mqtt_acl (id, allow, ipaddr, username, clientid, access, topic)
  267. VALUES
  268. (1,1,NULL,'$all',NULL,2,'#'),
  269. (2,0,NULL,'$all',NULL,1,'$SYS/#'),
  270. (3,0,NULL,'$all',NULL,1,'eq #'),
  271. (5,1,'127.0.0.1',NULL,NULL,2,'$SYS/#'),
  272. (6,1,'127.0.0.1',NULL,NULL,2,'#'),
  273. (7,1,NULL,'dashboard',NULL,1,'$SYS/#');
  274. Configure Postgre Auth/ACL Plugin
  275. ----------------------------------
  276. Plugin Config: etc/plugins/emq_auth_pgsql.conf.
  277. Configure host, username, password and database of PostgreSQL:
  278. .. code-block:: properties
  279. ## Postgre Server
  280. auth.pgsql.server = 127.0.0.1:5432
  281. auth.pgsql.pool = 8
  282. auth.pgsql.username = root
  283. #auth.pgsql.password =
  284. auth.pgsql.database = mqtt
  285. auth.pgsql.encoding = utf8
  286. auth.pgsql.ssl = false
  287. ## Variables: %u = username, %c = clientid, %a = ipaddress
  288. ## Authentication Query: select password only
  289. auth.pgsql.auth_query = select password from mqtt_user where username = '%u' limit 1
  290. ## Password hash: plain, md5, sha, sha256, pbkdf2
  291. auth.pgsql.password_hash = sha256
  292. ## sha256 with salt prefix
  293. ## auth.pgsql.password_hash = salt sha256
  294. ## sha256 with salt suffix
  295. ## auth.pgsql.password_hash = sha256 salt
  296. ## Superuser Query
  297. auth.pgsql.super_query = select is_superuser from mqtt_user where username = '%u' limit 1
  298. ## ACL Query. Comment this query, the acl will be disabled.
  299. auth.pgsql.acl_query = select allow, ipaddr, username, clientid, access, topic from mqtt_acl where ipaddr = '%a' or username = '%u' or username = '$all' or clientid = '%c'
  300. ## If no rules matched, return...
  301. auth.pgsql.acl_nomatch = deny
  302. Load Postgre Auth/ACL Plugin
  303. -----------------------------
  304. .. code-block:: bash
  305. ./bin/emqttd_ctl plugins load emq_auth_pgsql
  306. --------------------------------------
  307. emq_auth_redis - Redis Auth/ACL Plugin
  308. --------------------------------------
  309. MQTT Authentication, ACL with Redis: https://github.com/emqtt/emq_auth_redis
  310. Configure Redis Auth/ACL Plugin
  311. -------------------------------
  312. etc/plugins/emq_auth_redis.conf:
  313. .. code-block:: properties
  314. ## Redis Server
  315. auth.redis.server = 127.0.0.1:6379
  316. ## Redis Pool Size
  317. auth.redis.pool = 8
  318. ## Redis Database
  319. auth.redis.database = 0
  320. ## Redis Password
  321. ## auth.redis.password =
  322. ## Variables: %u = username, %c = clientid
  323. ## Authentication Query Command
  324. auth.redis.auth_cmd = HGET mqtt_user:%u password
  325. ## Password hash: plain, md5, sha, sha256, pbkdf2
  326. auth.redis.password_hash = sha256
  327. ## Superuser Query Command
  328. auth.redis.super_cmd = HGET mqtt_user:%u is_superuser
  329. ## ACL Query Command
  330. auth.redis.acl_cmd = HGETALL mqtt_acl:%u
  331. ## ACL nomatch
  332. auth.redis.acl_nomatch = deny
  333. Redis User Hash
  334. ---------------
  335. Set a 'user' hash with 'password' field, for example::
  336. HSET mqtt_user:<username> is_superuser 1
  337. HSET mqtt_user:<username> password "passwd"
  338. Redis ACL Rule Hash
  339. -------------------
  340. The plugin uses a redis Hash to store ACL rules::
  341. HSET mqtt_acl:<username> topic1 1
  342. HSET mqtt_acl:<username> topic2 2
  343. HSET mqtt_acl:<username> topic3 3
  344. .. NOTE:: 1: subscribe, 2: publish, 3: pubsub
  345. Redis Subscription Hash
  346. -----------------------
  347. The plugin can store static subscriptions in a redis Hash::
  348. HSET mqtt_subs:<username> topic1 0
  349. HSET mqtt_subs:<username> topic2 1
  350. HSET mqtt_subs:<username> topic3 2
  351. Load Redis Auth/ACL Plugin
  352. --------------------------
  353. .. code-block:: bash
  354. ./bin/emqttd_ctl plugins load emq_auth_redis
  355. ----------------------------------------
  356. emq_auth_mongo - MongoDB Auth/ACL Plugin
  357. ----------------------------------------
  358. MQTT Authentication/ACL with MongoDB: https://github.com/emqtt/emq_auth_mongo
  359. Configure MongoDB Auth/ACL Plugin
  360. ---------------------------------
  361. etc/plugins/emq_plugin_mongo.conf:
  362. .. code-block:: properties
  363. ## Mongo Server
  364. auth.mongo.server = 127.0.0.1:27017
  365. ## Mongo Pool Size
  366. auth.mongo.pool = 8
  367. ## Mongo User
  368. ## auth.mongo.user =
  369. ## Mongo Password
  370. ## auth.mongo.password =
  371. ## Mongo Database
  372. auth.mongo.database = mqtt
  373. ## auth_query
  374. auth.mongo.auth_query.collection = mqtt_user
  375. auth.mongo.auth_query.password_field = password
  376. auth.mongo.auth_query.password_hash = sha256
  377. auth.mongo.auth_query.selector = username=%u
  378. ## super_query
  379. auth.mongo.super_query.collection = mqtt_user
  380. auth.mongo.super_query.super_field = is_superuser
  381. auth.mongo.super_query.selector = username=%u
  382. ## acl_query
  383. auth.mongo.acl_query.collection = mqtt_user
  384. auth.mongo.acl_query.selector = username=%u
  385. ## acl_nomatch
  386. auth.mongo.acl_nomatch = deny
  387. MongoDB Database
  388. ----------------
  389. .. code-block::
  390. use mqtt
  391. db.createCollection("mqtt_user")
  392. db.createCollection("mqtt_acl")
  393. db.mqtt_user.ensureIndex({"username":1})
  394. MongoDB User Collection
  395. -----------------------
  396. .. code-block:: json
  397. {
  398. username: "user",
  399. password: "password hash",
  400. is_superuser: boolean (true, false),
  401. created: "datetime"
  402. }
  403. For example::
  404. db.mqtt_user.insert({username: "test", password: "password hash", is_superuser: false})
  405. db.mqtt_user:insert({username: "root", is_superuser: true})
  406. MongoDB ACL Collection
  407. ----------------------
  408. .. code-block:: json
  409. {
  410. username: "username",
  411. clientid: "clientid",
  412. publish: ["topic1", "topic2", ...],
  413. subscribe: ["subtop1", "subtop2", ...],
  414. pubsub: ["topic/#", "topic1", ...]
  415. }
  416. For example::
  417. db.mqtt_acl.insert({username: "test", publish: ["t/1", "t/2"], subscribe: ["user/%u", "client/%c"]})
  418. db.mqtt_acl.insert({username: "admin", pubsub: ["#"]})
  419. Load MongoDB Auth/ACL Plugin
  420. ----------------------------
  421. .. code-block:: bash
  422. ./bin/emqttd_ctl plugins load emq_auth_mongo
  423. ----------------------------------
  424. emq_mod_presence - Presence Module
  425. ----------------------------------
  426. `Presence` module will publish presence message to $SYS topic when a client connected or disconnected:
  427. .. NOTE:: Released in 2.0-rc.3: https://github.com/emqtt/emq_mod_presence
  428. Configure Presence Module
  429. -------------------------
  430. etc/plugins/emq_mod_presence.conf:
  431. .. code-block:: properties
  432. ## Enable presence module
  433. ## Values: on | off
  434. module.presence = on
  435. module.presence.qos = 0
  436. Load Presence Module
  437. --------------------
  438. .. NOTE:: This module will be loaded by default.
  439. .. code:: bash
  440. ./bin/emqttd_ctl plugins load emq_mod_presence
  441. ----------------------------------
  442. emq_mod_retainer - Retainer Module
  443. ----------------------------------
  444. `Retainer` module is responsbile for storing MQTT retained messages.
  445. .. NOTE:: Released in 2.0-rc.3: https://github.com/emqtt/emq_mod_retainer
  446. Configure Retainer Module
  447. -------------------------
  448. etc/plugins/emq_mod_retainer.conf:
  449. .. code-block:: properties
  450. ## disc: disc_copies, ram: ram_copies
  451. module.retainer.storage_type = ram
  452. ## Max number of retained messages
  453. module.retainer.max_message_num = 100000
  454. ## Max Payload Size of retained message
  455. module.retainer.max_payload_size = 64KB
  456. ## Expired after seconds, never expired if 0
  457. module.retainer.expired_after = 0
  458. Load Retainer Module
  459. --------------------
  460. .. NOTE:: This module will be loaded by default.
  461. .. code:: bash
  462. ./bin/emqttd_ctl plugins load emq_mod_retainer
  463. ------------------------------------------
  464. emq_mod_subscription - Subscription Module
  465. ------------------------------------------
  466. `Subscription` module forces the client to subscribe some topics when connected to the broker:
  467. .. NOTE:: Released in 2.0-rc.3: https://github.com/emqtt/emq_mod_subscription
  468. Configure Subscription Module
  469. -----------------------------
  470. etc/plugins/emq_mod_subscription.conf:
  471. .. code-block:: properties
  472. ## Subscribe the Topics automatically when client connected
  473. module.subscription.1.topic = $client/%c
  474. ## Qos of the subscription: 0 | 1 | 2
  475. module.subscription.1.qos = 1
  476. ##module.subscription.2.topic = $user/%u
  477. ##module.subscription.2.qos = 1
  478. ## Load static subscriptions from backend storage
  479. ## Values: on | off
  480. module.subscription.backend = on
  481. Load Subscription Module
  482. ------------------------
  483. .. NOTE:: This module will be loaded by default.
  484. .. code:: bash
  485. ./bin/emqttd_ctl plugins load emq_mod_subscription
  486. --------------------------------------
  487. emq_mod_rewrite - Topic Rewrite Module
  488. --------------------------------------
  489. Released in 2.0-rc.2: https://github.com/emqtt/emq_mod_rewrite
  490. Configure Rewrite Module
  491. ------------------------
  492. etc/plugins/emq_mod_rewrite.config:
  493. .. code-block:: erlang
  494. [
  495. {emq_mod_rewrite, [
  496. {rules, [
  497. %% {rewrite, Topic, Re, Dest}
  498. %% Example: x/y/ -> z/y/
  499. %% {rewrite, "x/#", "^x/y/(.+)$", "z/y/$1"},
  500. %% {rewrite, "y/+/z/#", "^y/(.+)/z/(.+)$", "y/z/$2"}
  501. ]}
  502. ]}
  503. ].
  504. Load Rewrite Module
  505. -------------------
  506. .. code:: bash
  507. ./bin/emqttd_ctl plugins load emq_mod_rewrite
  508. ------------------------------
  509. emq_coap: CoAP Protocol Plugin
  510. ------------------------------
  511. CoAP Protocol Plugin: https://github.com/emqtt/emqttd_coap
  512. Configure CoAP Plugin
  513. ---------------------
  514. .. code-block:: properties
  515. coap.server = 5683
  516. coap.prefix.mqtt = mqtt
  517. coap.handler.mqtt = emq_coap_gateway
  518. Load CoAP Protocol Plugin
  519. -------------------------
  520. .. code:: bash
  521. ./bin/emqttd_ctl plugins load emq_coap
  522. libcoap Client
  523. --------------
  524. .. code:: bash
  525. yum install libcoap
  526. % coap client publish message
  527. coap-client -m post -e "qos=0&retain=0&message=payload&topic=hello" coap://localhost/mqtt
  528. ------------------------
  529. emq_sn: MQTT-SN Protocol
  530. ------------------------
  531. MQTT-SN Protocol/Gateway Plugin.
  532. Configure MQTT-SN Plugin
  533. ------------------------
  534. .. NOTE:: UDP Port for MQTT-SN: 1884
  535. etc/plugins/emq_sn.conf:
  536. .. code-block:: properties
  537. mqtt.sn.port = 1884
  538. Load MQTT-SN Plugin
  539. -------------------
  540. .. code::
  541. ./bin/emqttd_ctl plugins load emq_sn
  542. --------------------------
  543. emq_stomp - STOMP Protocol
  544. --------------------------
  545. Support STOMP 1.0/1.1/1.2 clients to connect to emqttd broker and communicate with MQTT Clients.
  546. Configure Stomp Plugin
  547. ----------------------
  548. etc/plugins/emq_stomp.conf:
  549. .. NOTE:: Default Port for STOMP Protocol: 61613
  550. .. code-block:: properties
  551. stomp.default_user.login = guest
  552. stomp.default_user.passcode = guest
  553. stomp.allow_anonymous = true
  554. stomp.frame.max_headers = 10
  555. stomp.frame.max_header_length = 1024
  556. stomp.frame.max_body_length = 8192
  557. stomp.listener = 61613
  558. stomp.listener.acceptors = 4
  559. stomp.listener.max_clients = 512
  560. Load Stomp Plugin
  561. -----------------
  562. .. code-block:: bash
  563. ./bin/emqttd_ctl plugins load emq_stomp
  564. --------------------------------
  565. emq_sockjs - STOMP/SockJS Plugin
  566. --------------------------------
  567. emq_sockjs plugin enables web browser to connect to emqttd broker and communicate with MQTT clients.
  568. .. WARNING:: The plugin is deprecated in 2.0
  569. Configure SockJS Plugin
  570. -----------------------
  571. .. NOTE:: Default TCP Port: 61616
  572. .. code-block:: erlang
  573. [
  574. {emq_sockjs, [
  575. {sockjs, []},
  576. {cowboy_listener, {stomp_sockjs, 61616, 4}},
  577. %% TODO: unused...
  578. {stomp, [
  579. {frame, [
  580. {max_headers, 10},
  581. {max_header_length, 1024},
  582. {max_body_length, 8192}
  583. ]}
  584. ]}
  585. ]}
  586. ].
  587. Load SockJS Plugin
  588. ------------------
  589. .. code-block:: bash
  590. ./bin/emqttd_ctl plugins load emqttd_sockjs
  591. SockJS Demo Page
  592. ----------------
  593. http://localhost:61616/index.html
  594. ------------------------
  595. emq_recon - Recon Plugin
  596. ------------------------
  597. The plugin loads `recon`_ library on a running *EMQ* broker. Recon libray helps debug and optimize an Erlang application.
  598. Load Recon Plugin
  599. -----------------
  600. .. code-block:: bash
  601. ./bin/emqttd_ctl plugins load emq_recon
  602. Recon CLI
  603. ---------
  604. .. code-block:: bash
  605. ./bin/emqttd_ctl recon
  606. recon memory #recon_alloc:memory/2
  607. recon allocated #recon_alloc:memory(allocated_types, current|max)
  608. recon bin_leak #recon:bin_leak(100)
  609. recon node_stats #recon:node_stats(10, 1000)
  610. recon remote_load Mod #recon:remote_load(Mod)
  611. ------------------------------
  612. emq_reloader - Reloader Plugin
  613. ------------------------------
  614. Erlang Module Reloader for Development
  615. .. NOTE:: Don't load the plugin in production!
  616. Load `Reloader` Plugin
  617. ----------------------
  618. .. code-block:: bash
  619. ./bin/emqttd_ctl plugins load emq_reloader
  620. reload CLI
  621. ----------
  622. .. code-block:: bash
  623. ./bin/emqttd_ctl reload
  624. reload <Module> # Reload a Module
  625. ------------------------
  626. Plugin Development Guide
  627. ------------------------
  628. Create a Plugin Project
  629. -----------------------
  630. Clone emq_plugin_template source from github.com::
  631. git clone https://github.com/emqtt/emq_plugin_template.git
  632. Create a plugin project with erlang.mk and depends on 'emqttd' application, the 'Makefile'::
  633. PROJECT = emq_plugin_abc
  634. PROJECT_DESCRIPTION = emqttd abc plugin
  635. PROJECT_VERSION = 1.0
  636. BUILD_DEPS = emqttd
  637. dep_emqttd = git https://github.com/emqtt/emqttd master
  638. COVER = true
  639. include erlang.mk
  640. Template Plugin: https://github.com/emqtt/emq_plugin_template
  641. Register Auth/ACL Modules
  642. -------------------------
  643. emq_auth_demo.erl - demo authentication module:
  644. .. code-block:: erlang
  645. -module(emq_auth_demo).
  646. -behaviour(emqttd_auth_mod).
  647. -include_lib("emqttd/include/emqttd.hrl").
  648. -export([init/1, check/3, description/0]).
  649. init(Opts) -> {ok, Opts}.
  650. check(#mqtt_client{client_id = ClientId, username = Username}, Password, _Opts) ->
  651. io:format("Auth Demo: clientId=~p, username=~p, password=~p~n",
  652. [ClientId, Username, Password]),
  653. ok.
  654. description() -> "Demo Auth Module".
  655. emq_acl_demo.erl - demo ACL module:
  656. .. code-block:: erlang
  657. -module(emq_acl_demo).
  658. -include_lib("emqttd/include/emqttd.hrl").
  659. %% ACL callbacks
  660. -export([init/1, check_acl/2, reload_acl/1, description/0]).
  661. init(Opts) ->
  662. {ok, Opts}.
  663. check_acl({Client, PubSub, Topic}, Opts) ->
  664. io:format("ACL Demo: ~p ~p ~p~n", [Client, PubSub, Topic]),
  665. allow.
  666. reload_acl(_Opts) ->
  667. ok.
  668. description() -> "ACL Module Demo".
  669. emq_plugin_template_app.erl - Register the auth/ACL modules:
  670. .. code-block:: erlang
  671. ok = emqttd_access_control:register_mod(auth, emq_auth_demo, []),
  672. ok = emqttd_access_control:register_mod(acl, emq_acl_demo, []),
  673. Register Callbacks for Hooks
  674. -----------------------------
  675. The plugin could register callbacks for hooks. The hooks will be run by the broker when a client connected/disconnected, a topic subscribed/unsubscribed or a message published/delivered:
  676. +------------------------+-----------------------------------------+
  677. | Name | Description |
  678. +------------------------+-----------------------------------------+
  679. | client.connected | Run when a client connected to the |
  680. | | broker successfully |
  681. +------------------------+-----------------------------------------+
  682. | client.subscribe | Run before a client subscribes topics |
  683. +------------------------+-----------------------------------------+
  684. | client.unsubscribe | Run when a client unsubscribes topics |
  685. +------------------------+-----------------------------------------+
  686. | session.subscribed | Run after a client subscribed a topic |
  687. +------------------------+-----------------------------------------+
  688. | session.unsubscribed | Run after a client unsubscribed a topic |
  689. +------------------------+-----------------------------------------+
  690. | message.publish | Run when a message is published |
  691. +------------------------+-----------------------------------------+
  692. | message.delivered | Run when a message is delivered |
  693. +------------------------+-----------------------------------------+
  694. | message.acked | Run when a message(qos1/2) is acked |
  695. +------------------------+-----------------------------------------+
  696. | client.disconnected | Run when a client is disconnnected |
  697. +------------------------+-----------------------------------------+
  698. emq_plugin_template.erl for example:
  699. .. code-block:: erlang
  700. %% Called when the plugin application start
  701. load(Env) ->
  702. emqttd:hook('client.connected', fun ?MODULE:on_client_connected/3, [Env]),
  703. emqttd:hook('client.disconnected', fun ?MODULE:on_client_disconnected/3, [Env]),
  704. emqttd:hook('client.subscribe', fun ?MODULE:on_client_subscribe/4, [Env]),
  705. emqttd:hook('session.subscribed', fun ?MODULE:on_session_subscribed/4, [Env]),
  706. emqttd:hook('client.unsubscribe', fun ?MODULE:on_client_unsubscribe/4, [Env]),
  707. emqttd:hook('session.unsubscribed', fun ?MODULE:on_session_unsubscribed/4, [Env]),
  708. emqttd:hook('message.publish', fun ?MODULE:on_message_publish/2, [Env]),
  709. emqttd:hook('message.delivered', fun ?MODULE:on_message_delivered/4, [Env]),
  710. emqttd:hook('message.acked', fun ?MODULE:on_message_acked/4, [Env]).
  711. Register CLI Modules
  712. --------------------
  713. emq_cli_demo.erl:
  714. .. code-block:: erlang
  715. -module(emqttd_cli_demo).
  716. -include_lib("emqttd/include/emqttd_cli.hrl").
  717. -export([cmd/1]).
  718. cmd(["arg1", "arg2"]) ->
  719. ?PRINT_MSG("ok");
  720. cmd(_) ->
  721. ?USAGE([{"cmd arg1 arg2", "cmd demo"}]).
  722. emq_plugin_template_app.erl - register the CLI module to *EMQ* broker:
  723. .. code-block:: erlang
  724. emqttd_ctl:register_cmd(cmd, {emq_cli_demo, cmd}, []).
  725. There will be a new CLI after the plugin loaded::
  726. ./bin/emqttd_ctl cmd arg1 arg2
  727. Create Configuration File
  728. -------------------------
  729. Create `etc/${plugin_name}.conf|config` file for the plugin. The *EMQ* broker supports two type of config syntax:
  730. 1. ${plugin_name}.config with erlang syntax:
  731. .. code-block:: erlang
  732. [
  733. {plugin_name, [
  734. {key, value}
  735. ]}
  736. ].
  737. 2. ${plugin_name}.conf with a general `k = v` syntax:
  738. .. code-block:: properties
  739. plugin_name.key = value
  740. Build and Release the Plugin
  741. ----------------------------
  742. 1. clone emqttd-relx project:
  743. .. code-block:: bash
  744. git clone https://github.com/emqtt/emqttd-relx.git
  745. 2. Add `DEPS` in Makefile:
  746. .. code-block:: makefile
  747. DEPS += plugin_name
  748. dep_plugin_name = git url_of_plugin
  749. 3. Add the plugin in relx.config:
  750. .. code-block:: erlang
  751. {plugin_name, load},
  752. .. _emq_dashboard: https://github.com/emqtt/emqttd_dashboard
  753. .. _emq_auth_clientid: https://github.com/emqtt/emq_auth_clientid
  754. .. _emq_auth_username: https://github.com/emqtt/emq_auth_username
  755. .. _emq_auth_ldap: https://github.com/emqtt/emq_auth_ldap
  756. .. _emq_auth_http: https://github.com/emqtt/emq_auth_http
  757. .. _emq_auth_mysql: https://github.com/emqtt/emq_auth_mysql
  758. .. _emq_auth_pgsql: https://github.com/emqtt/emq_auth_pgsql
  759. .. _emq_auth_redis: https://github.com/emqtt/emq_auth_redis
  760. .. _emq_auth_mongo: https://github.com/emqtt/emq_auth_mongo
  761. .. _emq_mod_rewrite: https://github.com/emqtt/emq_mod_rewrite
  762. .. _emq_sn: https://github.com/emqtt/emq_sn
  763. .. _emq_coap: https://github.com/emqtt/emq_coap
  764. .. _emq_stomp: https://github.com/emqtt/emq_stomp
  765. .. _emq_sockjs: https://github.com/emqtt/emq_sockjs
  766. .. _emq_recon: https://github.com/emqtt/emq_recon
  767. .. _emq_reloader: https://github.com/emqtt/emq_reloader
  768. .. _emq_plugin_template: https://github.com/emqtt/emq_plugin_template
  769. .. _recon: http://ferd.github.io/recon/
  770. .. _emq_mod_retainer: https://github.com/emqtt/emq_mod_retainer
  771. .. _emq_mod_presence: https://github.com/emqtt/emq_mod_presence
  772. .. _emq_mod_subscription: https://github.com/emqtt/emq_mod_subscription