Monthly Archives: April 2013

Setting up Openfire + BOSH + Strophe

The following post explains how to install an XMPP/Jabber server on a linux machine and how to use Strophe.js as the XMPP client. The following applications and packages were used:

  • Ubuntu Server 12.04
  • Apache 2
  • Openfire 3.8.1
  • Strophe 1.0.2

1.- Installing Openfire

Openfire is an IM server that uses XMPP protocol (Ejabberd is another server, but after playing around with both applications I found Openfire easier to setup). As a prerequisite, you must have OpenJDK in you machine. If you don’t have it, you can installed it running the following command:

sudo apt-get install openjdk-7-jre

Once OpenJDK is properly configured. You must download the Openfire and untar it with the following command:

tar -xvzf openfire_3_8_1.tar.gz

Then, move the files to /opt:

mv openfire /opt/

And finally, add a symbolic link for the daemon script to /etc/init.d so that you can start the daemon with a call to service:

ln -s /opt/openfire/bin/openfire /etc/init.d/

Openfire should be installed now. You can start the service with the following command:

service openfire start

If no errors were displayed, open the following URL in your browser:

http://localhost:9090

2.- Configuring Openfire

If everything went OK, you should see the following wizard:

Click ‘Continue’ and configure your domain and ports for administration (You can keep the defaults and use localhost as the domain name).

The next screen will ask you for the database configuration. You can use an external DB, but it is easier to use the built-in option (Embedded database):

User profiles can be stored in the server Database, LDAP or Clearspace. Keep the default option (Default):

Finally, enter the details for the admin account:

Restart the server:

service openfire stop service openfire start

Go back to http://localhost:9090 and you should see a login form (the username is “admin” and the password is the one that you set during the setup wizard).

3.- Testing Openfire

In order to test openfire, you must create a user id. To do this, log in to the administrative console, click Users and Create New User:

Once the user is created, you can test your Openfire server using an XMPP Client (Pidgin, Gajim, etc) and configuring the URL and port of your server. To configure Pidgin, click on  Accounts -> Add and select as protocol: XMPP.  the username is the one that was created in the previous step and the domain is locahost. Then, click on Advanced and make sure that “connection security” is set to “use encryption if available” and port is “5222”. If everything is OK, you should be able to connect:

4.- Configuring BOSH

BOSH stands for Bidirectional-streams Over Synchronous HTTP. Basically, it is used to transport XMPP stanzas over HTTP. So, if you are planning to use your server with an XMPP Web client, you must configure BOSH (or websockets..). In order to enable BOSH in Openfire, log in to the admin console and click on Server->Server Settings->HTTP Binding. Http-Bind should be configured with the following parameters:

 

Save the changes and run a test on Pidgin (Change your account settings: Modify Account -> Advanced. Connect port: 7070, Connect server: localhost).

 

5.- Connecting with Strophe.js

 

Strophe is an XMPP library for Javascript which allows you to connect from a Web browser to an XMPP server. Download the latest version of this library and unzip it in your workspace. This package contains a couple of examples that can be used as reference. In my case, I will use examples/basic.html. If you open it in your browser you should see the following form:

 

Before trying to connect. You must update the file examples/basic.js and change the BOSH URL as follows:

var BOSH_SERVICE = 'http://localhost:7070';

Now reload the page and try to connect using the username/password that was created previously. The following error might be displayed:

This error happens because you are trying to make a cross-domain request.  You could fix it using the crossdomain example found in the same folder or you could tweak your apache server.

6.- Enabling Proxy_Http in Apache 2

In order to fix the previous issue. You must set a proxy to redirect the “http-bind” request. To do that, you need to enable the proxy_http module in apache with the following command:

sudo a2enmod proxy_http

Then, you need to update the rules in your Site’s config file. You can update the “default” file found in /etc/apache2/sites-avaiable/ folder but its better to keep these changes in a separate file. So, create a new file under /etc/apache2/sites-available/ with the name ‘jabberserver’:

sudo nano jabberserver

And add the following rules:

<VirtualHost *:80>

DocumentRoot /var/www/jabber

ServerName jabber.local

#requests sent to /http-bind will be redirected to jabber.local:7070

ProxyPass /http-bind http://jabber.local:7070/http-bind/

ProxyPassReverse /http-bind http://jabber.local:7070/http-bind/

</VirtualHost>

DocumentRoot and ServerName are optional parameters. I decided to use them to have a different name for the XMPP server. If you do the same, remember to update your /etc/hosts file.

Restart your apache server (sudo service apache2 restart). And update the basic.js file one more time:

var BOSH_SERVICE = 'http://localhost/http-bind'

Go back to your browser and if you try to log in again you should see the following message:

Everything went fine this time. If you want to extend the Web client functionality, you might want to take a look at JQuery UI Chat box. or Penguin.

Advertisements