How to Build One to One Chat App Using Ejabberd & Erlang from Scratch?

In this article, the development of one to one chat app from scratch is discussed in detail. The technical stack involved in WhatsApp like chat app are Erlang (Language), Ejabberd (Framework), XMPP protocol, MySQL (Database). The following steps will help you to develop custom mobile chat application in a hassle-free manner.

The parameters involved to develop one on one chat are as follows

  • Erlang
  • MySQL
  • Ejabberd
  • PSi+
  • One to One chat

Here is the procedure to develop on to one chat app.

Install and Setup Erlang on Ubuntu

Erlang is a functional programming language and open source used for high scalability and availability for system requirements. Erlang shows the concept “Write Once and Run Forever”. Erlang / OTP (Open Telecom Platform) are interchangeable consists of a collection of libraries, design principles like (soft real-time, distributed, fault tolerance etc) that are written in Erlang language to support the high run-time system and scalability.

The following steps are the installation procedure of Erlang in the Ubuntu server:

Step 1: Download the Erlang in the Ubuntu Server

  • In Linux server, download the Erlang source file using the below link. Here Erlang version of 18.0 is used and this extends the updates regularly.
  • Thewgetcommandis used to download the Erlang source files.
$ wget http://erlang.org/download/otp_src_18.0.tar.gz

Step 2: Extract the Erlang tar File.

Extract the downloaded tar file using the command.

 $ tar -xvzf otp_src_18.0.tar.gz

Step 3: Adding Dependencies in the Command

Provide the required dependencies in the single library function to perform a specific task. Below listed are the dependencies that will do a certain task.

 $ sudo apt-get install gcc make build-essential libncurses5-dev openssl libssl-dev fop xsltproc unixodbc-dev libwxbase3.0 libwxgtk3.0-dev libqt4-opengl-dev autoconf

Step 4: Start compilation and Install the Erlang

To compile the source code use “make” command that starts to execute the file and finally install the Erlang using “make install” on the Linux server. The “configure” command will help you to verify the dependencies.

 $ sudo ./configure
 $ sudo make
 $ sudo make install

process to build chat app

Install MYSQL and Import Ejabberd DB

Install the MySQL database using the below command:

 $ sudo apt-get install mysql-server-5.6

1. While the installation of MySQL Server packages, you will get a screen tosetupthe MySQL root password on your terminal. Provide the MySQL user root password.

2. Confirm once again with the popup.

3. After the installation of MySQL server, it runs the MySQL service automatically. Once it is done, you can log in the MySQL Server with user root and password that is already defined.

To log in to MySQL Server, use the below-given command. It tells to enter the MySQL root password which you have already set during the installation of MySQL server.

4. To check MySQL server status run the following command

 $ sudo service mysql status

5. To create the database, use the below command

 $ mysql -u root -p

Pop-up shows to enter the MySQL password, and then createdatabasefor one-one chat users

 $ mysql> create database chat;
 $ mysql > \q;

Copy the Ejabberd MySQL database structure from the below link and save to mysql.sql file

https://github.com/processone/ejabberd/blob/16.08/sql/mysql.sql

Import Ejabberd database structure to chat database.

 $ mysql -u root -p chat < mysql.sql

Read : How to Build Chat App like WhatsApp?

Start and Install Ejabberd XMPP Server on Ubuntu

Ejabberd is an XMPP (Extensible Messaging Presence Protocol) programming server written using Erlang programming language. One of the most popular open sources and free software. Ejabberd runs on multiple operating systems like Linux, Mac, Unix, Windows etc. XMPP is the backbone that allows sending real-time message system, online presence indicators, XML routing features and more.

The installation of Ejabberd XMPP server on Ubuntu 14.04/16.04 in detail.

Step 1: Download the Ejabberd in the Ubuntu Server

  • In Linux server, download the ejabberd source file using the below link. Here Ejabberd version of 16.08 is used and this extends the updates regularly.
  • The wget command is used to download the Erlang source files.
 $wget https://www.processone.net/downloads/ejabberd/16.08/ejabberd-16.08.tgz

Step 2: Extract the erlang tar file.

Extract the downloaded tar file using the command.

 $ tar -xvzf ejabberd-16.08.tgz

Step 3: Adding Dependencies in the Command

Provide the required dependencies in the single library function to perform a specific task. Below listed are the dependencies that will do a certain task.

 $ sudo apt-get install rebar libyaml-dev libexpat1-dev

Step 4: Fix and Verify the Issue

autogen.sh used for compilation, verification of the functionality and overcome the initial issues.


nbsp;./autogen

Step 5: Start execution and Install the Ejabberd

To compile the source code use “make” command that starts to execute the file and finally install the Erlang using “make install” on the Linux server. The configure command will help you to verify the dependencies.

 $ sudo ./configure
 $ sudo make
 $ sudo make install

Step 6: Open the Ejabberd configuration file

Using the command, open the Ejabberd file to set the database.

 $ sudo nano /etc/ejabberd/ejabberd.yml

Provide MySQL database IP, Database name, username, the password for the confirmation.

 auth_method: sql
 sql_type: mysql
 sql_server: “localhost″ (Mysql Database IP)
 sql_database: “chat” (Database name)
 sql_username: “root” (Database user name)
 sql_password: “password” (Database password)

Finally, save the file /etc/ejabberd/ejabberd.yml

Step 7: Run the Ejabberd

Once the changes are done, run the Ejabberd which in turn helps to refresh the database.

 $ sudo ejabberdctl start

Step 8: Check the Status using Local Host

To verify and to check the status of the ejabberd installation use the below command.

 $ sudo ejabberdctl status

The node Ejabberd at Localhost starts with the status command and shows the ejabberd 0.0 is running

Using a localhost IP “http://localhost:5222” the browser displays the text that is similar to the XML

Register the users in Ejabberd using the below displayed code:-

 $ ejabberdctl register user1 localhost password
 $ ejabberdctl register user2 localhost password

Read : How to Build Chat App like WhatsApp?

Chat API & Messsaging SDK for in-app communication

Installation of PSi+ Software for the One-One chat

Using Ubuntu software center, download the PSi+ and install the software.

Follow the guidelines and screenshots to register the users and create a 1–1 chat application:
1. Click the bottom icon to setup & add the account for “User1”.


2. Go to Account setup, enter “User1” and then click “add button”.


3. Provide XMPP address as “user1@localhost” or server IP and enter the password as given on the Ejabberd registration database.


4. A popup displays, just close the window.


5. Repeat the steps from 1 to 3, to register the “User2”.

6. In “User1”, do right click to enable the status “online” and repeat the same for “User2”.


7. To add the contact in “User2”, do right-click and click the “Add Contact” button. Then enter the XMPP address of “User1” (User1@localhost)


8. Repeat the above step for adding the “User2” in the “User1”.



9. Click “add/auth” and then close the popup.



10. Double click the “User1”, a popup will display. Now enter the required message to send. The message will received by “User2” and repeat the same procedure for “User2”.




11. To get the XMPP protocol format, right click the “User1” then click Account -> XML console -> Enable & Filter option.


Follow the above steps and build a simple messaging app from scratch. For further assistance, reach me through the comment section.

Rajeshwari

Rajeshwari is a skilled digital marketer, passionate about SEO and exploring the latest trends and tech innovations in communication and Chat APIs. With a keen eye for detail, she helps brands improve their online visibility, and she is always eager to stay ahead in the evolving digital landscape.

10 Comments

    • Two major reasons:

      i) No straightforward API available for sending push notification from one device to another (without the use of a separate backend).

      ii) No inbuilt encryption, you would have to implement your own encryption for chatting.

      Reply
    • List of libraries that we’ll use to build the chat app:

      firebase – the client-side libraries for applications that use Firebase services
      react – a JavaScript library for building user interfaces
      react-native – a framework for building native apps with React
      redux ‒ a state management library for JavaScript applications
      eslint ‒ a linting utility for JavaScript applications
      relative-date – JavaScript module for outputting relative dates

      Reply
  1. The tutorial which is more helpful for the new developer to build a one to one chat app. Thanks for sharing the article!

    Reply
  2. Hey Alex,
    Nice post. I am developing a one to one chat application in Android. This blog helped me and also clarified the doubts.
    Looking for further updates.

    Reply
  3. Other members upon accepting the request can have private chat with them. … We’ll be making use of AngularJS 1 for this tutorial. Download the AngularJS …. There are many ways I could improve this application.

    Reply

Leave a Comment