The Extensible Messaging and Presence Protocol (XMPP) is a near realtime instant messaging protocol, and is used in instant messaging like Jabber.
XMPP is extremely flexible and extensible protocol. It has been used in instant messaging, presence information, VOIP applications and others.
In this series of articles, I will show you how to use XMPP to 'peer-to-peer' enable your application.
One of the nicest benefits of using XMPP is that there are lots of commercial and FOSS implementation of servers, clients and also libraries that you can use with your application.
Before we begin, you will need the following:
- Jabber server--Openfire
- Jabber IM client--Spark
- XMPP library--Smack
All of these software can be downloaded from Ignite site. After you have installed the software, either use Openfires administration or Spark to register a few users.
An alternative to installing Openfire (Jabber server) on your computer is to use one that is publically available. You can find a list of the servers here.
Smack XMPP Library
There are many XMPP Java libraries. However in this article we will be using Smack.
Connecting and Login In
To connect to a Jabber server with Smack, you will first need to create a ConnectionConfiguration object by passing the Jabber server and the corresponding number.
most cases, the port number to use will be the default port: 5222.
ConnectionConfiguration config = new ConnectionConfiguration("localhost", 5222);
Once we have created a ConnectionConfiguration object, we now use this to connect the the Jabber server on localhost:5222.
XMPPConnection connection = new XMPPConnection(config);
//Connect to the server
We are now ready to login to the server with the established connection.
String password = "fred";
//Login with username, password and a specific resource
connection.login(username, password, "my_app_service");
The code to do this is quite simple and straightforward. The only item that warrant some explanation is the resource ("my_app_service") in the login method. A service is very similar in concept to a TCP or a UDP port.
The service parameter is used by the server to identify an incoming connection. If a user has two or more connections with the same service, then one of these connection will be terminated. So to avoid potentially clashing with an existing Jabber IM client connection, it is advisable to set this.
One other use of the service parameter is to identify a connection to an application; let say we have written a peer-to-peer chess application using "chess" string as the resource for connection. Our chess application can now selectively accept other peers' connections only if that connection is marked with "chess" in its service parameter.
Use disconnect to terminate the connection.
You can find an example of the above code in this ZIP file. Unzip the file. The directory is a NetBeans project so you can open it with NetBeans. Do not forget to add smack.jar and smakx.jar to NetBeans.
Use Libarary Manager to create a libarary call 'Smack', and add the two mentioned JAR files into it. If you are just interested
in running the application, go into the dist directroy and use the following command to execute the application:
In a subsequent article, we will look at how to look up your peers and start a chat session with them.
Lee Chuk-Munn is a staff engineer at Sun Microsystems.