Site Archive (Complete)
Java Blog: Java on Smack
Java
SWAINE'S CAFE

Black. No Sugar. Extra Caffeine.

by Mike Swaine
ERIC BRUNO'S BLOG

Java: The Daily Grind.

by Eric Bruno
October 11, 2006

Java on Smack

Many of you know Jabber, the open-source, XML-based, standard for instant messaging, but did you know about Smack?

Jabber has over 21 million users, and is the basis for Google’s IM client, Google Talk. There exist literally hundreds of existing Jabber-based IM clients and Jabber-enabled applications. There are some good reasons why you may want to incorporate Jabber IM into your application as well, such as to enable Internet-capable event notification, or to build a community around your users so they can communicate with one another.

With the Smack API for Java, it’s possible for you to add IM functionality to your Java applications today. A recent article by John Ferguson Smart, published on java.net, describes the process in detail. Let’s take a quick look at the highlights here.

eXtensible Messaging and Presence Protocol (XMPP)

Jabber uses an XML-based protocol called eXtensible Messaging and Presence Protocol (XMPP). The protocol supports all types of messages, such as chat, group chat, email-like messages, and headline (or alert) messages as well. Here’s a simple example:

<message to=”eric@ericbruno.com” type=”chat”>
  <thread>t12345</thread>
  <body>Hi Eric, great blog entry</body>
</message>


The Smack API

The Smack API itself is written in Java, and it easily allows you to integrate message sending into your Java application. Of course, you need to have a message server in the middle to broker the communication, such as Wildfire, or Google Talk.


Here is the code to connect to a generic Jabber server:

XMPPConnection connection = new XMPPConnection("ericbruno.com");
connection.login("user", "password");
Message message = new Message();
message.setTo("eric@ericbruno.com");
message.setSubject("Blog Entry");
message.setBody("I really liked your entry on the Smack API");
message.setType(Message.Type.HEADLINE);
connection.sendPacket(message);
connection.close();


It’s very simple: you login to a server; build your message; send the message; close the server connection. If you think that’s easy, take a look at the specialized Google Talk API:

GoogleTalkConnection connection = new GoogleTalkConnection();
connection.login("john", "smith");
connection.createChat("ericbruno@gmail.com").sendMessage("Nice blog entry");


Detecting presence is straightforward as well:

Roster roster = connection.getRoster();
Iteration iter = roster.getEntries()
while (iter.hasNext())
{
  RosterEntry entry = (RosterEntry) iter.next();

  Presence presence = roster.getPresence(entry.getUser());

  if (presence.getType() == Presence.Type.AVAILABLE)
    System.out.println(entry.getUser() + " is ONLINE");
  else
    System.out.println(entry.getUser() + " is OFFLINE");
}


First, the code goes through your roster, which is the list of users you’ve indicated you’d like IM with from time to time. As we loop through the roster entries, we check their availability by calling getPresence on each RosterEntry. Finally, we display each roster entry’s username and an indication of presence: online or offline. The API supports adding and removing users to your roster, as well as user discovery.

Receiving messages is also straightforward. In fact, the entire Jabber API is quite easy to use. Most of your work can focus on the user interface of your IM-enabled application. For more details, see below:

- Jabber
- Smack API
- XMPP
- Article: Instant Message in Java Made Easy: The Smack API

-EJB

Posted by Eric Bruno at 09:15 AM  Permalink




 
INFO-LINK


Related Sites: DotNetJunkies, SD Expo, SqlJunkies