September 28, 2010 36 Comments
There are few ways to develop ‘server’ type XMPP applications; by server I mean a bot or an application connecting to a XMPP server and providing some sort of service like weather, fortunes, die roll, magic 8 ball or even act as mediator for games like Diplomacy and chess. For the sake of this blog, lets call this a xmpp service. The following are 3 possible ways you can implement xmpp service
- As a ‘regular’ XMPP user – what I mean by this is that the xmpp service is like a regular user. The service would create an account with say jabber.org, logs in and waits messages. The XMPP service can also be part of a real user’s (human) login. When a person logs in, the service starts. XMPP clients like Spark provides a plugin architecture (sparkplug) to develop such services. This method is by far the easiest method to develop the services; the disadvantage is that your service has a limited reach because in order to use the xmpp service other users must discover the JID and must also explicitly add that JID into their roster before you can do anything with it.
- XMPP Server plugins – another way of developing these services is to make them part of the server. Most servers have some sort of a plugin architecture; see Openfire’s as an example. The advantage of this approach is that the service does not need to in a users roster for the user to use it. And discovering the service boils down to sending a disco#item packet to the server. For example sending a disco#item to jabber.org returns the following <item jid=’conference.jabber.org’ name=’Multi-User Chat’/> in part of the reply. We can now start a conference with that JID. The disadvantage of this approach is that the service that you have developed may not be portable across to other servers.
- XMPP components – the XMPP Foundation has defined a standard protocol for services to connect to XMPP servers. This is defined in Jabber Component Protocol (XEP-0114). The way this works is that the service will communicate with a standard XMPP server using a predefine mechanism for identification and authentication. Once the handshake is successful, the XMPP component effectively becomes part of the server and behaves like a server plugin in that it can be discoverable using the standard disco#item as well as disco#info mechanism. The component runs outside (in a separate process) of the XMPP server. So you can shutdown the service without affecting the server. An added advantage is that because XEP-0114 is a standard, in theory , you can connect the service to different XMPP servers.
- Start by downloading the Whack source from the repository as shown here. The reason why you download the source instead of just the binaries is because when you eventually run your XMPP service, the Whack runtime will need a few more libraries that are not present in the binary download. So its better to bite the bullet and get the source which contains all the dependencies. There is also 2 sample services which you can reference. They are actually the same service (weather) but implemented differently
- Build it either with Ant or Maven. For Ant the whack.jar will be target directory and for Maven it’ll be in sample/weatherabstract/build/lib/merge/whack.jar.
- Now set your CLASSPATH to include whack.jar and all the JARs in build/lib/dist and build/lib/merge and other libraries that you may need for your application. You can exclude the Jetty JARs if you want to run your component as a standalone Java application. Tip: if you are using JavaSE 6 or later, you can use the wildcard character like so
The second step is to give a subdomain name and a secret key that is shared between the XMPP server and the service. Both the subdomain and secret key are setup on the XMPP server. A subdomain is a name that will be used to identify your service; eg in the example above, jabber.org MUC’s JID is conference.jabber.org; the subdomain here is conference and jabber.org is the domain. So if we call our subdomain rot13 because we are going to provide a ROT13 service, then the JID will be rot13.jabber.org (assuming its on jabber.org).
The following diagram shows how to set these 2 values if you are using Openfire.
- Login to the admin console http://localhost:9090
- Click on Server Settings then External Components
- Type in the subdomain name and the secret key and click on Add Component to save the setting
In my next blog we will look at developing the XMPP service.