Trying to create a chat app (like WA ) using Ejabberd?

Hello Everyone,

I am trying to create a Chat app like whatsapp and i have bascially read every other discussion on the forum regarding this topic and decided to use ejabberd (because i want to store messages when the reciever is offline , and security was also an issue.). I have also watched few implementations of Ejabberd in Phoenix Application.

Though i still have few doubts, i am already halfway through my application (its an application with many features chat is just a part of it) and i have my own Auth system in application which is working, my question is do i still need to use something like ejabberd.auth() and get JID from it to initiate my Chat stuff and from where can anyone give me some resources to understand the docs.

There is a lot of stuff and it’s quite overwhelming, also if anyone can give me some github link or anything if this stuff has already been implemented by someone else so that i can have a look.

Any help would be appreciated. Thanks for listening.

It seems like you have a lot to share; why not share whatever you have first?

Alright let’s start with the simple stuff

  1. My application is what you could say is like Reddit + WA. I am almost done with the reddit part and decided to look into how am i going to implement the WA one.

  2. Initially i thought i could just work with Websockets but they don’t provide all the services i needed. Like media transfers and how am i going to store messages for offline users and then deliver it.

  3. Thats why i researched more and decided to go with Ejabberd.

The difficult stuff.

  1. I have seen little bit of documentation and few implementation of Ejabberd in elixir but still not able to understand how am i going to implement one-to-one chat and multi user chat and also Video/Voice calls.

  2. Researched a little bit more then i came to know about JID (Jabber ID) and then i realized (i might be wrong here) that Ejabberd basically calls functions like
    ejabberdctl send_message chat 192.168.33.50 user1@192.168.33.50 “subject” “message from server”

and i guess here “user1@192.168.33.50” is acting like the JID.

The confusion part:-

  1. I already have implemented basic Auth system with email and password but for chat functionality i think i need to generate somekind of JID as well i guess?

  2. Even after i generate JID lets say how do i implement these one-to-one and multi user chat in my application because Ejabberd docs are some what confusing to me.

This basically sums up my problem.
Now few additional questions that i have if anyone can answer them
Q1) Should i use MNESIA or Postgresql as Database? (For my reddit part i am using PostgreSQL)

Q2) Do we generate JID again and again when user connects to the internet or it is generated only once when user first registers?

You ask a lot of questions like “Should I …” These questions are generally unanswerable because we do not know your situation as good as you. If you can re-phrase your question into non-subjective and isolated question you can greatly increase your chance of getting help, and the helpers and on-lookers, etc can benefit from the discussion as well.

Q1: If you store the messages only until they are delivered and then delete them - giving the client the responsibility to store them (like WA does it) then I would say mnesia is good choice. If you want to store them on the server potentially indefinitely and make the client query them each time (like FB) then I would say Postgres is a better choice. But it is difficult to say and depends on a lot of factors.

Q2: A JID needs to be generated only once per user. But you don’t even need to as you can reuse your existing auth. All you need to do is to store it in a way Ejabberd can access it. Then, with a minimal amount of logic, you can make your client just construct the JID for authentication with Ejabberd und Ejabberd check the credentials. A JID has the following form: [username]@[serverdomain]/[resource]. Make Ejabberd aware of the domain and set a random constant resource.

At the end of the day, Ejabberd implements the XMPP protocol. So it would certainly not be a bad idea to take a look at the following book if you haven’t already: https://www.amazon.com/XMPP-Definitive-Real-Time-Applications-Technologies/dp/059652126X

Thanks for all the insights. Though i am facing a new problem now. I have been trying to install ejabberd in my phoenix app i am getting this error.

I have tried to downgrade ejabberd but it does not seem to be working. This is my mix.exs

defmodule ChatTest.MixProject do
  use Mix.Project

  def project do
    [
      app: :chat_test,
      version: "0.1.0",
      elixir: "~> 1.13.4",
      elixirc_paths: elixirc_paths(Mix.env()),
      compilers: [:gettext] ++ Mix.compilers(),
      start_permanent: Mix.env() == :prod,
      aliases: aliases(),
      deps: deps()
    ]
  end

  # Configuration for the OTP application.
  #
  # Type `mix help compile.app` for more information.
  def application do
    [
      mod: {ChatTest.Application, []},
      extra_applications: [:logger, :runtime_tools, :ejabberd]
    ]
  end

  # Specifies which paths to compile per environment.
  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(_), do: ["lib"]

  # Specifies your project dependencies.
  #
  # Type `mix help deps` for examples and options.
  defp deps do
    [
      {:phoenix, "~> 1.6.13"},
      {:phoenix_ecto, "~> 4.4"},
      {:ecto_sql, "~> 3.6"},
      {:postgrex, ">= 0.0.0"},
      {:phoenix_live_dashboard, "~> 0.6"},
      {:swoosh, "~> 1.3"},
      {:telemetry_metrics, "~> 0.6"},
      {:telemetry_poller, "~> 1.0"},
      {:gettext, "~> 0.18"},
      {:jason, "~> 1.2"},
      {:plug_cowboy, "~> 2.5"},
      {:absinthe, "~> 1.7"},
      {:absinthe_plug, "~> 1.5"},      
      {:ejabberd, "~> 22.5", github: "processone/ejabberd"}
    ]
  end

  # Aliases are shortcuts or tasks specific to the current project.
  # For example, to install project dependencies and perform other setup tasks, run:
  #
  #     $ mix setup
  #
  # See the documentation for `Mix` for more info on aliases.
  defp aliases do
    [
      setup: ["deps.get", "ecto.setup"],
      "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
      "ecto.reset": ["ecto.drop", "ecto.setup"],
      test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"]
    ]
  end
end

Do you have any idea how can i solve this?

Try to remove the version number.

1 Like

Update: The app has compiled but when i start my phoenix server i am getting this error.

When i commented mod_irc in ejabberd.yml. I got this error.

Just for the context i was referring to this guide
Embedding Ejabberd in your Phoenix App

New updated. I just had to make few changes to ejabberd.yml it seems to be working now. my phoenix app is also running.

2 Likes