Plugins

The EMQX Broker distribution contains a large number of official plugins, which provide some basic or various extended functions.

They rely on the code API of emqxPlugin - 图1 (opens new window) or hooks for their special functions.

You can compile it with the emqxPlugin - 图2 (opens new window) core project and package it into a working package.

TIP

EMQX Enteprise does not provide source code for commercial sales, and does not support customers to develop and compile plugins by themselves.

List of plugins

The official plug-ins provided by EMQX include:

PluginConfiguration fileDescription
emqx_dashboardPlugin - 图3 (opens new window)etc/plugins/emqx_dashboard.confWeb dashboard Plugin (Default)
emqx_managementPlugin - 图4 (opens new window)etc/plugins/emqx_management.confHTTP API and CLI Management Plugin
emqx_auth_mnesiaPlugin - 图5 (opens new window)etc/plugins/emqx_auth_mnesia.confMnesia Auth/access control
emqx_auth_jwtPlugin - 图6 (opens new window)etc/plugins/emqx_auth_jwt.confJWT Auth/access control
emqx_auth_ldapPlugin - 图7 (opens new window)etc/plugins/emqx_auth_ldap.confLDAP Auth/access control
emqx_auth_httpPlugin - 图8 (opens new window)etc/plugins/emqx_auth_http.confHTTP Auth/access control
emqx_auth_mongoPlugin - 图9 (opens new window)etc/plugins/emqx_auth_mongo.confMongoDB Auth/access control
emqx_auth_mysqlPlugin - 图10 (opens new window)etc/plugins/emqx_auth_mysql.confMySQL Auth/access control
emqx_auth_pgsqlPlugin - 图11 (opens new window)etc/plugins/emqx_auth_pgsql.confPostgreSQL Auth/access control
emqx_auth_redisPlugin - 图12 (opens new window)etc/plugins/emqx_auth_redis.confRedis Auth/access control
emqx_psk_filePlugin - 图13 (opens new window)etc/plugins/emqx_psk_file.confPSK support
emqx_web_hookPlugin - 图14 (opens new window)etc/plugins/emqx_web_hook.confWeb Hook Plugin
emqx_lua_hookPlugin - 图15 (opens new window)etc/plugins/emqx_lua_hook.confLua Hook Plugin
emqx_retainerPlugin - 图16 (opens new window)etc/plugins/emqx_retainer.confRetain Message storage module
emqx_rule_enginePlugin - 图17 (opens new window)etc/plugins/emqx_rule_engine.confRule engine
emqx_bridge_mqttPlugin - 图18 (opens new window)etc/plugins/emqx_bridge_mqtt.confMQTT Message Bridge Plugin
emqx_coapPlugin - 图19 (opens new window)etc/plugins/emqx_coap.confCoAP protocol support
emqx_lwm2mPlugin - 图20 (opens new window)etc/plugins/emqx_lwm2m.confLwM2M protocol support
emqx_snPlugin - 图21 (opens new window)etc/plugins/emqx_sn.confMQTT-SN protocol support
emqx_stompPlugin - 图22 (opens new window)etc/plugins/emqx_stomp.confStomp protocol support
emqx_reconPlugin - 图23 (opens new window)etc/plugins/emqx_recon.confRecon performance debugging
emqx_plugin_templatePlugin - 图24 (opens new window)etc/plugins/emqx_plugin_template.confplugin develop template

Start and stop plugin

There are four ways to load plugins:

  1. Default loading
  2. Start and stop plugin on command line
  3. Start and stop plugin on Dashboard
  4. Start and stop plugin by calling management API

Default loading

If a plugin needs to start with the broker, add this plugin in data/loaded_plugins.

For example, the plugins that are loaded by default are:

  1. {emqx_management, true}.
  2. {emqx_recon, true}.
  3. {emqx_retainer, true}.
  4. {emqx_dashboard, true}.
  5. {emqx_rule_engine, true}.
  6. {emqx_bridge_mqtt, false}.

Start and stop plugin on command line

When the EMQX is running, plugins can be checked, loaded/unloaded by CLI - Load/Unload Plugin:

Start and stop plugin on Dashboard

If Dashboard plugin is started (by default), the plugins can be start or stopped by visiting the managing page that can be found under http://localhost:18083/plugins.

Start and stop plugins using management API

When EMQX Broker is running, you can view, start and stop a plugin through Managing and Monitoring API - Load Plugin.

Plugin development

Create plugin project

Refer to the emqx_plugin_templatePlugin - 图25 (opens new window) plugin template to create a new plugin project.

Tip

The tag of-emqx_plugin (? MODULE)should be added to <plugin name>_app.erl file to indicate that this is an EMQX Broker plugin.

Create Authentication / Access Control Module

Authentication/Access sample code - emqx_auth_demo.erl

  1. -module(emqx_auth_demo).
  2. -export([ init/1
  3. , check/2
  4. , description/0
  5. ]).
  6. init(Opts) -> {ok, Opts}.
  7. check(_ClientInfo = #{clientid := ClientId, username := Username, password := Password}, _State) ->
  8. io:format("Auth Demo: clientId=~p, username=~p, password=~p~n", [ClientId, Username, Password]),
  9. ok.
  10. description() -> "Auth Demo Module".

Access control sample code - emqx_acl_demo.erl

  1. -module(emqx_acl_demo).
  2. -include_lib("emqx/include/emqx.hrl").
  3. %% ACL callbacks
  4. -export([ init/1
  5. , check_acl/5
  6. , reload_acl/1
  7. , description/0
  8. ]).
  9. init(Opts) ->
  10. {ok, Opts}.
  11. check_acl({ClientInfo, PubSub, _NoMatchAction, Topic}, _State) ->
  12. io:format("ACL Demo: ~p ~p ~p~n", [ClientInfo, PubSub, Topic]),
  13. allow.
  14. reload_acl(_State) ->
  15. ok.
  16. description() -> "ACL Demo Module".

Example code for mounting authentication and access control hooks - emqx_plugin_template_app.erl

  1. ok = emqx:hook('client.authenticate', fun emqx_auth_demo:check/2, []),
  2. ok = emqx:hook('client.check_acl', fun emqx_acl_demo:check_acl/5, []).

Load hook

During the plugin extension, you can load hooks to handle events such as client online and offline, topic subscription, and message sending and receiving.

Hook load sample code - emqx_plugin_template.erl

  1. load(Env) ->
  2. emqx:hook('client.connect', {?MODULE, on_client_connect, [Env]}),
  3. emqx:hook('client.connack', {?MODULE, on_client_connack, [Env]}),
  4. emqx:hook('client.connected', {?MODULE, on_client_connected, [Env]}),
  5. emqx:hook('client.disconnected', {?MODULE, on_client_disconnected, [Env]}),
  6. emqx:hook('client.authenticate', {?MODULE, on_client_authenticate, [Env]}),
  7. emqx:hook('client.check_acl', {?MODULE, on_client_check_acl, [Env]}),
  8. emqx:hook('client.subscribe', {?MODULE, on_client_subscribe, [Env]}),
  9. emqx:hook('client.unsubscribe', {?MODULE, on_client_unsubscribe, [Env]}),
  10. emqx:hook('session.created', {?MODULE, on_session_created, [Env]}),
  11. emqx:hook('session.subscribed', {?MODULE, on_session_subscribed, [Env]}),
  12. emqx:hook('session.unsubscribed',{?MODULE, on_session_unsubscribed, [Env]}),
  13. emqx:hook('session.resumed', {?MODULE, on_session_resumed, [Env]}),
  14. emqx:hook('session.discarded', {?MODULE, on_session_discarded, [Env]}),
  15. emqx:hook('session.takeovered', {?MODULE, on_session_takeovered, [Env]}),
  16. emqx:hook('session.terminated', {?MODULE, on_session_terminated, [Env]}),
  17. emqx:hook('message.publish', {?MODULE, on_message_publish, [Env]}),
  18. emqx:hook('message.delivered', {?MODULE, on_message_delivered, [Env]}),
  19. emqx:hook('message.acked', {?MODULE, on_message_acked, [Env]}),
  20. emqx:hook('message.dropped', {?MODULE, on_message_dropped, [Env]}).

Register CLI commands

Processing command line sample code - emqx_cli_demo.erl

  1. -module(emqx_cli_demo).
  2. -export([cmd/1]).
  3. cmd(["arg1", "arg2"]) ->
  4. emqx_cli:print ("ok");
  5. cmd(_) ->
  6. emqx_cli:usage ([{"cmd arg1 arg2", "cmd demo"}]).

Register command line sample code - emqx_plugin_template_app.erl

  1. ok = emqx_ctl:register_command(cmd, {emqx_cli_demo, cmd}, []),

After the plugin is loaded, use ./bin/emqx_ctl to verify the new command line:

  1. ./bin/emqx_ctl cmd arg1 arg2

Plugin configuration file

Plug-in configuration files are placed in etc/${plugin_name}.conf|config. EMQX Broker supports two plugin configuration formats:

  1. Erlang native configuration file format-${plugin_name}.config:
  1. [
  2. {plugin_name, [
  3. {key, value}
  4. ]}
  5. ].
  1. Common format of k = v for sysctl-${plugin_name}.conf:
  1. plugin_name.key = value

Tip

k = v format configuration requires the plugin developer to create priv/plugin_name.schema mapping file.

Compile and publish the plugin

clone emqx project

  1. git clone https://github.com/emqx/emqx.git

Add plugin_name as a dependency by describing it in lib-extra/plugins:

  1. {erlang_plugins,
  2. [ {plugin_name, {git, "url_of_plugin", {tag, "tag_of_plugin"}}}
  3. , ....
  4. ....
  5. ]
  6. }

Build a release

  1. $ export EMQX_EXTRA_PLUGINS=plugin_name
  2. $ make

Run your code

Start the node (interactive mode)

  1. ./_build/emqx/rel/emqx/bin/emqx console

Load the plugin with the command:

  1. ./_build/emqx/rel/emqx/bin/emqx_ctl plugins load plugin_name

Tip

To have the plugin enabled/loaded by default, you can include it in data/loaded_plugins.tmpl.