Build a chat application in Android using XMPP

Extensible Messaging and Presence Protocol (XMPP) is a communications protocol for message-oriented middleware based on XML (Extensible Markup Language).[1] It enables the near-real-time exchange of structured yet extensible data between any two or more network entities.

How the Whatapp Works!

WhatsApp is a real-time messaging application, which makes use of Ejabberd (XMPP) server. This server facilitates instant message transfer between two or many users at any given second.

ERLANG is the programming language used by WhatsApp. Ejabberd is an open source Jabber server written in Erlang language. It was preferred by the techies of WhatsApp as it was suitable for accommodating huge communication system with high sustainability.

Building a chat application 

  1. Openfire server installation (XMPP SERVER)
  2. Openfire server configuration
  3. XMPP IM android client

 

Openfire server installation (XMPP SERVER)

The server we will use is Openfire, a real-time-collaboration (RTC) server provided by Ignite Realtime under the Open Source GPL. The protocol that Openfire uses is Extensible Messaging and Presence Protocol (XMPP), also known as Jabber. Actually, Google Talk uses XMPP for its instant messaging function.

Download the Latest build of Openfire from here

Openfire is a Java based server and does not require administrator privileges in order to start. The .tar.gz build does not contain a bundled Java runtime (JRE). Therefore, a JDK or JRE 1.5.0 (Java 5) or later has to be installed on your system. If not, follow one of the countless online tutorials on how to install it. Note that Java can also be installed without administrator privileges.

follow the commands in terminal:

  • tar -xvzf openfire_3_6_4.tar.gz
  • cd openfire/bin/
  • chmod +x openfire
  • ./openfire status
  • ./openfire start

You will receive the following response:

nohup: appending output to `nohup.out’

 

The server binds to port 9090 by default. So let’s visit the administration console by visiting the following URL (“localhost” is the linux machine hostname):

http://localhost:9090/

If everything worked, you should see the following screen:

Choose your language (I will go with English) and hit “Continue”. In the following screen, the server settings are configured.

The “Domain” option should be automatically populated. Choose the ports for Admin Console. Actually, the default options should be just fine.

Next step is to configure the database settings. Openfire can use various external databases, but also an embedded one.

For reasons of simplicity, I chose to go with the embedded HSQLDB option, but if you wish to connect with another database, there is a guide in the file “documentation/database.html”. Note that HQSLDB will use a folder named “embedded-db”, so make sure this does not get deleted.

Next we have to complete the profile settings

That’s it. If the installation was successful.

The first login attempt fails. You need to restart the Openfire server!

Yes sounds a little idiotic, but this is something that a lot of people have faced. However, restarting Openfire fixed the problem (at least in my case).

So, first stop the server:

  • ./openfire stop

and then start it again:

  • ./openfire start
(Note that the start up procedure takes a little time, be patient).

Browse again to the admin console page (http://localhost:9090/) and login to your account.

Openfire server configuration

Login to the admin console and choose “Users/Groups”. There you should see the existing users under “User Summary”. Only “admin” is currently existent, so choose “Create New User” from the left panel.

Fill in the user’s information as shown above and make sure the “Is Administrator” option is not selected. Hit the “Create & Create Another” button to create the current user and repeat the procedure to create a second one. I created users “testuser1” and “testuser2”.

Now let’s use an XMPP client to actually connect to the server. You can try the webclient for xmpp from here

XMPP IM android client

We can you the above xmpp server to build the android client. Or you can use any xmpp client also with the above server. Here am giving two xmpp client which is working perfectly fine.

clone the xmpp client 1 from here or you can clone the the other client form here

Then open your andorid studio.

  • import the client app to the studio
  • then configure the xmpp server with the client
  1. Create a connection to the igniterealtime.org XMPP server.
  2. XMPPConnection connection = new XMPPConnection(“myxmppserver.com”);
  3. Connect to the server  connection.connect();
  4. Most servers require you to login before performing other tasks.
  5. connection.login(“myuser”, “mypass”);
  • the build and run the project
  • make sure that the android phones you are using are on same network to access the xmpp server

screenshots

You can enable the media sending feature in the xmpp server for sending media files over the client, and you can add more plugin to the server based on the requirement

 

 

 

magento-banner

Leave a Reply

Your email address will not be published. Required fields are marked *