Develop and deploy EMQ X plugin for Enterprise 4.3

A few EMQ X Enterprise customers were asking how they can develop their own EMQ X plugins and use them in the enterprise edition. This article is to document the steps.

How to build your own plugin

Since EMQ X v4.3, community plugins can be built in the umbrella project with only a few lines of config change. Please find the instructions in this README

Where to install your plugin

Starting from community edition v4.3.2 and enterprise edition e4.3.1, external plugins can be installed in <expand_plugins_dir>, where <expand_plubins_dir> is configurable in emqx.conf by they config key plugins.expand_plugins_dir. By default the directory is configured as $INSTALL_DIR/etc/plugins when EMQ X is installed from zip package, and /etc/emqx/plugins/ when installed from RPM or DEB packages.

What should a plugin release contain

An EMQ X plugin, is an Erlang application that implements some of the EMQ X’s hook-points. In this sense, the build artifacts to be deployed to production is similar to the built-in plugins, it consists of directories as below (taking the plugin emqx_psk_pgsql for example)

$ tree _build/default/lib/emqx_psk_pgsql/
_build/default/lib/emqx_psk_pgsql/
├── ebin
│   ├── emqx_psk_pgsql.app
│   ├── emqx_psk_pgsql.beam
│   ├── emqx_psk_pgsql_app.beam
│   ├── emqx_psk_pgsql_client.beam
│   └── emqx_psk_pgsql_sup.beam
├── etc
│   └── emqx_psk_pgsql.conf
├── include
│   └── emqx_psk_pgsql.hrl
├── priv
│   └── emqx_psk_pgsql.schema
└── src
    ├── emqx_psk_pgsql.app.src
    ├── emqx_psk_pgsql.erl
    ├── emqx_psk_pgsql_app.erl
    ├── emqx_psk_pgsql_client.erl
    └── emqx_psk_pgsql_sup.erl

src and include are optional since all they have inside are source code. ebin directory is where the compiled beam files reside. etc is per EMQ X’s convention, the directory to store the plugin’s config file, and priv directory is, per Erlang’s convention, to keep build artifacts to be released (in our case, the plugin’s config schema file).

How to load/unload your plugin

After the plugin is installed, the node needs a restart to have the new code loaded, then the external plugin works the same as the built-in plugins delivered as a part of EMQ X packages. That is, the emqx_ctl plugins command can be used to list, load, and unload plugins.

If your plugin has other dependencies

A plugin (as an Erlang or Elixir) application may have other applications as dependencies. The dependency applications should also be installed to expand_plubins_dir. e.g. In a rebar3 project, after the code is compiled, you should be able to find all the dependencies in _build/default/lib/ – which are to be copied to expand_plubins_dir

Please note though, if a dependency application is included in the EMQ X release package, it should not be installed to external plugin dir.

You can list $INSTALL_DIR/lib/ directory to find all the applications released in the EMQ X package.

The streaming database built for IoT data storage and real-time processing.

Fully managed MQTT 5.0 IoT cloud, start a 30-day free trial.

Related Links

Set up EMQ X Cloud MQTT Broker with openHAB

In this article, we will introduce to you the process of integrating openHAB with EMQ X MQTT Cloud, a Cloud-Native fully managed MQTT service.

EMQ X project newsletter 202101

Hello! This is our very first newsletter to share what’s happening in EMQ X open-source team.

EMQ X rule engine series (7) store messages to the MongoDB database

MongoDB is a product between relational database and non-relational database. Among non-relational databases, MongoDB has the most abundant functions and most resembles relational database. MongoDB is written in C++ and is an open source database system based on distributed file storage. MongoDB is designed to provide a scalable, high-performance data storage solution for data storage. It can easily add more nodes under high load to ensure service performance.