Instant messaging on your site

Apr 14, 2009
Cesar Salazar

This is my first post at Oshyn :) (hopefully more will come soon). This post is related to research I recently conducted while I was looking for alternatives for including instant messaging (IM) in a very interesting project. There are some alternatives for implementing it in Java; in this post we analyze the alternatives and describe how to create a simple bot using some open source libraries.

What can IM do for my site?

I believe that the first question we must ask ourselves before doing something is... why do this? How can I benefit from having instant messaging on my site?

Here are some thoughts on how you can use IM within a website:

Wouldn't it be cool to have your sales representatives receive an instant message via their PC or their phone right when a user registers and requests for more information on one of your products? Or what about not only letting them receive an IM message but also send messages or start a chat with a client?

When it comes to social sites, the applications are unlimited; you can see which contacts are online, chat with them, send them articles, photos, videos, discount coupons or even gifts... Social networks can grow based on invitations sent using IM systems.

You can also connect the bot with other applications. One idea could be, for example, to connect the bot with an RSS feed so all registered users can receive an instant message about news or promotions.

If you can imagine a good idea on how to apply instant messaging to your site, it can be done. There are lots of ways you can use it. Really, you are only limited by your imagination.

Let's build it in Java

The first idea we had was creating an IM Bot. This bot would basically be an application that would receive requests from our website and send messages to IM clients.

When researching how to implement the bot in Java (the technology chosen for this project), we considered a lot of possible solutions; we thought about contracting an IM bot provider and we also thought about building the bot from the scratch. The final solution that fit the best for us was to build it using some tested and stable open source libraries.

There are several libraries available if you want to connect to a specific messenger service. (e.g. Google Talk, Yahoo Messenger, MSN, AIM, etc...). Some of these libraries are:

  • JML: An MSN Messenger Library
  • MSNM: Another, older MSN Messenger Library.
  • JOscar: Library for connecting to AOL Instant Messenger
  • OpenYMSG: A library for connecting to Yahoo Messenger.
  • yMsg API: Another API for Yahoo Messenger. For version 10 (YMSG-10).
  • Smack: An Open Source XMPP (Jabber) client library. Google talk and others are based on this protocol.

These libraries are stable and easy to use, but a big problem arose if you needed to connect to more than one IM network (or to all of them, as in our case), because all these libraries are different and would require you to repeat your code for all of them or that you create a common interface and a decorator class for each one of them.

At that junction we found JClaim, an open source library that provides a common interface to use all the libraries mentioned above.

JClaim also provides a user interface - but that's not usable if we want to develop our own bot.

 JClaim is not perfect but it's stable and suits many applications. One of its problems is that it's not provided as a library, but as an auto-installer that deploys a desktop application. Because of that, before using it, I had to download the source code and compile it again excluding the unnecessary elements.

Implementing a simple bot with JClaim is pretty straightforward:

  1. Download the source code, compile it, and package it in a jar (JClaim.jar). You may want to exclude all the user interface classes, since you are not going to use them in the bot.
  2. Create a java project and include the JClaim.jar library you just created and the other required dependencies, based on which IM network you are going to use.
  3. Start coding!


You need at least two classes:

1.    One class that must contain a method for initializing the connection and add the listener. This class contains the following code:

// Create a contactFactory. This can be a static final variable
ContactFactory contactFactory = new ContactFactoryImpl();
// Create a groupFactory. This can be also a static final variable
GroupFactory groupFactory = new GroupFactoryImpl();

// Create the connection, in this case to Yahoo Messenger
MessageSupport conn = new YmsgConnection();

// Assign the contactFactory, groupFactory and credentials
conn.assignContactFactory(contactFactory);
conn.assignGroupFactory(groupFactory);
conn.setUserName("username-here");
conn.setPassword("password-here");

// add the listener (see below for the listener implementation)
ConnectionEventListener listener = new ConnectionEventListenerImpl();

// connect
conn.connect();


2.   A second class "listener" that will process the events received. This class must implement the ConnectionEventListener interface. In order to keep this post short, I copied only the implementation of messageReceived method. This method just replies the sender right after receiving a message.

public boolean messageReceived(MessageSupport connection,
        Message message)
throws Exception {

    if (!message.isOutgoing()) {
        connection.sendTypingNotification();
        Message m = new MessageImpl(message.getContact(), true,
                false,
"You said: " + message.getText());
        connection.sendMessage(m);
    }
    return false;

}


And Voilà! 

    Another approach: An IM Broker


While the approach of building an IM bot can be interesting, and works well in many scenarios, for our specific application this solution was not suitable. We wanted that every user can gets connected and can chat with their contacts directly from the website. That meant creating not only a bot, but a broker to keep a pool of connections.

We realized that creating a stable, scalable instant message broker can be really challenging. To make the long story short, the final design included a website built on Seam Framework, a JMS server (ActiveMQ) with several queues for sending and receiving messages and commands from and to the message broker, and a broker implementation that wraps JClaim using EJB3.