Oct 28 2007
Writing a gTalk (Jabber/XMPP) client in Java
From its Wikipedia entry, “Extensible Messaging and Presence Protocol (XMPP) is an open, XML-inspired protocol for near-real-time, extensible instant messaging (IM) and presence information (a.k.a. buddy lists). It is the core protocol of the Jabber Instant Messaging and Presence technology. The protocol is built to be extensible and other features such as Voice over IP and file transfer signaling have been added.”
There are a large number of XMPP server and client implementations and code libraries. We will use the Smack open source Java library to implement a simple client that can connect to any Jabber service. Google’s messaging service uses the same protocol and we will use it for the example.
Create a folder for your project. Create a “lib” subfolder.
Download the Smack library and extract the following files to the lib folder:
- smack.jar
- smackx.jar (for the extensions)
- smackx-debug.jar (for the enhanced debugger)
Debugger screenshots: 1 | 2 | 3 | 4
As of this writing, the Smack API is in version 3.0.4 (build date: June 12, 2006). Create ChatClient.java and use the code snippets below for the methods of this class. You can also download the project files from the link at the end of this post.
The code below shows how to connect to the gTalk service:
1 2 3 4 5 6 7 8 9 10 | // import org.jivesoftware.smack.ConnectionConfiguration; // import org.jivesoftware.smack.XMPPConnection; ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com"); connection = new XMPPConnection(config); connection.connect(); connection.login(your_username, your_password); |
“talk.google.com” is the host. 5222 is the port number and “gmail.com” is the service name. Although they are different here but usually, the service name is same as the hostname. Let us now get hold of the buddy list:
1 2 3 4 5 6 7 8 9 10 11 | // import org.jivesoftware.smack.Roster; // import org.jivesoftware.smack.RosterEntry; Roster roster = connection.getRoster(); Collection<RosterEntry> entries = roster.getEntries(); System.out.println("\n\n" + entries.size() + " buddy(ies):"); for(RosterEntry r:entries) { System.out.println(r.getUser()); } |
We can also send a message to a buddy by using the Chat class:
1 2 3 4 | //import org.jivesoftware.smack.Chat; Chat chat = connection.getChatManager().createChat(to, this); chat.sendMessage(message); |
The first argument is quite obvious. The second argument is an instance of a class that implements the org.jivesoftware.smack.MessageListener interface. This listener receives incoming chat messages and calls the interface method
1 | public void processMessage(Chat chat, Message message) |
to process the message. We can now disconnect from the messenger service:
1 | connection.disconnect(); |
Thus, we have a bare-bones Jabber client. The complete program looks like this. You can download the project with instructions on compiling and running it. It has a hard-coded username and password which will have to be changed. You will also need to enter the email address of your gTalk buddy before sending any message. The person you are trying to communicate with should already be in your buddy list.
