Example EMQX Elixir Plugin Template
June 17, 2024 ยท View on GitHub
An example Mix project that can be used to build an EMQX 5.7.0 plugin.
Quickstart
-
makeorMIX_ENV=prod mix release --overwrite -
Copy
_build/prod/plugrelex/elixir_plugin_template/elixir_plugin_template-0.1.0.tar.gzto thepluginsdirectory in your EMQX installation. -
emqx ctl plugins install elixir_plugin_template-0.1.0 emqx ctl plugins enable elixir_plugin_template-0.1.0 emqx ctl plugins start elixir_plugin_template-0.1.0 -
In a console in your broker (
emqx remote_console)::emqx.subscribe("topic") :emqx.publish(:emqx_message.make("topic", "payload"))You should see your message printed by the plugin.
emqx_msg: %{ extra: [], flags: %{}, from: :undefined, headers: %{}, id: <<0, 5, 216, 140, 219, 62, 202, 170, 244, 66, 0, 0, 10, 211, 0, 0>>, payload: "payload", qos: 0, timestamp: 1645474368899, topic: "topic" }
Custom configuration schema
EMQX 5.7.0 introduced the Avro configuration schema feature which allows plugins to define their own config schema to be used and managed. In order to use this feature:
- Be sure to use EMQX 5.7.0 or newer;
- Rename and edit the following files:
priv/config.hocon.example->priv/config.hoconpriv/config_i18n.json.example->priv/config_i18n.jsonpriv/config_schema.avsc.example->priv/config_schema.avsc- Refer to
priv/config_schema.avsc.enterprise.exampleif using EMQX Enterprise Edition.
- Refer to
- Set
:with_config_schema?totrueunder theemqx_plugin_optsrelease config inmix.exs. - Add
:emqx_pluginsas a dependency by uncommenting the line containingemqx_dep.(:emqx_plugins)inmix.exs's dependencies.