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.
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.