plugins.rst 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. .. _plugins:
  2. =======
  3. Plugins
  4. =======
  5. The emqttd 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 emqttd 2.0 released:
  7. +---------------------------+---------------------------+
  8. | Plugin | Description |
  9. +===========================+===========================+
  10. | `emqttd_dashboard`_ | Web Dashboard |
  11. +---------------------------+---------------------------+
  12. | `emqttd_plugin_template`_ | Template Plugin |
  13. +---------------------------+---------------------------+
  14. | `emqttd_auth_ldap`_ | LDAP Auth |
  15. +---------------------------+---------------------------+
  16. | `emqttd_auth_http`_ | HTTP Auth/ACL Plugin |
  17. +---------------------------+---------------------------+
  18. | `emqttd_auth_mysql`_ | MySQL Auth/ACL Plugin |
  19. +---------------------------+---------------------------+
  20. | `emqttd_auth_pgsql`_ | PostgreSQL Auth/ACL Plugin|
  21. +---------------------------+---------------------------+
  22. | `emqttd_auth_redis`_ | Redis Auth/ACL Plugin |
  23. +---------------------------+---------------------------+
  24. | `emqttd_auth_mongo`_ | MongoDB Auth/ACL Plugin |
  25. +---------------------------+---------------------------+
  26. | `emqttd_sn`_ | MQTT-SN Protocol Plugin |
  27. +---------------------------+---------------------------+
  28. | `emqttd_stomp`_ | STOMP Protocol Plugin |
  29. +---------------------------+---------------------------+
  30. | `emqttd_sockjs`_ | STOMP over SockJS Plugin |
  31. +---------------------------+---------------------------+
  32. | `emqttd_recon`_ | Recon Plugin |
  33. +---------------------------+---------------------------+
  34. | `emqttd_reloader`_ | Reloader Plugin |
  35. +---------------------------+---------------------------+
  36. ----------------------------------------
  37. emqttd_plugin_template - Template Plugin
  38. ----------------------------------------
  39. A plugin is just a normal Erlang application which has its own configuration file: 'etc/<PluginName>.config'.
  40. emqttd_plugin_template is a demo plugin.
  41. Load, unload Plugin
  42. -------------------
  43. Use 'bin/emqttd_ctl plugins' CLI to load, unload a plugin::
  44. ./bin/emqttd_ctl plugins load <PluginName>
  45. ./bin/emqttd_ctl plugins unload <PluginName>
  46. ./bin/emqttd_ctl plugins list
  47. -----------------------------------
  48. emqttd_dashboard - Dashboard Plugin
  49. -----------------------------------
  50. The Web Dashboard for emqttd broker. The plugin will be loaded automatically when the broker started successfully.
  51. +------------------+---------------------------+
  52. | Address | http://localhost:18083 |
  53. +------------------+---------------------------+
  54. | Default User | admin |
  55. +------------------+---------------------------+
  56. | Default Password | public |
  57. +------------------+---------------------------+
  58. .. image:: _static/images/dashboard.png
  59. Configure Dashboard Plugin
  60. --------------------------
  61. etc/plugins/emqttd_dashboard.conf:
  62. .. code-block:: erlang
  63. {listener,
  64. {dashboard, 18083, [
  65. {acceptors, 4},
  66. {max_clients, 512}
  67. ]}
  68. }.
  69. ----------------------------------
  70. emqttd_auth_ldap: LDAP Auth Plugin
  71. ----------------------------------
  72. LDAP Auth Plugin: https://github.com/emqtt/emqttd_auth_ldap
  73. .. NOTE:: Supported in 2.0-beta1 release
  74. Configure LDAP Plugin
  75. ---------------------
  76. etc/plugins/emqttd_auth_ldap.conf:
  77. .. code-block:: erlang
  78. {ldap, [
  79. {servers, ["localhost"]},
  80. {port, 389},
  81. {timeout, 30},
  82. {user_dn, "uid=$u,ou=People,dc=example,dc=com"},
  83. {ssl, fasle},
  84. {sslopts, [
  85. {certfile, "ssl.crt"},
  86. {keyfile, "ssl.key"}
  87. ]}
  88. ]}.
  89. Load LDAP Plugin
  90. ----------------
  91. ./bin/emqttd_ctl plugins load emqttd_auth_ldap
  92. ---------------------------------------
  93. emqttd_auth_http - HTTP Auth/ACL Plugin
  94. ---------------------------------------
  95. MQTT Authentication/ACL with HTTP API: https://github.com/emqtt/emqttd_auth_http
  96. .. NOTE:: Supported in 1.1 release
  97. Configure HTTP Auth/ACL Plugin
  98. ------------------------------
  99. etc/plugins/emqttd_auth_http.conf:
  100. .. code-block:: erlang
  101. %% Variables: %u = username, %c = clientid, %a = ipaddress, %t = topic
  102. {super_req, [
  103. {method, post},
  104. {url, "http://localhost:8080/mqtt/superuser"},
  105. {params, [
  106. {username, "%u"},
  107. {clientid, "%c"}
  108. ]}
  109. ]}.
  110. {auth_req, [
  111. {method, post},
  112. {url, "http://localhost:8080/mqtt/auth"},
  113. {params, [
  114. {clientid, "%c"},
  115. {username, "%u"},
  116. {password, "%P"}
  117. ]}
  118. ]}.
  119. %% 'access' parameter: sub = 1, pub = 2
  120. {acl_req, [
  121. {method, post},
  122. {url, "http://localhost:8080/mqtt/acl"},
  123. {params, [
  124. {access, "%A"},
  125. {username, "%u"},
  126. {clientid, "%c"},
  127. {ipaddr, "%a"},
  128. {topic, "%t"}
  129. ]}
  130. ]}.
  131. HTTP Auth/ACL API
  132. -----------------
  133. Return 200 if ok
  134. Return 4xx if unauthorized
  135. Load HTTP Auth/ACL Plugin
  136. ----------------------------
  137. .. code:: bash
  138. ./bin/emqttd_ctl plugins load emqttd_auth_http
  139. -------------------------------------------
  140. emqttd_plugin_mysql - MySQL Auth/ACL Plugin
  141. -------------------------------------------
  142. MQTT Authentication, ACL with MySQL database.
  143. MQTT User Table
  144. ---------------
  145. .. code-block:: sql
  146. CREATE TABLE `mqtt_user` (
  147. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  148. `username` varchar(100) DEFAULT NULL,
  149. `password` varchar(100) DEFAULT NULL,
  150. `salt` varchar(20) DEFAULT NULL,
  151. `is_superuser` tinyint(1) DEFAULT 0,
  152. `created` datetime DEFAULT NULL,
  153. PRIMARY KEY (`id`),
  154. UNIQUE KEY `mqtt_username` (`username`)
  155. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  156. MQTT ACL Table
  157. --------------
  158. .. code-block:: sql
  159. CREATE TABLE `mqtt_acl` (
  160. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  161. `allow` int(1) DEFAULT NULL COMMENT '0: deny, 1: allow',
  162. `ipaddr` varchar(60) DEFAULT NULL COMMENT 'IpAddress',
  163. `username` varchar(100) DEFAULT NULL COMMENT 'Username',
  164. `clientid` varchar(100) DEFAULT NULL COMMENT 'ClientId',
  165. `access` int(2) NOT NULL COMMENT '1: subscribe, 2: publish, 3: pubsub',
  166. `topic` varchar(100) NOT NULL DEFAULT '' COMMENT 'Topic Filter',
  167. PRIMARY KEY (`id`)
  168. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  169. INSERT INTO `mqtt_acl` (`id`, `allow`, `ipaddr`, `username`, `clientid`, `access`, `topic`)
  170. VALUES
  171. (1,1,NULL,'$all',NULL,2,'#'),
  172. (2,0,NULL,'$all',NULL,1,'$SYS/#'),
  173. (3,0,NULL,'$all',NULL,1,'eq #'),
  174. (5,1,'127.0.0.1',NULL,NULL,2,'$SYS/#'),
  175. (6,1,'127.0.0.1',NULL,NULL,2,'#'),
  176. (7,1,NULL,'dashboard',NULL,1,'$SYS/#');
  177. Configure MySQL Auth/ACL Plugin
  178. -------------------------------
  179. etc/plugins/emqttd_plugin_mysql.conf:
  180. .. code-block:: erlang
  181. {mysql_pool, [
  182. %% pool options
  183. {pool_size, 8},
  184. {auto_reconnect, 1},
  185. %% mysql options
  186. {host, "localhost"},
  187. {port, 3306},
  188. {user, ""},
  189. {password, ""},
  190. {database, "mqtt"},
  191. {encoding, utf8},
  192. {keep_alive, true}
  193. ]}.
  194. %% Variables: %u = username, %c = clientid, %a = ipaddress
  195. %% Superuser Query
  196. {superquery, "select is_superuser from mqtt_user where username = '%u' limit 1"}.
  197. %% Authentication Query: select password only
  198. {authquery, "select password from mqtt_user where username = '%u' limit 1"}.
  199. %% hash algorithm: plain, md5, sha, sha256, pbkdf2?
  200. {password_hash, sha256}.
  201. %% select password with salt
  202. %% {authquery, "select password, salt from mqtt_user where username = '%u'"}.
  203. %% sha256 with salt prefix
  204. %% {password_hash, {salt, sha256}}.
  205. %% sha256 with salt suffix
  206. %% {password_hash, {sha256, salt}}.
  207. %% '%a' = ipaddress, '%u' = username, '%c' = clientid
  208. %% Comment this query, the acl will be disabled
  209. {aclquery, "select allow, ipaddr, username, clientid, access, topic from mqtt_acl where ipaddr = '%a' or username = '%u' or username = '$all' or clientid = '%c'"}.
  210. %% If no ACL rules matched, return...
  211. {acl_nomatch, allow}.
  212. Load MySQL Auth/ACL plugin
  213. --------------------------
  214. .. code-block:: bash
  215. ./bin/emqttd_ctl plugins load emqttd_plugin_mysql
  216. ------------------------------------------------
  217. emqttd_plugin_pgsql - PostgreSQL Auth/ACL Plugin
  218. ------------------------------------------------
  219. MQTT Authentication, ACL with PostgreSQL Database.
  220. Postgre MQTT User Table
  221. -----------------------
  222. .. code-block:: sql
  223. CREATE TABLE mqtt_user (
  224. id SERIAL primary key,
  225. is_superuser boolean,
  226. username character varying(100),
  227. password character varying(100),
  228. salt character varying(40)
  229. );
  230. Postgre MQTT ACL Table
  231. ----------------------
  232. .. code-block:: sql
  233. CREATE TABLE mqtt_acl (
  234. id SERIAL primary key,
  235. allow integer,
  236. ipaddr character varying(60),
  237. username character varying(100),
  238. clientid character varying(100),
  239. access integer,
  240. topic character varying(100)
  241. );
  242. INSERT INTO mqtt_acl (id, allow, ipaddr, username, clientid, access, topic)
  243. VALUES
  244. (1,1,NULL,'$all',NULL,2,'#'),
  245. (2,0,NULL,'$all',NULL,1,'$SYS/#'),
  246. (3,0,NULL,'$all',NULL,1,'eq #'),
  247. (5,1,'127.0.0.1',NULL,NULL,2,'$SYS/#'),
  248. (6,1,'127.0.0.1',NULL,NULL,2,'#'),
  249. (7,1,NULL,'dashboard',NULL,1,'$SYS/#');
  250. Configure Postgre Auth/ACL Plugin
  251. -----------------------------------------------
  252. Plugin Config: etc/plugins/emqttd_plugin_pgsql.conf.
  253. Configure host, username, password and database of PostgreSQL:
  254. .. code-block:: erlang
  255. {pgsql_pool, [
  256. %% pool options
  257. {pool_size, 8},
  258. {auto_reconnect, 3},
  259. %% pgsql options
  260. {host, "localhost"},
  261. {port, 5432},
  262. {ssl, false},
  263. {username, "feng"},
  264. {password, ""},
  265. {database, "mqtt"},
  266. {encoding, utf8}
  267. ]}.
  268. %% Variables: %u = username, %c = clientid, %a = ipaddress
  269. %% Superuser Query
  270. {superquery, "select is_superuser from mqtt_user where username = '%u' limit 1"}.
  271. %% Authentication Query: select password only
  272. {authquery, "select password from mqtt_user where username = '%u' limit 1"}.
  273. %% hash algorithm: plain, md5, sha, sha256, pbkdf2?
  274. {password_hash, sha256}.
  275. %% select password with salt
  276. %% {authquery, "select password, salt from mqtt_user where username = '%u'"}.
  277. %% sha256 with salt prefix
  278. %% {password_hash, {salt, sha256}}.
  279. %% sha256 with salt suffix
  280. %% {password_hash, {sha256, salt}}.
  281. %% Comment this query, the acl will be disabled. Notice: don't edit this query!
  282. {aclquery, "select allow, ipaddr, username, clientid, access, topic from mqtt_acl where ipaddr = '%a' or username = '%u' or username = '$all' or clientid = '%c'"}.
  283. %% If no rules matched, return...
  284. {acl_nomatch, allow}.
  285. Load Postgre Auth/ACL Plugin
  286. -----------------------------
  287. .. code-block:: bash
  288. ./bin/emqttd_ctl plugins load emqttd_auth_pgsql
  289. -------------------------------------------
  290. emqttd_plugin_redis - Redis Auth/ACL Plugin
  291. -------------------------------------------
  292. MQTT Authentication, ACL with Redis: https://github.com/emqtt/emqttd_plugin_redis
  293. Configure Redis Auth/ACL Plugin
  294. -------------------------------
  295. etc/plugins/emqttd_auth_redis.conf:
  296. .. code-block:: erlang
  297. {redis_pool, [
  298. %% pool options
  299. {pool_size, 8},
  300. {auto_reconnect, 2},
  301. %% redis options
  302. {host, "127.0.0.1"},
  303. {port, 6379},
  304. {database, 0},
  305. {password, ""}
  306. ]}.
  307. %% Variables: %u = username, %c = clientid
  308. %% HMGET mqtt_user:%u password
  309. {authcmd, "HGET mqtt_user:%u password"}.
  310. %% Password hash algorithm: plain, md5, sha, sha256, pbkdf2?
  311. {password_hash, sha256}.
  312. %% HMGET mqtt_user:%u is_superuser
  313. {supercmd, "HGET mqtt_user:%u is_superuser"}.
  314. %% HGETALL mqtt_acl:%u
  315. {aclcmd, "HGETALL mqtt_acl:%u"}.
  316. %% If no rules matched, return...
  317. {acl_nomatch, deny}.
  318. %% Load Subscriptions form Redis when client connected.
  319. {subcmd, "HGETALL mqtt_sub:%u"}.
  320. Redis User Hash
  321. ---------------
  322. Set a 'user' hash with 'password' field, for example::
  323. HSET mqtt_user:<username> is_superuser 1
  324. HSET mqtt_user:<username> password "passwd"
  325. Redis ACL Rule Hash
  326. -------------------
  327. The plugin uses a redis Hash to store ACL rules::
  328. HSET mqtt_acl:<username> topic1 1
  329. HSET mqtt_acl:<username> topic2 2
  330. HSET mqtt_acl:<username> topic3 3
  331. .. NOTE:: 1: subscribe, 2: publish, 3: pubsub
  332. Redis Subscription Hash
  333. -----------------------
  334. The plugin can store static subscriptions in a redis Hash::
  335. HSET mqtt_subs:<username> topic1 0
  336. HSET mqtt_subs:<username> topic2 1
  337. HSET mqtt_subs:<username> topic3 2
  338. Load Redis Auth/ACL Plugin
  339. --------------------------
  340. .. code-block:: bash
  341. ./bin/emqttd_ctl plugins load emqttd_auth_redis
  342. ---------------------------------------------
  343. emqttd_plugin_mongo - MongoDB Auth/ACL Plugin
  344. ---------------------------------------------
  345. MQTT Authentication, ACL with MongoDB: https://github.com/emqtt/emqttd_plugin_mongo
  346. Configure MongoDB Auth/ACL Plugin
  347. ---------------------------------
  348. etc/plugins/emqttd_plugin_mongo.conf:
  349. .. code-block:: erlang
  350. {mongo_pool, [
  351. {pool_size, 8},
  352. {auto_reconnect, 3},
  353. %% Mongodb Opts
  354. {host, "localhost"},
  355. {port, 27017},
  356. %% {login, ""},
  357. %% {password, ""},
  358. {database, "mqtt"}
  359. ]}.
  360. %% Variables: %u = username, %c = clientid
  361. %% Superuser Query
  362. {superquery, [
  363. {collection, "mqtt_user"},
  364. {super_field, "is_superuser"},
  365. {selector, {"username", "%u"}}
  366. ]}.
  367. %% Authentication Query
  368. {authquery, [
  369. {collection, "mqtt_user"},
  370. {password_field, "password"},
  371. %% Hash Algorithm: plain, md5, sha, sha256, pbkdf2?
  372. {password_hash, sha256},
  373. {selector, {"username", "%u"}}
  374. ]}.
  375. %% ACL Query: "%u" = username, "%c" = clientid
  376. {aclquery, [
  377. {collection, "mqtt_acl"},
  378. {selector, {"username", "%u"}}
  379. ]}.
  380. %% If no ACL rules matched, return...
  381. {acl_nomatch, deny}.
  382. MongoDB Database
  383. ----------------
  384. .. code-block::
  385. use mqtt
  386. db.createCollection("mqtt_user")
  387. db.createCollection("mqtt_acl")
  388. db.mqtt_user.ensureIndex({"username":1})
  389. MongoDB User Collection
  390. -----------------------
  391. .. code-block:: json
  392. {
  393. username: "user",
  394. password: "password hash",
  395. is_superuser: boolean (true, false),
  396. created: "datetime"
  397. }
  398. For example::
  399. db.mqtt_user.insert({username: "test", password: "password hash", is_superuser: false})
  400. db.mqtt_user:insert({username: "root", is_superuser: true})
  401. MongoDB ACL Collection
  402. ----------------------
  403. .. code-block:: json
  404. {
  405. username: "username",
  406. clientid: "clientid",
  407. publish: ["topic1", "topic2", ...],
  408. subscribe: ["subtop1", "subtop2", ...],
  409. pubsub: ["topic/#", "topic1", ...]
  410. }
  411. For example::
  412. db.mqtt_acl.insert({username: "test", publish: ["t/1", "t/2"], subscribe: ["user/%u", "client/%c"]})
  413. db.mqtt_acl.insert({username: "admin", pubsub: ["#"]})
  414. Load MongoDB Auth/ACL Plugin
  415. ----------------------------
  416. .. code-block:: bash
  417. ./bin/emqttd_ctl plugins load emqttd_auth_mongo
  418. ---------------------------
  419. emqttd_sn: MQTT-SN Protocol
  420. --------------------------
  421. MQTT-SN Protocol/Gateway Plugin.
  422. Configure MQTT-SN Plugin
  423. -------------------------
  424. .. NOTE:: UDP Port for MQTT-SN: 1884
  425. etc/plugins/emqttd_sn.conf::
  426. {listener, {1884, []}}.
  427. Load MQTT-SN Plugin
  428. -------------------
  429. .. code::
  430. ./bin/emqttd_ctl plugins load emqttd_sn
  431. -----------------------------
  432. emqttd_stomp - STOMP Protocol
  433. -----------------------------
  434. Support STOMP 1.0/1.1/1.2 clients to connect to emqttd broker and communicate with MQTT Clients.
  435. Configure Stomp Plugin
  436. ----------------------
  437. etc/plugins/emqttd_stomp.conf:
  438. .. NOTE:: Default Port for STOMP Protocol: 61613
  439. .. code-block:: erlang
  440. {default_user, [
  441. {login, "guest"},
  442. {passcode, "guest"}
  443. ]}.
  444. {allow_anonymous, true}.
  445. {frame, [
  446. {max_headers, 10},
  447. {max_header_length, 1024},
  448. {max_body_length, 8192}
  449. ]}.
  450. {listener, emqttd_stomp, 61613, [
  451. {acceptors, 4},
  452. {max_clients, 512}
  453. ]}.
  454. Load Stomp Plugin
  455. -----------------
  456. .. code-block:: bash
  457. ./bin/emqttd_ctl plugins load emqttd_stomp
  458. -----------------------------------
  459. emqttd_sockjs - STOMP/SockJS Plugin
  460. -----------------------------------
  461. emqttd_sockjs plugin enables web browser to connect to emqttd broker and communicate with MQTT clients.
  462. .. NOTE:: Default TCP Port: 61616
  463. Configure emqttd_sockjs
  464. -----------------------
  465. .. code-block:: erlang
  466. {sockjs, []}.
  467. {cowboy_listener, {stomp_sockjs, 61616, 4}}.
  468. %% TODO: unused...
  469. {stomp, [
  470. {frame, [
  471. {max_headers, 10},
  472. {max_header_length, 1024},
  473. {max_body_length, 8192}
  474. ]}
  475. ]}.
  476. Load SockJS Plugin
  477. ------------------
  478. .. NOTE:: emqttd_stomp Plugin required.
  479. .. code-block:: bash
  480. ./bin/emqttd_ctl plugins load emqttd_stomp
  481. ./bin/emqttd_ctl plugins load emqttd_sockjs
  482. SockJS Demo Page
  483. ----------------
  484. http://localhost:61616/index.html
  485. ---------------------------
  486. emqttd_recon - Recon Plugin
  487. ---------------------------
  488. The plugin loads `recon`_ library on a running emqttd broker. Recon libray helps debug and optimize an Erlang application.
  489. Load Recon Plugin
  490. -----------------
  491. .. code-block:: bash
  492. ./bin/emqttd_ctl plugins load emqttd_recon
  493. Recon CLI
  494. ---------
  495. .. code-block:: bash
  496. ./bin/emqttd_ctl recon
  497. recon memory #recon_alloc:memory/2
  498. recon allocated #recon_alloc:memory(allocated_types, current|max)
  499. recon bin_leak #recon:bin_leak(100)
  500. recon node_stats #recon:node_stats(10, 1000)
  501. recon remote_load Mod #recon:remote_load(Mod)
  502. ---------------------------------
  503. emqttd_reloader - Reloader Plugin
  504. ---------------------------------
  505. Erlang Module Reloader for Development
  506. .. NOTE:: Don't load the plugin in production!
  507. Load 'Reloader' Plugin
  508. ----------------------
  509. .. code-block:: bash
  510. ./bin/emqttd_ctl plugins load emqttd_reloader
  511. reload CLI
  512. ----------
  513. .. code-block:: bash
  514. ./bin/emqttd_ctl reload
  515. reload <Module> # Reload a Module
  516. ------------------------
  517. Plugin Development Guide
  518. ------------------------
  519. Create a Plugin Project
  520. -----------------------
  521. Clone emqttd_plugin_template source from github.com::
  522. git clone https://github.com/emqtt/emqttd_plugin_template.git
  523. Create a plugin project with erlang.mk and depends on 'emqttd' application, the 'Makefile'::
  524. PROJECT = emqttd_plugin_abc
  525. PROJECT_DESCRIPTION = emqttd abc plugin
  526. PROJECT_VERSION = 1.0
  527. DEPS = emqttd
  528. dep_emqttd = git https://github.com/emqtt/emqttd emq20
  529. COVER = true
  530. include erlang.mk
  531. Template Plugin: https://github.com/emqtt/emqttd_plugin_template
  532. Register Auth/ACL Modules
  533. -------------------------
  534. emqttd_auth_demo.erl - demo authentication module:
  535. .. code-block:: erlang
  536. -module(emqttd_auth_demo).
  537. -behaviour(emqttd_auth_mod).
  538. -include_lib("emqttd/include/emqttd.hrl").
  539. -export([init/1, check/3, description/0]).
  540. init(Opts) -> {ok, Opts}.
  541. check(#mqtt_client{client_id = ClientId, username = Username}, Password, _Opts) ->
  542. io:format("Auth Demo: clientId=~p, username=~p, password=~p~n",
  543. [ClientId, Username, Password]),
  544. ok.
  545. description() -> "Demo Auth Module".
  546. emqttd_acl_demo.erl - demo ACL module:
  547. .. code-block:: erlang
  548. -module(emqttd_acl_demo).
  549. -include_lib("emqttd/include/emqttd.hrl").
  550. %% ACL callbacks
  551. -export([init/1, check_acl/2, reload_acl/1, description/0]).
  552. init(Opts) ->
  553. {ok, Opts}.
  554. check_acl({Client, PubSub, Topic}, Opts) ->
  555. io:format("ACL Demo: ~p ~p ~p~n", [Client, PubSub, Topic]),
  556. allow.
  557. reload_acl(_Opts) ->
  558. ok.
  559. description() -> "ACL Module Demo".
  560. emqttd_plugin_template_app.erl - Register the auth/ACL modules:
  561. .. code-block:: erlang
  562. ok = emqttd_access_control:register_mod(auth, emqttd_auth_demo, []),
  563. ok = emqttd_access_control:register_mod(acl, emqttd_acl_demo, []),
  564. Register Callbacks for Hooks
  565. -----------------------------
  566. 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:
  567. +------------------------+-----------------------------------------+
  568. | Name | Description |
  569. +------------------------+-----------------------------------------+
  570. | client.connected | Run when a client connected to the |
  571. | | broker successfully |
  572. +------------------------+-----------------------------------------+
  573. | client.subscribe | Run before a client subscribes topics |
  574. +------------------------+-----------------------------------------+
  575. | client.unsubscribe | Run when a client unsubscribes topics |
  576. +------------------------+-----------------------------------------+
  577. | session.subscribed | Run after a client subscribed a topic |
  578. +------------------------+-----------------------------------------+
  579. | session.unsubscribed | Run after a client unsubscribed a topic |
  580. +------------------------+-----------------------------------------+
  581. | message.publish | Run when a message is published |
  582. +------------------------+-----------------------------------------+
  583. | message.delivered | Run when a message is delivered |
  584. +------------------------+-----------------------------------------+
  585. | message.acked | Run when a message(qos1/2) is acked |
  586. +------------------------+-----------------------------------------+
  587. | client.disconnected | Run when a client is disconnnected |
  588. +------------------------+-----------------------------------------+
  589. emqttd_plugin_template.erl for example:
  590. .. code-block:: erlang
  591. %% Called when the plugin application start
  592. load(Env) ->
  593. emqttd:hook('client.connected', fun ?MODULE:on_client_connected/3, [Env]),
  594. emqttd:hook('client.disconnected', fun ?MODULE:on_client_disconnected/3, [Env]),
  595. emqttd:hook('client.subscribe', fun ?MODULE:on_client_subscribe/4, [Env]),
  596. emqttd:hook('session.subscribed', fun ?MODULE:on_session_subscribed/4, [Env]),
  597. emqttd:hook('client.unsubscribe', fun ?MODULE:on_client_unsubscribe/4, [Env]),
  598. emqttd:hook('session.unsubscribed', fun ?MODULE:on_session_unsubscribed/4, [Env]),
  599. emqttd:hook('message.publish', fun ?MODULE:on_message_publish/2, [Env]),
  600. emqttd:hook('message.delivered', fun ?MODULE:on_message_delivered/4, [Env]),
  601. emqttd:hook('message.acked', fun ?MODULE:on_message_acked/4, [Env]).
  602. Register CLI Modules
  603. --------------------
  604. emqttd_cli_demo.erl:
  605. .. code-block:: erlang
  606. -module(emqttd_cli_demo).
  607. -include_lib("emqttd/include/emqttd_cli.hrl").
  608. -export([cmd/1]).
  609. cmd(["arg1", "arg2"]) ->
  610. ?PRINT_MSG("ok");
  611. cmd(_) ->
  612. ?USAGE([{"cmd arg1 arg2", "cmd demo"}]).
  613. emqttd_plugin_template_app.erl - register the CLI module to emqttd broker:
  614. .. code-block:: erlang
  615. emqttd_ctl:register_cmd(cmd, {emqttd_cli_demo, cmd}, []).
  616. There will be a new CLI after the plugin loaded::
  617. ./bin/emqttd_ctl cmd arg1 arg2
  618. .. _emqttd_dashboard: https://github.com/emqtt/emqttd_dashboard
  619. .. _emqttd_auth_ldap: https://github.com/emqtt/emqttd_auth_ldap
  620. .. _emqttd_auth_http: https://github.com/emqtt/emqttd_auth_http
  621. .. _emqttd_auth_mysql: https://github.com/emqtt/emqttd_auth_mysql
  622. .. _emqttd_auth_pgsql: https://github.com/emqtt/emqttd_auth_pgsql
  623. .. _emqttd_auth_redis: https://github.com/emqtt/emqttd_auth_redis
  624. .. _emqttd_auth_mongo: https://github.com/emqtt/emqttd_auth_mongo
  625. .. _emqttd_sn: https://github.com/emqtt/emqttd_sn
  626. .. _emqttd_stomp: https://github.com/emqtt/emqttd_stomp
  627. .. _emqttd_sockjs: https://github.com/emqtt/emqttd_sockjs
  628. .. _emqttd_recon: https://github.com/emqtt/emqttd_recon
  629. .. _emqttd_reloader: https://github.com/emqtt/emqttd_reloader
  630. .. _emqttd_plugin_template: https://github.com/emqtt/emqttd_plugin_template
  631. .. _recon: http://ferd.github.io/recon/