getstarted.rst 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. .. _getstarted:
  2. ===========
  3. Get Started
  4. ===========
  5. --------
  6. Overview
  7. --------
  8. emqttd(Erlang MQTT Broker) is an open source MQTT broker written in Erlang/OTP. Erlang/OTP is a concurrent, fault-tolerant, soft-realtime and distributed programming platform. MQTT is an extremely lightweight publish/subscribe messaging protocol powering IoT, M2M and Mobile applications.
  9. The emqttd project is aimed to implement a scalable, distributed, extensible open-source MQTT broker for IoT, M2M and Mobile applications that hope to handle millions of concurrent MQTT clients.
  10. Highlights of the emqttd broker:
  11. * Full MQTT V3.1/3.1.1 Protocol Specifications Support
  12. * Easy to Install - Quick Install on Linux, FreeBSD, Mac and Windows
  13. * Massively scalable - Scaling to 1 million connections on a single server
  14. * Cluster and Bridge Support
  15. * Easy to extend - Hooks and plugins to customize or extend the broker
  16. * Pluggable Authentication - LDAP, MySQL, PostgreSQL, Redis Authentication Plugins
  17. --------
  18. Features
  19. --------
  20. * Full MQTT V3.1/V3.1.1 protocol specification support
  21. * QoS0, QoS1, QoS2 Publish and Subscribe
  22. * Session Management and Offline Messages
  23. * Retained Message
  24. * Last Will Message
  25. * TCP/SSL Connection
  26. * MQTT Over WebSocket(SSL)
  27. * HTTP Publish API
  28. * STOMP protocol
  29. * MQTT-SN Protocol
  30. * CoAP Protocol
  31. * STOMP over SockJS
  32. * $SYS/# Topics
  33. * ClientID Authentication
  34. * IpAddress Authentication
  35. * Username and Password Authentication
  36. * Access control based on IpAddress, ClientID, Username
  37. * Authentication with LDAP, Redis, MySQL, PostgreSQL and HTTP API
  38. * Cluster brokers on several servers
  39. * Bridge brokers locally or remotely
  40. * mosquitto, RSMB bridge
  41. * Extensible architecture with Hooks, Modules and Plugins
  42. * Passed eclipse paho interoperability tests
  43. -----------
  44. Quick Start
  45. -----------
  46. Download and Install
  47. --------------------
  48. The emqttd broker is cross-platform, which could be deployed on Linux, FreeBSD, Mac, Windows and even Raspberry Pi.
  49. Download binary package from: http://emqtt.io/downloads.
  50. Installing on Mac, for example:
  51. .. code-block:: bash
  52. unzip emqttd-macosx-2.0-beta1-20160830.zip && cd emqttd
  53. # Start emqttd
  54. ./bin/emqttd start
  55. # Check Status
  56. ./bin/emqttd_ctl status
  57. # Stop emqttd
  58. ./bin/emqttd stop
  59. Installing from Source
  60. ----------------------
  61. .. NOTE:: emqttd broker requires Erlang R18+ to build since 1.1 release.
  62. .. code-block:: bash
  63. git clone https://github.com/emqtt/emqttd.git
  64. cd emqttd && make && make dist
  65. cd rel/emqttd && ./bin/emqttd console
  66. -------------
  67. Web Dashboard
  68. -------------
  69. A Web Dashboard will be loaded when the emqttd broker is started successfully.
  70. The Dashboard helps check running status of the broker, monitor statistics and metrics of MQTT packets, query clients, sessions, topics and subscriptions.
  71. +------------------+---------------------------+
  72. | Default Address | http://localhost:18083 |
  73. +------------------+---------------------------+
  74. | Default User | admin |
  75. +------------------+---------------------------+
  76. | Default Password | public |
  77. +------------------+---------------------------+
  78. .. image:: ./_static/images/dashboard.png
  79. -------------------
  80. Modules and Plugins
  81. -------------------
  82. The Authentication and Authorization(ACL) are usually implemented by a Module or Plugin.
  83. Modules
  84. -------
  85. +-------------------------+--------------------------------------------+
  86. | emqttd_auth_clientid | Authentication with ClientId |
  87. +-------------------------+--------------------------------------------+
  88. | emqttd_auth_username | Authentication with Username and Password |
  89. +-------------------------+--------------------------------------------+
  90. | emqttd_mod_presence | Publish presence message to $SYS topics |
  91. | | when client connected or disconnected |
  92. +-------------------------+--------------------------------------------+
  93. | emqttd_mod_subscription | Subscribe topics automatically when client |
  94. | | connected |
  95. +-------------------------+--------------------------------------------+
  96. | emqttd_mod_rewrite | Topics rewrite like HTTP rewrite module |
  97. +-------------------------+--------------------------------------------+
  98. Configure the 'auth', 'module' paragraph in 'etc/emqttd.config' to enable a module.
  99. Enable 'emqttd_auth_username' module:
  100. .. code-block:: erlang
  101. %% Authentication with username, password
  102. {auth, username, [{passwd, "etc/modules/passwd.conf"}]}.
  103. Enable 'emqttd_mod_presence' module:
  104. .. code-block:: erlang
  105. %% Client presence management module. Publish presence messages when
  106. %% client connected or disconnected.
  107. {module, presence, [{qos, 0}]}.
  108. Plugins
  109. -------
  110. A plugin is an Erlang application to extend the emqttd broker.
  111. +----------------------------+-----------------------------------+
  112. | `emqttd_plugin_template`_ | Plugin template and demo |
  113. +----------------------------+-----------------------------------+
  114. | `emqttd_dashboard`_ | Web Dashboard |
  115. +----------------------------+-----------------------------------+
  116. | `emqttd_auth_ldap`_ | LDAP Auth Plugin |
  117. +----------------------------+-----------------------------------+
  118. | `emqttd_auth_http`_ | Authentication/ACL with HTTP API |
  119. +----------------------------+-----------------------------------+
  120. | `emqttd_auth_mysql` _ | Authentication with MySQL |
  121. +----------------------------+-----------------------------------+
  122. | `emqttd_auth_pgsql`_ | Authentication with PostgreSQL |
  123. +----------------------------+-----------------------------------+
  124. | `emqttd_auth_redis`_ | Authentication with Redis |
  125. +----------------------------+-----------------------------------+
  126. | `emqttd_plugin_mongo`_ | Authentication with MongoDB |
  127. +----------------------------+-----------------------------------+
  128. | `emqttd_sn`_ | MQTT-SN Protocol Plugin |
  129. +----------------------------+-----------------------------------+
  130. | `emqttd_stomp`_ | STOMP Protocol Plugin |
  131. +----------------------------+-----------------------------------+
  132. | `emqttd_sockjs`_ | SockJS(Stomp) Plugin |
  133. +----------------------------+-----------------------------------+
  134. | `emqttd_recon`_ | Recon Plugin |
  135. +----------------------------+-----------------------------------+
  136. A plugin could be enabled by 'bin/emqttd_ctl plugins load' command.
  137. For example, enable 'emqttd_auth_pgsql' plugin::
  138. ./bin/emqttd_ctl plugins load emqttd_auth_pgsql
  139. -----------------------
  140. One Million Connections
  141. -----------------------
  142. Latest release of emqttd broker is scaling to 1.3 million MQTT connections on a 12 Core, 32G CentOS server.
  143. .. NOTE::
  144. The emqttd broker only allows 512 concurrent connections by default, for 'ulimit -n' limit is 1024 on most platform.
  145. We need tune the OS Kernel, TCP Stack, Erlang VM and emqttd broker for one million connections benchmark.
  146. Linux Kernel Parameters
  147. -----------------------
  148. .. code-block:: bash
  149. # 2M:
  150. sysctl -w fs.file-max=2097152
  151. sysctl -w fs.nr_open=2097152
  152. echo 2097152 > /proc/sys/fs/nr_open
  153. # 1M:
  154. ulimit -n 1048576
  155. TCP Stack Parameters
  156. --------------------
  157. .. code-block:: bash
  158. # backlog
  159. sysctl -w net.core.somaxconn=65536
  160. Erlang VM
  161. ---------
  162. emqttd/etc/vm.args::
  163. ## max process numbers
  164. +P 2097152
  165. ## Sets the maximum number of simultaneously existing ports for this system
  166. +Q 1048576
  167. ## Increase number of concurrent ports/sockets
  168. -env ERL_MAX_PORTS 1048576
  169. -env ERTS_MAX_PORTS 1048576
  170. emqttd broker
  171. -------------
  172. emqttd/etc/emqttd.conf:
  173. .. code-block:: erlang
  174. {listener, mqtt, 1883, [
  175. %% Size of acceptor pool
  176. {acceptors, 64},
  177. %% Maximum number of concurrent clients
  178. {max_clients, 1000000},
  179. %% Socket Access Control
  180. {access, [{allow, all}]},
  181. %% Connection Options
  182. {connopts, [
  183. %% Rate Limit. Format is 'burst, rate', Unit is KB/Sec
  184. %% {rate_limit, "100,10"} %% 100K burst, 10K rate
  185. ]},
  186. ...
  187. ]}.
  188. Test Client
  189. -----------
  190. .. code-block:: bash
  191. sysctl -w net.ipv4.ip_local_port_range="500 65535"
  192. echo 1000000 > /proc/sys/fs/nr_open
  193. ulimit -n 100000
  194. ---------------------
  195. MQTT Client Libraries
  196. ---------------------
  197. GitHub: https://github.com/emqtt
  198. +--------------------+----------------------+
  199. | `emqttc`_ | Erlang MQTT Client |
  200. +--------------------+----------------------+
  201. | `emqtt_benchmark`_ | MQTT benchmark Tool |
  202. +--------------------+----------------------+
  203. | `CocoaMQTT`_ | Swift MQTT Client |
  204. +--------------------+----------------------+
  205. | `QMQTT`_ | QT MQTT Client |
  206. +--------------------+----------------------+
  207. .. _emqttc: https://github.com/emqtt/emqttc
  208. .. _emqtt_benchmark: https://github.com/emqtt/emqtt_benchmark
  209. .. _CocoaMQTT: https://github.com/emqtt/CocoaMQTT
  210. .. _QMQTT: https://github.com/emqtt/qmqtt
  211. .. _emqttd_plugin_template: https://github.com/emqtt/emqttd_plugin_template
  212. .. _emqttd_dashboard: https://github.com/emqtt/emqttd_dashboard
  213. .. _emqttd_auth_ldap: https://github.com/emqtt/emqttd_auth_ldap
  214. .. _emqttd_auth_http: https://github.com/emqtt/emqttd_auth_http
  215. .. _emqttd_auth_mysql: https://github.com/emqtt/emqttd_plugin_mysql
  216. .. _emqttd_auth_pgsql: https://github.com/emqtt/emqttd_plugin_pgsql
  217. .. _emqttd_auth_redis: https://github.com/emqtt/emqttd_plugin_redis
  218. .. _emqttd_auth_mongo: https://github.com/emqtt/emqttd_plugin_mongo
  219. .. _emqttd_reloader: https://github.com/emqtt/emqttd_reloader
  220. .. _emqttd_stomp: https://github.com/emqtt/emqttd_stomp
  221. .. _emqttd_sockjs: https://github.com/emqtt/emqttd_sockjs
  222. .. _emqttd_recon: https://github.com/emqtt/emqttd_recon
  223. .. _emqttd_sn: https://github.com/emqtt/emqttd_sn