jump to navigation

How to write your own IM program/GTalk client in Java August 7, 2008

Posted by razasayed in programming.
Tags: , , ,

Here i will show you how you can write your own simple gtalk client using an open source java library called “Smack“. Smack is a cross-platform Open Source XMPP (Jabber) client library for instant messaging and presence , and as you will see it actually makes writing your own client to connect to any jabber service a breeze 🙂 . The library is extremely versatile but to keep things simple, here we develop a program to exchange messages with a buddy of yours in GTalk.

But before we start, what the heck is XMPP and Jabber ? . If you already know this you can skip to the next part.

You see there are many, many IM clients around. Companies such as Microsoft, and Yahoo provide IM services based on proprietary messaging protocols, and also provide popular client software (Microsofts MSN Messenger and Yahoos Yahoo Messenger) using these protocols. Many third-party clients also work with these protocols. However, these protocols remain proprietary and closed, which tends to make them difficult to work with for a developer.

Jabber is an open, XML-based “standard” for instant messaging and is an open source alternative to the proprietary messaging protocols . Jabber uses XMPP (Extensible Messaging and Presence Protocol), which is an “XML based protocol” , for transferring messages. Since the Jabber messages are transmitted in XML form it makes it easy for developers to work with them. Googles instant messaging service uses the Jabber protocol .

OK, now lets get our hands dirty with some code, shall we ? 🙂 . Alright, here we go .

Step 1) To start with download the Smack API

Step 2) Create a folder for your project. Create a lib subfolder . Unzip the downloaded zip file and copy the following files to the lib subfolder : smack.jar, smackx.jar,smackx-debug.jar.If you want you can also copy these files to any other location of your choice and then add the location of these files to the classpath.

Step 3) Now we write the actual code for implementing our gtalk client. To do that first create a file MyGTalkClient.java in your main folder, or anywhere else if you have added the location of the jar files to your classpath.

Step 3.1) The first step is connecting to the GTalk service  The code below does that :

ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com",5222,"gmail.com");
XMPPConnection connection = new XMPPConnection(config);
connection.connect(); /* Connect to the XMPP server  */
connection.login("username","password"); /* Login to the IM service */

“talk.google.com” is the host. 5222 is the port number and “gmail.com” is the service name.

Step 3.2) To send a message to a buddy we use the Chat class as follows :

Chat chat = connection.getChatManager().createChat("friend@gmail.com",new MyGtalkClient());
chat.sendMessage("Hi"); /* Send the message  */

The first argument to createChat method is obviously the email id of your buddy in GTalk . The second argument is an instance of a class that implements the org.jivesoftware.smack.MessageListener interface . This listener interface receives incoming messages and calls its processMessage method to process the message.

Step 3.3) Now, the next step is making sure that we receive the messages that our friend sends . To do that we implement the processMessage method of the MessageListener interface as follows :

public void processMessage(Chat chat,Message message) /*Callback method from MessageListener interface . It is called when a message is received */
        System.out.println("Received message: " + message.getBody());

Step 3.4) After you are done chatting with your friend you can disconnect from the server as follows :

connection.disconnect() ; //Disconnect 

There we are . A simple bare-bones Jabber client to connect to the GTalk service is ready ! .The final code looks like this .

Step 4) If you have not added the locations of the jar files to the classpath , you can compile and run your chat client as follows :

javac -cp lib\smack.jar;lib\smackx.jar;lib\smackx-debug.jar MyGTalkClient.java

java -cp lib\smack.jar;lib\smackx.jar;lib\smackx-debug.jar;. MyGTalkClient
(Note the ;. at the end of the classpath)

Thats all there is to it ! . From here on you can explore the API, and try to come up with really cool ideas for using instant messaging and presence in your applications.

Happy Hacking !! 🙂