plugins.rst 24 KB

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