plugins.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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`_ | Plugin Template |
  11. +---------------------------+---------------------------+
  12. | `emqttd_dashboard`_ | Web Dashboard |
  13. +---------------------------+---------------------------+
  14. | `emqttd_plugin_mysql`_ | MySQL Auth/ACL Plugin |
  15. +---------------------------+---------------------------+
  16. | `emqttd_plugin_pgsql`_ | PostgreSQL Auth/ACL Plugin|
  17. +---------------------------+---------------------------+
  18. | `emqttd_plugin_redis`_ | Redis Auth/ACL Plugin |
  19. +---------------------------+---------------------------+
  20. | `emqttd_stomp`_ | Stomp Protocol Plugin |
  21. +---------------------------+---------------------------+
  22. | `emqttd_sockjs`_ | Stomp over SockJS Plugin |
  23. +---------------------------+---------------------------+
  24. | `emqttd_recon`_ | Recon Plugin |
  25. +---------------------------+---------------------------+
  26. ----------------------------------------
  27. emqttd_plugin_template - Plugin Template
  28. ----------------------------------------
  29. A plugin is just a normal Erlang application under the 'emqttd/plugins' folder. Each plugin has e configuration file: 'etc/plugin.config'.
  30. plugins/emqttd_plugin_template is a demo plugin:
  31. +------------------------+---------------------------+
  32. | File | Description |
  33. +========================+===========================+
  34. | etc/plugin.config | Plugin config file |
  35. +------------------------+---------------------------+
  36. | ebin/ | Erlang program files |
  37. +------------------------+---------------------------+
  38. Load, unload Plugin
  39. -------------------
  40. Use 'bin/emqttd_ctl plugins' CLI to load, unload a plugin::
  41. ./bin/emqttd_ctl plugins load <PluginName>
  42. ./bin/emqttd_ctl plugins unload <PluginName>
  43. ./bin/emqttd_ctl plugins list
  44. ----------------------------------
  45. emqttd_dashboard: Dashboard Plugin
  46. ----------------------------------
  47. The Web Dashboard of emqttd broker. Address: http://localhost:18083, Default User: admin, Password: public
  48. .. image:: _static/images/dashboard.png
  49. Configure Dashboard
  50. -------------------
  51. plugins/emqttd_dashboard/etc/plugin.config::
  52. [
  53. {emqttd_dashboard, [
  54. {default_admin, [
  55. {login, "admin"},
  56. {password, "public"}
  57. ]},
  58. {listener,
  59. {emqttd_dashboard, 18083, [
  60. {acceptors, 4},
  61. {max_clients, 512}]}
  62. }
  63. ]}
  64. ].
  65. -------------------------------------------
  66. emqttd_plugin_mysql - MySQL Auth/ACL Plugin
  67. -------------------------------------------
  68. MQTT Authentication, ACL with MySQL database.
  69. MQTT User Table
  70. ---------------
  71. .. code:: sql
  72. CREATE TABLE `mqtt_user` (
  73. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  74. `username` varchar(100) DEFAULT NULL,
  75. `password` varchar(100) DEFAULT NULL,
  76. `salt` varchar(20) DEFAULT NULL,
  77. `created` datetime DEFAULT NULL,
  78. PRIMARY KEY (`id`),
  79. UNIQUE KEY `mqtt_username` (`username`)
  80. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  81. MQTT ACL Table
  82. --------------
  83. .. code:: sql
  84. CREATE TABLE `mqtt_acl` (
  85. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  86. `allow` int(1) DEFAULT NULL COMMENT '0: deny, 1: allow',
  87. `ipaddr` varchar(60) DEFAULT NULL COMMENT 'IpAddress',
  88. `username` varchar(100) DEFAULT NULL COMMENT 'Username',
  89. `clientid` varchar(100) DEFAULT NULL COMMENT 'ClientId',
  90. `access` int(2) NOT NULL COMMENT '1: subscribe, 2: publish, 3: pubsub',
  91. `topic` varchar(100) NOT NULL DEFAULT '' COMMENT 'Topic Filter',
  92. PRIMARY KEY (`id`)
  93. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  94. emqttd_plugin_mysql/etc/plugin.config
  95. -------------------------------------
  96. Configure MySQL host, username, password and database::
  97. [
  98. {emqttd_plugin_mysql, [
  99. {mysql_pool, [
  100. %% ecpool options
  101. {pool_size, 4},
  102. {auto_reconnect, 3},
  103. %% mysql options
  104. {host, "localhost"},
  105. {port, 3306},
  106. {user, ""},
  107. {password, ""},
  108. {database, "mqtt"},
  109. {encoding, utf8}
  110. ]},
  111. %% select password only
  112. {authquery, "select password from mqtt_user where username = '%u' limit 1"},
  113. %% hash algorithm: md5, sha, sha256, pbkdf2?
  114. {password_hash, sha256},
  115. %% select password with salt
  116. %% {authquery, "select password, salt from mqtt_user where username = '%u'"},
  117. %% sha256 with salt prefix
  118. %% {password_hash, {salt, sha256}},
  119. %% sha256 with salt suffix
  120. %% {password_hash, {sha256, salt}},
  121. %% comment this query, the acl will be disabled
  122. {aclquery, "select * from mqtt_acl where ipaddr = '%a' or username = '%u' or username = '$all' or clientid = '%c'"},
  123. %% If no rules matched, return...
  124. {acl_nomatch, allow}
  125. ]}
  126. ].
  127. Load emqttd_plugin_mysql plugin
  128. -------------------------------
  129. .. code::
  130. ./bin/emqttd_ctl plugins load emqttd_plugin_mysql
  131. ------------------------------------------------
  132. emqttd_plugin_pgsql - PostgreSQL Auth/ACL Plugin
  133. ------------------------------------------------
  134. MQTT Authentication, ACL with PostgreSQL Database.
  135. MQTT User Table
  136. ---------------
  137. .. code:: sql
  138. CREATE TABLE mqtt_user (
  139. id SERIAL primary key,
  140. username character varying(100),
  141. password character varying(100),
  142. salt character varying(40)
  143. );
  144. MQTT ACL Table
  145. --------------
  146. .. code:: sql
  147. CREATE TABLE mqtt_acl (
  148. id SERIAL primary key,
  149. allow integer,
  150. ipaddr character varying(60),
  151. username character varying(100),
  152. clientid character varying(100),
  153. access integer,
  154. topic character varying(100)
  155. );
  156. INSERT INTO mqtt_acl (id, allow, ipaddr, username, clientid, access, topic)
  157. VALUES
  158. (1,1,NULL,'$all',NULL,2,'#'),
  159. (2,0,NULL,'$all',NULL,1,'$SYS/#'),
  160. (3,0,NULL,'$all',NULL,1,'eq #'),
  161. (5,1,'127.0.0.1',NULL,NULL,2,'$SYS/#'),
  162. (6,1,'127.0.0.1',NULL,NULL,2,'#'),
  163. (7,1,NULL,'dashboard',NULL,1,'$SYS/#');
  164. Configure emqttd_plugin_pgsql/etc/plugin.config
  165. -----------------------------------------------
  166. Configure host, username, password and database of PostgreSQL::
  167. [
  168. {emqttd_plugin_pgsql, [
  169. {pgsql_pool, [
  170. %% ecpool options
  171. {pool_size, 4},
  172. {auto_reconnect, 3},
  173. %% pgsql options
  174. {host, "localhost"},
  175. {port, 5432},
  176. {username, "feng"},
  177. {password, ""},
  178. {database, "mqtt"},
  179. {encoding, utf8}
  180. ]},
  181. %% select password only
  182. {authquery, "select password from mqtt_user where username = '%u' limit 1"},
  183. %% hash algorithm: md5, sha, sha256, pbkdf2?
  184. {password_hash, sha256},
  185. %% select password with salt
  186. %% {authquery, "select password, salt from mqtt_user where username = '%u'"},
  187. %% sha256 with salt prefix
  188. %% {password_hash, {salt, sha256}},
  189. %% sha256 with salt suffix
  190. %% {password_hash, {sha256, salt}},
  191. %% Comment this query, the acl will be disabled. Notice: don't edit this query!
  192. {aclquery, "select allow, ipaddr, username, clientid, access, topic from mqtt_acl
  193. where ipaddr = '%a' or username = '%u' or username = '$all' or clientid = '%c'"},
  194. %% If no rules matched, return...
  195. {acl_nomatch, allow}
  196. ]}
  197. ].
  198. Load emqttd_plugin_pgsql Plugin
  199. -------------------------------
  200. .. code:: shell
  201. ./bin/emqttd_ctl plugins load emqttd_plugin_pgsql
  202. -------------------------------------------
  203. emqttd_plugin_redis - Redis Auth/ACL Plugin
  204. -------------------------------------------
  205. MQTT Authentication, ACL with Redis.
  206. Configure emqttd_plugin_redis/etc/plugin.config
  207. -----------------------------------------------
  208. .. code:: erlang
  209. [
  210. {emqttd_plugin_redis, [
  211. {eredis_pool, [
  212. %% ecpool options
  213. {pool_size, 8},
  214. {auto_reconnect, 2},
  215. %% eredis options
  216. {host, "127.0.0.1"},
  217. {port, 6379},
  218. {database, 0},
  219. {password, ""}
  220. ]},
  221. %% HMGET mqtt_user:%u password
  222. {authcmd, ["HGET", "mqtt_user:%u", "password"]},
  223. %% Password hash algorithm: plain, md5, sha, sha256, pbkdf2?
  224. {password_hash, sha256},
  225. %% SMEMBERS mqtt_acl:%u
  226. {aclcmd, ["SMEMBERS", "mqtt_acl:%u"]},
  227. %% If no rules matched, return...
  228. {acl_nomatch, deny},
  229. %% Store subscriptions to redis when SUBSCRIBE packets received.
  230. {subcmd, ["HMSET", "mqtt_subs:%u"]},
  231. %% Load Subscriptions form Redis when client connected.
  232. {loadsub, ["HGETALL", "mqtt_subs:%u"]},
  233. %% Remove subscriptions from redis when UNSUBSCRIBE packets received.
  234. {unsubcmd, ["HDEL", "mqtt_subs:%u"]}
  235. ]}
  236. ].
  237. Load emqttd_plugin_redis Plugin
  238. -------------------------------
  239. .. code:: console
  240. ./bin/emqttd_ctl plugins load emqttd_plugin_redis
  241. -----------------------------
  242. emqttd_stomp - STOMP Protocol
  243. -----------------------------
  244. Support STOMP 1.0/1.1/1.2 clients to connect to emqttd broker and communicate with MQTT Clients.
  245. Configure emqttd_stomp/etc/plugin.config
  246. ----------------------------------------
  247. .. NOTE:: TCP Port for STOMP Protocol: 61613
  248. .. code:: erlang
  249. [
  250. {emqttd_stomp, [
  251. {default_user, [
  252. {login, "guest"},
  253. {passcode, "guest"}
  254. ]},
  255. {allow_anonymous, true},
  256. %%TODO: unused...
  257. {frame, [
  258. {max_headers, 10},
  259. {max_header_length, 1024},
  260. {max_body_length, 8192}
  261. ]},
  262. {listeners, [
  263. {emqttd_stomp, 61613, [
  264. {acceptors, 4},
  265. {max_clients, 512}
  266. ]}
  267. ]}
  268. ]}
  269. ].
  270. Load emqttd_stomp Plugin
  271. ------------------------
  272. .. code::
  273. ./bin/emqttd_ctl plugins load emqttd_stomp
  274. -----------------------------------
  275. emqttd_sockjs - Stomp/SockJS Plugin
  276. -----------------------------------
  277. emqttd_sockjs plugin enables web browser to connect to emqttd broker and communicate with MQTT clients.
  278. .. NOTE:: Default TCP Port: 61616
  279. Configure emqttd_sockjs
  280. -----------------------
  281. .. code:: erlang
  282. [
  283. {emqttd_sockjs, [
  284. {sockjs, []},
  285. {cowboy_listener, {stomp_sockjs, 61616, 4}},
  286. ]}
  287. ].
  288. Load emqttd_sockjs Plugin
  289. -------------------------
  290. .. NOTE:: emqttd_stomp Plugin required.
  291. .. code:: console
  292. ./bin/emqttd_ctl plugins load emqttd_stomp
  293. ./bin/emqttd_ctl plugins load emqttd_sockjs
  294. SockJS Demo Page
  295. ----------------
  296. http://localhost:61616/index.html
  297. ---------------------------
  298. emqttd_recon - Recon Plugin
  299. ---------------------------
  300. The plugin will load `recon`_ library on a running emqttd broker. Recon libray helps to debug and optimize an Erlang application.
  301. Load emqttd_recon Plugin
  302. ------------------------
  303. .. code:: console
  304. ./bin/emqttd_ctl plugins load emqttd_recon
  305. Recon CLI
  306. ---------
  307. .. code:: console
  308. ./bin/emqttd_ctl recon
  309. recon memory #recon_alloc:memory/2
  310. recon allocated #recon_alloc:memory(allocated_types, current|max)
  311. recon bin_leak #recon:bin_leak(100)
  312. recon node_stats #recon:node_stats(10, 1000)
  313. recon remote_load Mod #recon:remote_load(Mod)
  314. ------------------------
  315. Plugin Development Guide
  316. ------------------------
  317. Create a Plugin Project
  318. -----------------------
  319. Register Auth/ACL Modules
  320. -------------------------
  321. Register Handlers for Hooks
  322. ---------------------------
  323. Register CLI Modules
  324. --------------------
  325. .. _emqttd_dashboard: https://github.com/emqtt/emqttd_dashboard
  326. .. _emqttd_plugin_mysql: https://github.com/emqtt/emqttd_plugin_mysql
  327. .. _emqttd_plugin_pgsql: https://github.com/emqtt/emqttd_plugin_pgsql
  328. .. _emqttd_plugin_redis: https://github.com/emqtt/emqttd_plugin_redis
  329. .. _emqttd_stomp: https://github.com/emqtt/emqttd_stomp
  330. .. _emqttd_sockjs: https://github.com/emqtt/emqttd_sockjs
  331. .. _emqttd_recon: https://github.com/emqtt/emqttd_recon
  332. .. _emqttd_plugin_template: https://github.com/emqtt/emqttd_plugin_template
  333. .. _recon: http://ferd.github.io/recon/