How to use ejabberd as an Elixir application dependency

Starting from version 16.02, ejabberd is packaged as an Hex.pm application: ejabberd on hex.pm. In case of doubt, you can refer to ejabberd official documentation: Embedding ejabberd in an Elixir application

It means that you can now build a customized XMPP messaging platform with Elixir on top of ejabberd. You do so by leveraging ejabberd code base in your app and providing only your custom modules.

This makes the management of your ejabberd plugins easier and cleaner.

ejabberd_elixir

To create your own application depending on ejabberd, you can go through the following steps:

  1. Create new Elixir app with mix:
    mix new ejapp
    * creating README.md
    * creating .gitignore
    * creating mix.exs
    * creating config
    * creating config/config.exs
    * creating lib
    * creating lib/ejapp.ex
    * creating test
    * creating test/test_helper.exs
    * creating test/ejapp_test.exs
    
    Your Mix project was created successfully.
    You can use "mix" to compile it, test it, and more:
    
    cd ejapp
    mix test
    
    Run "mix help" for more commands.
    
  2. Get to your new app directory:
    cd ejapp
    
  3. Add ejabberd package as a dependency in your mix.exs file:
    defmodule Ejapp.Mixfile do
    ...
      defp deps do
        [{:ejabberd, "~> 16.2"}]
      end
    end
    
  4. Start ejabberd application from mix.exs when your app is started:
    defmodule Ejapp.Mixfile do
    ...
      def application do
        [applications: [:logger, :ejabberd]]
      end
    ...
    end
    
  5. Create or get ejabberd config file:
    (cd config; wget https://gist.githubusercontent.com/mremond/383666d563025e86adfe/raw/723dfa50c955c112777f3361b4f2067b76a55d7b/ejabberd.yml)
    
  6. Define ejabberd configuration reference in config/config.exs:
    config :ejabberd,
      file: "config/ejabberd.yml",
      log_path: 'logs/ejabberd.log'
    
    # Customize Mnesia directory:
    config :mnesia,
      dir: 'mnesiadb/'
    
  7. Create log dir:
    mkdir logs
    
  8. Compile everything:
    mix do deps.get, compile
    
  9. Start your app, ejabberd will be started as a dependency:
    iex -S mix
    
  10. Register user from Elixir console:
    :ejabberd_auth.try_register("test", "localhost", "passw0rd")
    
  11. You are all set, you can now connect with an XMPP client !


Let us know what you think 💬


3 thoughts on “How to use ejabberd as an Elixir application dependency

Leave a Comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.