Welcome!

Java Authors: Liz McMillan, Walter H. Pinson, III, Maureen O'Gara, Yakov Werde, Tony Bishop

Related Topics: Java

Java: Article

Building an Instant Messaging Application Using Jabber/XMPP

An adventure with Smack and Wildfire

Client-Side Processing of Reply Packets
At the client end, you'll have to define a class to handles the IQ message. This is defined in a configuration file called smack.
provider:

   <iqProvider>
     <elementName>query</elementName>
     <namespace>jabber:iq:token</namespace>
     <className>org.jivesoftware.smack.packet.ClientID$Provider</className>
   </iqProvider>

The iqProvider defines the class and methods for both generating custom iq messages with the namespace jabber:iq:token for sending to server or other clients and for processing incoming iq messages with the namespace jabber:iq:token.

The first section of the code is for generating an iq packet, as described in the section "Client to Server" above. The provider contains a parseIQ() method to parse the incoming message and extract useful data and the tokenNum in the reply message above and stores them in object c in Listing 2. This ends the lifecycle of the iq packet.

Use Case 2: The Custom Message
During collaboration, userA wants to launch a browser with a URL in userB's machine. This will be implemented as a custom <message>. Usually <message> is used to carry chat messages, but in this use case it will be used to carry a custom message (<xsow>) like:

   <message xmlns="" id="WHFl2-7" to="userB@sow" from="userA@sow/1139322562328">
     <xsow xmlns="http://www.indent.org">
       <conf Url="http://my.shareonWeb.com/jetspeed/indent/sowWCLogin.jsp?p=2079810"/>
     </xsow>
   </message>

The steps are:

  1. At the client SOWExtension, a custom PacketExtension, is implemented to create the message and the provider to parse the incoming custom message
  2. At the receiving client, a packet filter and listener are implemented to trap the custom message and process it.
This is illustrated in Figure 2.

Creating a Custom Message at the Client In the use case, the first step for userA to send a custom message to userB is to do the following in the client GUI: userA chooses "Open Document" from a menu or right-clicks on userB in the roster list and chooses "Open Document." The client GUI in our case uses Smack and JFace; "Open Document" triggers a call to the run() method associated with the selection. The run() method creates a message and sends to server, which it then routes to userB.

public void run() {
   Message iceMessage = new Message();
   SOWExtension icePacket = new SOWExtension();
   String urlToSend = this.appData.aspireRequest + "&p=" + ClientID.token;
   icePacket.setUrl(urlToSend);
   iceMessage.addExtension(icePacket);
   iceMessage.setType(Message.Type.NORMAL);
   for (Iterator i=selection.iterator(); i.hasNext();) {
     Object user = i.next();
     RosterEntry entry = (RosterEntry) user;
     iceMessage.setTo(entry.getUser());
     Session.getInstance().getConnection().sendPacket(iceMessage);
     }
   }

A standard message/packet is created first; an extension is created on the message using the new SOWExtension() - this is going to carry the custom packet; the custom packet is added to the message using addExtension(icePacket). The for loop then sends the message to each selected user (userB, userC, ...). The sendPacket() method calls toXML() in Listing 3 to create an XML string.

The SOWExtension class implements PacketExtension; one of the methods of interest is the class is toXML() and the class of interest is the Provider class. The Provider class provides the parse method for incoming custom messages with the extension <xsow>. The provider class will be discussed in the next section.

Processing the Custom Message at the Client
Note that the server doesn't do any operation on the message other than route it to userB. userB gets the message first by parsing it using the parseExtension() in Listing 3, and then passing the message through a filter described below. filterExt below is a filter that looks for packets with the extension "xsow." When the filter recognizes such a message, a listener then processes the message. The processPacket() method below first extracts the extension in the custom message by calling message.getExtension(). getExtension() returns an SOWExtension object defined in Listing 3. Since the custom message above has already been parsed, this object contains all the relevant information contained in the packet extension. sowExt.getURL() returns the desired value. The last two lines in listener below correspond to the desired action, in this case, opening a browse with a URL that was contained in the extension of the custom message above.

PacketListener listener = new PacketListener() {
   public void processPacket(Packet packet) {
     final Message message = (Message) packet;
     SOWExtension sowExt = (SOWExtension) message.getExtension(
     SOWExtension.elementname, SOWExtension.namespace);
     String url = sowExt.getUrl() + "&uname=" +
     Session.getInstance().getConnectionDetails().getUserId();
     Program.launch(url);
   }
};
PacketExtensionFilter filterExt = new PacketExtensionFilter("xsow", "http://www.indent.org"); connection.addPacketListener(listener, filterExt);

The message extension and the SOWExtension's Provider are defined in smack.provider.

   <extensionProvider>
     <elementName>xsow</elementName>
     <namespace>http://www.indent.org</namespace>
     <className>org.jivesoftware.smackx.packet.SOWExtension$Provider</className>
   </extensionProvider>

Note: iqProvider to handle custom IQ messages was defined in reply packets code above. extensionProvider to handle custom message extension is defined in the code immediately above.

Summary
There are several Open Source and commercial implementations of XMPP server and client to choose from that provide basic chat, presence, and roster functionality. Here we discussed how to extend the functionality of your XMPP-based IM applications through two mechanisms: defining custom queries in the <iq> element and defining extensions in the <message> element. Client-side extensions and server-side plug-ins are described to accomplish the custom functionality.

Some of the specific uses of the <iq> extension will be to create rich IM applications that interact with a backend application server and/or database; examples of uses of the <message> extension include doing things like opening browsers and sending files from one user's client IM application to another.

References

Acknowledgements
We would like to acknowledge partial funding from NIMH of NIH, and the help of Gaurav Mantro for coding some pieces of the client application and thank Ryan Graham, Gaston Dombiak, and a few others for answering questions we posted on www.jivesoftware.org's discussion forum for developers.

More Stories By Pramod Jain

Pramod Jain is president of Innovative Decision Technologies, Inc. (INDENT, www.indent.org), in Jacksonville, FL. Their clients include Recruitmax, NASA, and NIH. Pramod has a PhD from the University of California, Berkeley.

More Stories By Mahaveer Jain

Mahaveer Jain is a lead programmer at INDENT. His expertise is in developing collaboration applications with Java technologies.

Comments (2) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
SYS-CON Australia News Desk 06/23/06 09:05:30 AM EDT

This article will describe our experiences with developing a Java-based instant messenger application using Jabber/XMPP (Extensible Messaging and Presence Protocol) - a free, open and public protocol and technology for instant messaging. According to the Jabber Software Foundation, 'Under the hood, Jabber is a set of streaming XML protocols and technologies that enable any two entities on the Internet to exchange messages, presence, and other structured information in close to real-time.'

SYS-CON Australia News Desk 06/22/06 03:52:45 PM EDT

This article will describe our experiences with developing a Java-based instant messenger application using Jabber/XMPP (Extensible Messaging and Presence Protocol) - a free, open and public protocol and technology for instant messaging. According to the Jabber Software Foundation, 'Under the hood, Jabber is a set of streaming XML protocols and technologies that enable any two entities on the Internet to exchange messages, presence, and other structured information in close to real-time.'