plugins.rst 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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 is_superuser
  309. {supercmd, ["HGET", "mqtt_user:%u", "is_superuser"]}.
  310. %% HMGET mqtt_user:%u password
  311. {authcmd, ["HGET", "mqtt_user:%u", "password"]}.
  312. %% Password hash algorithm: plain, md5, sha, sha256, pbkdf2?
  313. {password_hash, sha256}.
  314. %% SMEMBERS mqtt_acl:%u
  315. {aclcmd, ["SMEMBERS", "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_subs:%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 SET
  326. ------------------
  327. The plugin uses a redis SET to store ACL rules::
  328. SADD mqtt_acl:<username> "publish topic1"
  329. SADD mqtt_acl:<username> "subscribe topic2"
  330. SADD mqtt_acl:<username> "pubsub topic3"
  331. Redis Subscription HASH
  332. -----------------------
  333. The plugin can store static subscriptions in a redis Hash::
  334. HSET mqtt_subs:<username> topic1 0
  335. HSET mqtt_subs:<username> topic2 1
  336. HSET mqtt_subs:<username> topic3 2
  337. Load Redis Auth/ACL Plugin
  338. --------------------------
  339. .. code-block:: bash
  340. ./bin/emqttd_ctl plugins load emqttd_auth_redis
  341. ---------------------------------------------
  342. emqttd_plugin_mongo - MongoDB Auth/ACL Plugin
  343. ---------------------------------------------
  344. MQTT Authentication, ACL with MongoDB: https://github.com/emqtt/emqttd_plugin_mongo
  345. Configure MongoDB Auth/ACL Plugin
  346. ---------------------------------
  347. etc/plugins/emqttd_plugin_mongo.conf:
  348. .. code-block:: erlang
  349. {mongo_pool, [
  350. {pool_size, 8},
  351. {auto_reconnect, 3},
  352. %% Mongodb Opts
  353. {host, "localhost"},
  354. {port, 27017},
  355. %% {login, ""},
  356. %% {password, ""},
  357. {database, "mqtt"}
  358. ]}.
  359. %% Variables: %u = username, %c = clientid
  360. %% Superuser Query
  361. {superquery, pool, [
  362. {collection, "mqtt_user"},
  363. {super_field, "is_superuser"},
  364. {selector, {"username", "%u"}}
  365. ]}.
  366. %% Authentication Query
  367. {authquery, pool, [
  368. {collection, "mqtt_user"},
  369. {password_field, "password"},
  370. %% Hash Algorithm: plain, md5, sha, sha256, pbkdf2?
  371. {password_hash, sha256},
  372. {selector, {"username", "%u"}}
  373. ]}.
  374. %% ACL Query: "%u" = username, "%c" = clientid
  375. {aclquery, pool, [
  376. {collection, "mqtt_acl"},
  377. {selector, {"username", "%u"}}
  378. ]}.
  379. %% If no ACL rules matched, return...
  380. {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 emqttd_auth_mongo
  417. ---------------------------
  418. emqttd_sn: MQTT-SN Protocol
  419. --------------------------
  420. MQTT-SN Protocol/Gateway Plugin.
  421. Configure MQTT-SN Plugin
  422. -------------------------
  423. .. NOTE:: UDP Port for MQTT-SN: 1884
  424. etc/plugins/emqttd_sn.conf::
  425. {listener, {1884, []}}.
  426. Load MQTT-SN Plugin
  427. -------------------
  428. .. code::
  429. ./bin/emqttd_ctl plugins load emqttd_sn
  430. -----------------------------
  431. emqttd_stomp - STOMP Protocol
  432. -----------------------------
  433. Support STOMP 1.0/1.1/1.2 clients to connect to emqttd broker and communicate with MQTT Clients.
  434. Configure Stomp Plugin
  435. ----------------------
  436. etc/plugins/emqttd_stomp.conf:
  437. .. NOTE:: Default Port for STOMP Protocol: 61613
  438. .. code-block:: erlang
  439. {default_user, [
  440. {login, "guest"},
  441. {passcode, "guest"}
  442. ]}.
  443. {allow_anonymous, true}.
  444. {frame, [
  445. {max_headers, 10},
  446. {max_header_length, 1024},
  447. {max_body_length, 8192}
  448. ]}.
  449. {listener, emqttd_stomp, 61613, [
  450. {acceptors, 4},
  451. {max_clients, 512}
  452. ]}.
  453. Load Stomp Plugin
  454. -----------------
  455. .. code-block:: bash
  456. ./bin/emqttd_ctl plugins load emqttd_stomp
  457. -----------------------------------
  458. emqttd_sockjs - STOMP/SockJS Plugin
  459. -----------------------------------
  460. emqttd_sockjs plugin enables web browser to connect to emqttd broker and communicate with MQTT clients.
  461. .. NOTE:: Default TCP Port: 61616
  462. Configure emqttd_sockjs
  463. -----------------------
  464. .. code-block:: erlang
  465. {sockjs, []}.
  466. {cowboy_listener, {stomp_sockjs, 61616, 4}}.
  467. %% TODO: unused...
  468. {stomp, [
  469. {frame, [
  470. {max_headers, 10},
  471. {max_header_length, 1024},
  472. {max_body_length, 8192}
  473. ]}
  474. ]}.
  475. Load SockJS Plugin
  476. ------------------
  477. .. NOTE:: emqttd_stomp Plugin required.
  478. .. code-block:: bash
  479. ./bin/emqttd_ctl plugins load emqttd_stomp
  480. ./bin/emqttd_ctl plugins load emqttd_sockjs
  481. SockJS Demo Page
  482. ----------------
  483. http://localhost:61616/index.html
  484. ---------------------------
  485. emqttd_recon - Recon Plugin
  486. ---------------------------
  487. The plugin loads `recon`_ library on a running emqttd broker. Recon libray helps debug and optimize an Erlang application.
  488. Load Recon Plugin
  489. -----------------
  490. .. code-block:: bash
  491. ./bin/emqttd_ctl plugins load emqttd_recon
  492. Recon CLI
  493. ---------
  494. .. code-block:: bash
  495. ./bin/emqttd_ctl recon
  496. recon memory #recon_alloc:memory/2
  497. recon allocated #recon_alloc:memory(allocated_types, current|max)
  498. recon bin_leak #recon:bin_leak(100)
  499. recon node_stats #recon:node_stats(10, 1000)
  500. recon remote_load Mod #recon:remote_load(Mod)
  501. ---------------------------------
  502. emqttd_reloader - Reloader Plugin
  503. ---------------------------------
  504. Erlang Module Reloader for Development
  505. .. NOTE:: Don't load the plugin in production!
  506. Load 'Reloader' Plugin
  507. ----------------------
  508. .. code-block:: bash
  509. ./bin/emqttd_ctl plugins load emqttd_reloader
  510. reload CLI
  511. ----------
  512. .. code-block:: bash
  513. ./bin/emqttd_ctl reload
  514. reload <Module> # Reload a Module
  515. ------------------------
  516. Plugin Development Guide
  517. ------------------------
  518. Create a Plugin Project
  519. -----------------------
  520. Clone emqttd_plugin_template source from github.com::
  521. git clone https://github.com/emqtt/emqttd_plugin_template.git
  522. Create a plugin project with erlang.mk and depends on 'emqttd' application, the 'Makefile'::
  523. PROJECT = emqttd_plugin_abc
  524. PROJECT_DESCRIPTION = emqttd abc plugin
  525. PROJECT_VERSION = 1.0
  526. DEPS = emqttd
  527. dep_emqttd = git https://github.com/emqtt/emqttd emq20
  528. COVER = true
  529. include erlang.mk
  530. Template Plugin: https://github.com/emqtt/emqttd_plugin_template
  531. Register Auth/ACL Modules
  532. -------------------------
  533. emqttd_auth_demo.erl - demo authentication module:
  534. .. code-block:: erlang
  535. -module(emqttd_auth_demo).
  536. -behaviour(emqttd_auth_mod).
  537. -include_lib("emqttd/include/emqttd.hrl").
  538. -export([init/1, check/3, description/0]).
  539. init(Opts) -> {ok, Opts}.
  540. check(#mqtt_client{client_id = ClientId, username = Username}, Password, _Opts) ->
  541. io:format("Auth Demo: clientId=~p, username=~p, password=~p~n",
  542. [ClientId, Username, Password]),
  543. ok.
  544. description() -> "Demo Auth Module".
  545. emqttd_acl_demo.erl - demo ACL module:
  546. .. code-block:: erlang
  547. -module(emqttd_acl_demo).
  548. -include_lib("emqttd/include/emqttd.hrl").
  549. %% ACL callbacks
  550. -export([init/1, check_acl/2, reload_acl/1, description/0]).
  551. init(Opts) ->
  552. {ok, Opts}.
  553. check_acl({Client, PubSub, Topic}, Opts) ->
  554. io:format("ACL Demo: ~p ~p ~p~n", [Client, PubSub, Topic]),
  555. allow.
  556. reload_acl(_Opts) ->
  557. ok.
  558. description() -> "ACL Module Demo".
  559. emqttd_plugin_template_app.erl - Register the auth/ACL modules:
  560. .. code-block:: erlang
  561. ok = emqttd_access_control:register_mod(auth, emqttd_auth_demo, []),
  562. ok = emqttd_access_control:register_mod(acl, emqttd_acl_demo, []),
  563. Register Callbacks for Hooks
  564. -----------------------------
  565. 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:
  566. +------------------------+-----------------------------------------+
  567. | Name | Description |
  568. +------------------------+-----------------------------------------+
  569. | client.connected | Run when a client connected to the |
  570. | | broker successfully |
  571. +------------------------+-----------------------------------------+
  572. | client.subscribe | Run before a client subscribes topics |
  573. +------------------------+-----------------------------------------+
  574. | client.unsubscribe | Run when a client unsubscribes topics |
  575. +------------------------+-----------------------------------------+
  576. | session.subscribed | Run after a client subscribed a topic |
  577. +------------------------+-----------------------------------------+
  578. | session.unsubscribed | Run after a client unsubscribed a topic |
  579. +------------------------+-----------------------------------------+
  580. | message.publish | Run when a message is published |
  581. +------------------------+-----------------------------------------+
  582. | message.delivered | Run when a message is delivered |
  583. +------------------------+-----------------------------------------+
  584. | message.acked | Run when a message(qos1/2) is acked |
  585. +------------------------+-----------------------------------------+
  586. | client.disconnected | Run when a client is disconnnected |
  587. +------------------------+-----------------------------------------+
  588. emqttd_plugin_template.erl for example:
  589. .. code-block:: erlang
  590. %% Called when the plugin application start
  591. load(Env) ->
  592. emqttd:hook('client.connected', fun ?MODULE:on_client_connected/3, [Env]),
  593. emqttd:hook('client.disconnected', fun ?MODULE:on_client_disconnected/3, [Env]),
  594. emqttd:hook('client.subscribe', fun ?MODULE:on_client_subscribe/4, [Env]),
  595. emqttd:hook('session.subscribed', fun ?MODULE:on_session_subscribed/4, [Env]),
  596. emqttd:hook('client.unsubscribe', fun ?MODULE:on_client_unsubscribe/4, [Env]),
  597. emqttd:hook('session.unsubscribed', fun ?MODULE:on_session_unsubscribed/4, [Env]),
  598. emqttd:hook('message.publish', fun ?MODULE:on_message_publish/2, [Env]),
  599. emqttd:hook('message.delivered', fun ?MODULE:on_message_delivered/4, [Env]),
  600. emqttd:hook('message.acked', fun ?MODULE:on_message_acked/4, [Env]).
  601. Register CLI Modules
  602. --------------------
  603. emqttd_cli_demo.erl:
  604. .. code-block:: erlang
  605. -module(emqttd_cli_demo).
  606. -include_lib("emqttd/include/emqttd_cli.hrl").
  607. -export([cmd/1]).
  608. cmd(["arg1", "arg2"]) ->
  609. ?PRINT_MSG("ok");
  610. cmd(_) ->
  611. ?USAGE([{"cmd arg1 arg2", "cmd demo"}]).
  612. emqttd_plugin_template_app.erl - register the CLI module to emqttd broker:
  613. .. code-block:: erlang
  614. emqttd_ctl:register_cmd(cmd, {emqttd_cli_demo, cmd}, []).
  615. There will be a new CLI after the plugin loaded::
  616. ./bin/emqttd_ctl cmd arg1 arg2
  617. .. _emqttd_dashboard: https://github.com/emqtt/emqttd_dashboard
  618. .. _emqttd_auth_ldap: https://github.com/emqtt/emqttd_auth_ldap
  619. .. _emqttd_auth_http: https://github.com/emqtt/emqttd_auth_http
  620. .. _emqttd_auth_mysql: https://github.com/emqtt/emqttd_auth_mysql
  621. .. _emqttd_auth_pgsql: https://github.com/emqtt/emqttd_auth_pgsql
  622. .. _emqttd_auth_redis: https://github.com/emqtt/emqttd_auth_redis
  623. .. _emqttd_auth_mongo: https://github.com/emqtt/emqttd_auth_mongo
  624. .. _emqttd_sn: https://github.com/emqtt/emqttd_sn
  625. .. _emqttd_stomp: https://github.com/emqtt/emqttd_stomp
  626. .. _emqttd_sockjs: https://github.com/emqtt/emqttd_sockjs
  627. .. _emqttd_recon: https://github.com/emqtt/emqttd_recon
  628. .. _emqttd_reloader: https://github.com/emqtt/emqttd_reloader
  629. .. _emqttd_plugin_template: https://github.com/emqtt/emqttd_plugin_template
  630. .. _recon: http://ferd.github.io/recon/