How to Install Openfire XMPP Server and Use it with Smack Android App From Emulator

Openfire is an widely used XMPP Server that is written in the Java programming language. It boasts  an intuitive and easy to use dashboard where your setup is just interacting with the UI and you can get complex setups done very easily. In this tutorial we are focusing on installing Openfire for use with a Smack based Android App on the local machine were Android Studio is installed. It is in response to my students from the course Build an Android Chat App from Scratch using XMPP and Smack, who happen to need this in big numbers. Let’s get started.

Installing Openfire

Openfire and Smack are both developed by an organization called Ignite Realtime, so let’s go to their web page and grab our latest copy of Openfire. Find the Openfire listing and click download for your platform of choice. Being written in Java, Openfire can run on all major desktop platforms : Windows, Mac , Linux. This tutorial is going to be based on Windows, but the same steps should apply to Linux and Mac as well. Start the installer you downloaded and you’ll see a window to let you choose your language

I choose English and move on . Confirm that you want to install the package in the following window :

In the next screens, accept the agreement and chose where you want your packages installed.

Hit next and choose whether you want the shortcuts and the install progress dialog will show up

And when the install is done, you’ll see a confirmation dialog, prompting you to start the Openfire service to start using it right away.

Hit finish and your default browser will open the openfire welcome page where you choose the language of your install.

Choose your preferred language and hit continue. You’ll be given a UI where you should set up your domain name.

Because we want to use our server locally, we set our domain name to be localhost. If we had set it up to be something else, we would have to go mess with the host files to resolve whatever we put in here to a recognizable network address. Just leave in localhost, it is good enough for our local use purposes. Leave the rest to defaults and hit continue. You are then given a choice for the database you want to use.

We choose Embedded database here for convenience, but you can choose Standard Database Connection and set up an external database if you need that. Hit continue and choose a setting for where you want your user info to be saved.

We choose Default in this case. Hit continue, and you are presented a UI where you need to provide server admin info, the email isn’t really important here but you should REMEMBER the password that you put in here .

If you forget the password, you won’t be able to login and see the dashboard for managing your server. Put in your details and hit continue and you’ll see a screen saying that your installation is complete.

Some people haven’t been able to login into the dashboard after a successful install like we have here. An easy fix for that is to stop the openfire service and start it again. Follow the appropriate steps to stop and start openfire on your favorite operating sytem. In this case we are on windows, we are simply going to go in the start menu, find openfire and start a program that allows us to manage it.

Click on the yellow icon that says “Openfire Server” and you are going to see a dialog as shown below

You can see that the openfire service is running. We started it right after our install. Stop it and start it again. and then hit Launch Admin to see the web interface where you can login.

Type in a username of “admin” and the password you specified during  your install. Hit login and you should be in your dashboard.

There is a lot we can do in this dashboard, you can take some time to look around and familiarize yourself with the dashboard if this is your first time with it. For now, click on the Users/Groups tab and hit the create New user button.

Put in the credentials for your user.Remember that because out domain name is “localhost”, our JIDs are going to look like “gakwaya@localhost”. Hit the create user button and you’ll see a screen confirming your success.

We just created one user, but nothing prevents you from creating as many as you want! Openfire is now installed on our local machine. Now we want to use an XMPP Client installed in our virtual device to access it. That’s what we’re doing next.

Communicating from Virtual Device to Host Computer

There is actually an official way to do network communication from Virtual device to Host Computer from the android docs. In the virtual device you push your traffic to an IP address of 10.0.22 and it is forwarded to the address of localhost in the host computer. From our XMPP client, we basically need to specify a HARD CODED host address of 10.0.2.2. This way we can do normal XMPP things with JIDs like “gakwaya@localhost” .

Security : SSL Certificates

Because our server doesn’t have a public facing domain name, and is running locally, we can’t set up an SSL certificate for it. So we will need to disable security when connecting to our server as shown below

.setDebuggerEnabled(true)
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled) // Should make required on Public Server with SSL Configured
.setCompressionEnabled(true).build();

Adapting the Source Code of  your XMPP client

When our XMPP client is talking to our local server, we need to do two things. One is address the XMPP traffic to an IP address of 10.0.2.2 and the other is to use a domain name of “localhost”. That’s what we set up our server to use. We do all that when connecting to the server with the code snippet below :

XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder()

        //.setXmppDomain(mServiceName)//Public Internet server
        //.setHost(mServiceName) //Public Internet Server

        .setHostAddress(InetAddress.getByName("10.0.2.2"))  //Local Server
        .setXmppDomain(JidCreate.domainBareFrom("localhost"))  //Local Server
        .setResource("Rooster+")

 

Notice that I left in the code you should use when your client is talking to a public server. When you need that,m just comment out the //Local server flagged lines and uncomment the //Public Internet Server ones. You should also note that we have a hard coded XMPP domain because the user doesn’t have the flexibility of using other servers. Switching from local to public is a thing you can control only in code in this case. A screenshot of my setup is shown below in case it helps clarify things.

This should give  you a local environment to test your Smack Android XMPP client against. If you have any questions or queries, please do shoot me in the comments below. I hope this has been informative to you and thanks for reading.

Posted in Servers, xmpp and tagged , , , , , , .

Daniel Gakwaya loves computer Hardware/Software.He is a Software Engineer at BLIKOON and lead developer of bliboard-The whiteboard system currently marketed by the company.He is known to hack around on any piece of tech that happens to pick his interest. More on his tech endeavors here
Follow him on Twitter

5 Comments

  1. Hello I bought your tutorial on udemy and gone through it. I have tried to implement the solution on my local machine using it as the server but it gives the error “Something went wrong while connecting ,make sure the credentials are right and try again”

    • Hi Andrew,
      I don’t have enough info to know what is wrong with your setup but you can try to connect to your server using another xmpp client like Xabber. If you find it cumbersome to install and android client on a virtual device just to test your environment, you can try using a desktop client like gajim (https://gajim.org/). They all equally work well to make sure your clients can talk to your server. Let me know how it turns out.

  2. Hi, Gakwaya I’m trying to connect to my localhost server but its not connecting, below is the error message

    at android.view.ViewRootImpl.(ViewRootImpl.java:511)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:346)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
    at android.app.Dialog.show(Dialog.java:329)
    at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:958)
    at com.blikoon.roosterplus.ChatListActivity.requestBatteryOptimizationsFavor(ChatListActivity.java:145)
    at com.blikoon.roosterplus.ChatListActivity.onCreate(ChatListActivity.java:102)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
    09-03 05:46:31.148 5558-5558/com.blikoon.roosterplus I/AssistStructure: Flattened final assist data: 3496 bytes, containing 1 windows, 12 views
    09-03 05:46:35.856 5558-5558/com.blikoon.roosterplus D/RoosterPlus: saveCredentialsAndLogin() called.
    09-03 05:46:36.543 5558-5558/com.blikoon.roosterplus D/RoosterConService: Service Start() function called. mActive :false
    09-03 05:46:36.545 5558-5558/com.blikoon.roosterplus I/Choreographer: Skipped 40 frames! The application may be doing too much work on its main thread.
    09-03 05:46:36.545 5558-5587/com.blikoon.roosterplus D/RoosterConService: initConnection()
    09-03 05:46:36.554 5558-5587/com.blikoon.roosterplus D/RoosterConnection: RoosterConnection Constructor called.
    09-03 05:46:36.564 5558-5579/com.blikoon.roosterplus I/OpenGLRenderer: Davey! duration=748ms; Flags=1, IntendedVsync=951031575171, Vsync=951064908503, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=951080838100, AnimationStart=951080907200, PerformTraversalsStart=951081557020, DrawStart=951116214810, SyncQueued=951743045040, SyncStart=951745136460, IssueDrawCommandsStart=951745335060, SwapBuffers=951770358490, FrameCompleted=951782197170, DequeueBufferDuration=598000, QueueBufferDuration=1267000,
    09-03 05:46:36.598 5558-5579/com.blikoon.roosterplus I/OpenGLRenderer: Davey! duration=716ms; Flags=0, IntendedVsync=951085986707, Vsync=951752653347, OldestInputEvent=9223372036854775807, NewestInputEvent=0, HandleInputStart=951763412130, AnimationStart=951763475950, PerformTraversalsStart=951767307260, DrawStart=951768032220, SyncQueued=951768305590, SyncStart=951782373500, IssueDrawCommandsStart=951782691050, SwapBuffers=951801511200, FrameCompleted=951816324830, DequeueBufferDuration=52000, QueueBufferDuration=240000,
    09-03 05:46:37.191 5558-5587/com.blikoon.roosterplus D/RoosterConnection: Calling connect()
    09-03 05:47:07.193 5558-5588/com.blikoon.roosterplus W/PingManager: XMPPConnection was not authenticated
    09-03 05:47:07.203 5558-5587/com.blikoon.roosterplus D/RoosterConService: Something went wrong while connecting ,make sure the credentials are right and try again
    09-03 05:47:07.205 5558-5558/com.blikoon.roosterplus D/RoosterPlus: Got Connection Error in login activity
    09-03 05:47:07.206 5558-5587/com.blikoon.roosterplus D/RoosterConService: Sent the broadcast for connection Error from service
    09-03 05:47:07.207 5558-5587/com.blikoon.roosterplus D/RoosterConService: Logged in state :falsecalling stopself()
    09-03 05:47:07.216 5558-5587/com.blikoon.roosterplus W/System.err: org.jivesoftware.smack.SmackException$ConnectionException: The following addresses failed: ‘10.0.2.2:5222’ failed because: /10.0.2.2 exception: java.net.SocketTimeoutException: failed to connect to /10.0.2.2 (port 5222) from /192.168.232.2 (port 54330) after 30000ms
    at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectUsingConfiguration(XMPPTCPConnection.java:619)
    at org.jivesoftware.smack.tcp.XMPPTCPConnection.connectInternal(XMPPTCPConnection.java:902)
    at org.jivesoftware.smack.AbstractXMPPConnection.connect(AbstractXMPPConnection.java:380)
    at com.blikoon.roosterplus.xmpp.RoosterConnection.connect(RoosterConnection.java:243)
    at com.blikoon.roosterplus.xmpp.RoosterConnectionService.initConnection(RoosterConnectionService.java:51)
    at com.blikoon.roosterplus.xmpp.RoosterConnectionService.access$100(RoosterConnectionService.java:24)
    at com.blikoon.roosterplus.xmpp.RoosterConnectionService$1.run(RoosterConnectionService.java:120)
    at java.lang.Thread.run(Thread.java:764)
    09-03 05:47:07.306 5558-5558/com.blikoon.roosterplus D/RoosterConService: stop()
    09-03 05:47:07.307 5558-5587/com.blikoon.roosterplus D/RoosterConnection: Disconnecting from server localhost
    09-03 05:47:07.310 5558-5579/com.blikoon.roosterplus D/EGL_emulation: eglMakeCurrent: 0xe0385300: ver 2 0 (tinfo 0xe0383650)
    09-03 05:47:07.337 5558-5579/com.blikoon.roosterplus D/EGL_emulation: eglMakeCurrent: 0xe0385300: ver 2 0 (tinfo 0xe0383650)

    • I would try and connect with a desktop client like gajim( https://gajim.org/) as stated in a previous response and make sure whether the problem is on the server, or the client. Please download gajim ( or any other xmpp desktop client form your host platform) and try to connect with a JID with format ( username@localhost ) , for a user you defined in your Openfire server and use the password you defined there.If that works well , you’ll know the problems is in the android xmpp client, if it doesn’t then it’s the server that is problematic.
      Hope this helps.

  3. thank you very much sir for this wonderful tutorial. i have downloaded and configured my xmpp server with openfire, i have also tested it with gajim and it connected but I’m still unable to send messages from my android app, i am using your code from your first tutorial to connect to my xmpp server but whenever i click on send message button it returns the “Client not connected to server ,Message not sent!” message. please sir i really need your help because i have been stock here for weeks now and i don’t know were else to look.i would really appreciate your help. thanks in advance

Leave a Reply

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

*

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