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 .formatter.exs
    * creating .gitignore
    * creating mix.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. Go to your new app directory:
    cd ejapp
    
  3. Add the ejabberd package as a dependency in your mix.exs file:
    defmodule Ejapp.Mixfile do
    ...
      defp deps do
        [{:ejabberd, "~> 23.10"}]
      end
    end
    
  4. Get the default ejabberd configuration file:
    mkdir config; wget https://raw.githubusercontent.com/processone/ejabberd/master/ejabberd.yml.example -O config/ejabberd.yml
    
  5. Setup some system options in a new file called config/config.exs:
    import Config
    
    config :ejabberd,
      file: "config/ejabberd.yml",
      log_path: 'logs/ejabberd.log'
    
    config :mnesia,
      dir: 'database/'
    
  6. Get the source code of all dependencies and compile them:
    mix do deps.get, compile
    
  7. Start your app, ejabberd will be started as a dependency:
    iex -S mix
    
  8. Register an account from Elixir console, later check it was registered:
    :ejabberd_auth.try_register("test", "localhost", "passw0rd")
    :ejabberd_auth.get_users()
    
  9. 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 Reply to Igor Khomenko Cancel Reply


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