plugins.rst 28 KB

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