|
SYS-CON Magazines
|
Top Linux Links You Must Click On
Feature Building an Instant Messaging Application Using Jabber/XMPP
An adventure with Smack and Wildfire
By: Pramod Jain; Mahaveer Jain
Jun. 23, 2006 01:15 PM
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."
This article will describe the Jabber/XMPP protocol for messaging, the Jabber/XMPP client program based on JFace and Eclipse, and the Jabber/XMPP Java server. These will be illustrated through Open Source Smack and the Wildfire Server 2.4.4. It will offer examples of a login-class plug-in for custom authentication, a plug-in and server extension for custom messages, and a server-side extension for database interactions. Each example will contain the XML messages, client-side code, and server-side code.
What's Different About Jabber?
Any instant messaging system will have these basic features: It connects to server, registers new users, logs in, gets presence information, exchanges messages, and does custom interactions (VoIP, Web conferencing, etc.). Below each of these IM features are described using XMPP:
<stream to='192.168.0.12:5222' xmlns='jabber:client'/> The client receives:
<stream id='xxxx' from:'192.168.0.12:5222' xmlns='jabber:
<iq type='get' id='reg1' to='192.168.0.12:5222'><query xmlns='jabber:iq:register'/></iq> The server responds with a message that lists the fields required for registration:
<iq type='result' id='reg1'> <query xmlns='jabber:iq:register'> The client responds by sending the requested information:
<iq type='set' id='reg2'> On successful registration, the server responds: <iq type='result' id='reg2'/>
Declare Presence and Get Presence Information <presence xmlns="" id="d6vNV-3" from="mahaveerj@sow/1139352545625"/> Note the "to" attribute isn't required in the above message because the client knows which server it's talking to. To get presence information, client sends:
<iq xmlns="" id="d6vNV-2" type="get" from="mahaveerj@sow/1139352545625"> The server responds with presence data about two people in the roster:
<iq xmlns="" type="result" id="d6vNV-2" to="mahaveerj@sow/1139352545625">
Exchange Messages
<message xmlns="" id="H49LH-12" to="a1@sow" type="chat" from="a2 @sow/1139385809707"> The elements above form the core of XMPP (Extensible Messaging and Presence Protocol) XML syntax. It also gives you a sense of the flow of XML messages between the client and server to accomplish a task.
Jabber/XMPP Client and Server In this article we'll use the Smack library (www.jivesoftware.org/smack/) to illustrate client-side functionality. Smack provides an API for implementing the GUI and managing XML data. Managing XML data involves creating XML messages, sending the messages, receiving messages from the server, and processing the incoming messages. We'll use the Wildfire Server (www.jivesoftware.org/wildfire/) for illustrating creating server-side plug-ins. For basic IM needs, no server-side programming is required. For other needs Wildfire provides a plug-in architecture for extending the server's functionality. We'll look next at a use case to illustrate how custom messages can be created and processed at both the client and server.
Use Case 1: Custom Authentication and Custom Queries
Part A: Custom Authentication If you want to use a custom authentication method you'll have to change the user and authentication provider in wildfire.xml.
<provider> The simple extension defined in Listing 1 uses custom tables and custom encryption methods. The SOWUserProvider class is for user info and SOWAuthProvider is for authentication. The two static variables shown define the database authentication strings. And if custom encryption is used then the encryptPassword() method provides the encryption logic.
Part B: Retrieving a Token from the Database This process is illustrated in Figure 1.
Client to Server <iq to="serverName" type="get"><query xmlns="jabber:iq:token"/></iq> jabber:iq:token is a namespace for our custom query. This message is generated at the client using the following code:
XMPPConnection connection = session.getConnection(); The Class ClientID is defined in the section "Client-side Processing of Reply Packets" below.
The Server-Side Plug-In
public class ClientTokenPlugin implements Plugin { For more on how to create a plug-in, see www.jivesoftware.org/builds/wildfire/docs/ latest/documentation/plugin-dev-guide.html. IQTokenHandler has two key methods:
a) getInfo() which returns the type of queries in the iq messages that the token handler is going to process
public class IQTokenHandler extends IQHandler { The reply message will look like:
<iq to="clientName"> Reader Feedback: Page 1 of 1
Subscribe to our RSS feeds now and receive the next article instantly!
Subscribe to the World's Most Powerful Newsletters
|
||||||||||||||||