Home Tags Archive
+ TWITTER Add to Google
Recent Articles

Google App Engine sending messages with XMPP

Authors : luigi agosti - Date : 04/04/2011 - Views : 4490
app engine xmpp
Google app engine has an amazing list of services you can use to enrich your web application. One of them is the XMPP service. In the documentation is stating :
Important: An App Engine application can send and receive chat messages to and from any XMPP-compatible chat messaging service, such as Google Talk. An app can send and receive chat messages, send chat invites, request a user's chat presence and status, and provide a chat status.
In this sentence are listed all the functionalities of the XMPP service. This is how I have implemented the send of a message to a certain client.
I have build a simple Gwt example that is getting the list of contacts using google contact api. So I'm in the position to have a gwt service that is receiving the email of the receiver and the message. So this is the method use to deliver the message with XMPP. It is presented in three parts :
private static final XMPPService xmpp = XMPPServiceFactory.getXMPPService();

public String getCurrentUserEmail() {
   UserService userService = UserServiceFactory.getUserService();
   User user = userService.getCurrentUser();
   return user.getEmail();

public void sendMessage(String receiverEmail, String message) {
   String email = getCurrentUserEmail();
Ok now I have the senderEmail, the receiverEmail and the message. No I need to prepare the message. To do that it is necessary to translate email in JID.
JID jid = new JID(to);
JID jidFrom = new JID(JidUtils.getJid(senderEmail));
Message msg = new MessageBuilder()
Let me put aside for now JidUtils.getJid(senderEmail). Next step is the delivery of the message itself. If you don't know if the user is in contact with the sender JID is better to ask for the invitation
//send initial invitation
boolean messageSent = false;
boolean presence = xmpp.getPresence(jid).isAvailable();
if (presence) {
   SendResponse status = xmpp.sendMessage(msg);
   messageSent = (status.getStatusMap().get(jid) == SendResponse.Status.SUCCESS);
In case the receiver is not present you probably have to send an email...
if (!messageSent) {
   // Send an email message instead...
Well all this is nothing complicated... About JidUtils.getJid(senderEmail) you need to send the message with a specific JID. You have two choice:
  • app-id@appspot.com
  • whateveryoulike@app-id.appspotchat.com
Warning: Note that the second user appspotchat.com not appspot.com. The app-id can be prefixed with the version version.latest.app-id.
So what I did in the function JidUtils.getJid(senderEmail) is removing the part after @ and change it with app-id.appspotchat.com