Home Tags Archive
+ TWITTER Add to Google
Recent Articles

Google app engine xmpp managing the status

Authors : luigi agosti - Date : 20/04/2011 - Views : 4456
xmpp app engine
I'm playing with a chat. And I want to do it with Google App Engine. In a previous article I have shown how to send messages. Now I want to show you what I have done to manage the status of a user so that from google talk other users are able to see your status.
Xmpp and Google App Engine
To use xmpp service with google app engine is very easy. The only thing you have to do is to declare what you want to manage. To do that you need to edit the appengine-web.xml file in the WEB-INF folder. There are three services : Xmpp_message, xmpp_presence, and xmpp_subscribe. In this article we are interested in xmpp_presence. But this is the snippet of code you need to add in appengine-web.xml
As soon as you add xmpp_presence service to your appengine-web.xml the application receives messages to the following urls :
  • /_ah/xmpp/presence/available/
  • /_ah/xmpp/presence/unavailable/
  • /_ah/xmpp/presence/probe/
You have to handle this calls yourself if you want to manage the status. Otherwise the account of the chat will always appear in the available status. The easiest way to handle the call is to have servlets mapped to the urls in the web.xml like :
The most important one is probe. Here you need to get the information of the user and send back a response as soon as a request comes in. I store the status of the users in a profile table, in this way as soon as I receive a probe request I'm able to replay with the correct status. Here some pseudo code:
//in the servlet from the request I get the presence
Presence presence = xmpp.parsePresence(req);
//with the presence I can get the sender and the receiver
JID to = presence.getToJid();
JID from = presence.getFromJid();

//I get my profile for the receiver from witch I get a status string
String status = dao.getProfileStatus(to);
if("away".equalsIgnoreCase(status)) {
  type = PresenceType.AVAILABLE;
  show = PresenceShow.AWAY;
} else if("busy".equalsIgnoreCase(status)) {
  type = PresenceType.AVAILABLE;
  show = PresenceShow.DND;
} else if("available".equalsIgnoreCase(status)) {
  type = PresenceType.AVAILABLE;
  show = PresenceShow.CHAT;
} else if("offline".equalsIgnoreCase(status)) {
  type = PresenceType.UNAVAILABLE;
  show = PresenceShow.XA
When I have defined the current status of the user in terms of PresenceType and PresenceShow I can inform the sender :
xmpp.sendPresence(from, type, show, "", to);
In theory we should inform all the contacts of a user of a status change as well. Not sure though if is a bit too expensive.
I still have a lot to discover about xmpp so if you think that I'm wrong or that I should do stuff in another way please leave a comment.