Google recently released a new version of their SDK for the Google App Engine(GAE) and starting with this release the SDK now includes support for XMPP services in applications.
The ability to use XMPP opens up a number of interesting possibilities to interact with Google Talk users, but could also prove very useful for other tasks, such as integrating applications as well.
But let’s first take a look at how the XMPP support can be put into use.
Sending XMPP messages
This small piece of Java code sends a message to a specific Google Talk user, in this case the (presumably?) non-existent user someone@gmail.com
import com.google.appengine.api.xmpp.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;
import java.io.IOException;
public class XMPPSender extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws IOException {
ServletOutputStream out = res.getOutputStream();
//retrieve the service from the factory
XMPPService xmppService = XMPPServiceFactory.getXMPPService();
//specify who to chat with - JID is the "bare" XMPP address
String recipient = "someone@gmail.com";
JID jid = new JID(recipient);
String msgBody = "Hi there!";
//send an initial invitation to chat before sending messages
xmppService.sendInvitation(jid);
//build the message - in this case, just plain text.
//XML is also supported
Message msg = new MessageBuilder().withRecipientJids(jid)
.withBody(msgBody).build();
//send off the message and check if things went well
SendResponse status = xmppService.sendMessage(msg);
if (status.getStatusMap().get(jid) ==
SendResponse.Status.SUCCESS) {
out.println("That apparently went well...");
} else {
out.println("Something went wrong..");
}
}
}
On line 24 we send an invitation to the receiver of the message and the receiver must then accept the chat session before we can send messages.
A better way to do this, would be for the user to actively register with our application for chatting on a web page or by adding our the id of our XMPP client to the Google Talk roster.
JID names the target
As you can see in line 19, the JID is actually the address of the recipient of the message, also called the XMPP address.
An XMPP address could be a simple Gmail address like in this sample, but it could also target nodes specifying a resource to hit at the target domain.
Targetting specific resources at a domain requires a fully qualified JID like someone@example.com/machineXYZ, which is supported by the XMPP service on GAE.

The Linked Process Cloud (http://linkedprocess.org)
Using full JIDs you could use XMPP to target different nodes in a network and thus distribute processing on different nodes.
The Linked Process project extends XMPP to distribute processing on different computers and uses fully qualified JIDs to target a specific farm in their process cloud.
Receiving XMPP messages
Another feature of the new XMPP service is the ability to receive messages in your application.
If you want to receive XMPP messages you can do this by adding a few lines of code to a standard servlet as shown below.
import com.google.appengine.api.xmpp.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class XMPPReceiver extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException {
XMPPService xmppService = XMPPServiceFactory.getXMPPService();
Message message = xmppService.parseMessage(req);
JID fromId = message.getFromJid();
xmppService.sendMessage(
new MessageBuilder().
withBody("Your reply: " + message.getBody()).
withRecipientJids(fromId).
build());
}
}
In this servlet we simply reply back to incoming messages by echoing the message to the sender after adding “Your reply:” in front of it.
To make this work, you will need to change your configuration files a bit.
The first change is in the appengine-web.xml where you need to specify that you want to receive inbound XMMP messages.
xmpp_message
Next, you have to set up the XMPPReceiver servlet in your web.xml file which a specific URL
xmppreceiver
XMPPReceiver
xmppreceiver
/_ah/xmpp/message/chat/
The /_ah/xmpp/message/chat/ is the path that App Engine looks for to post XMPP messages to.
XMPP use cases
So the above samples simply deal with sending and receiving plain text messages to a Google Talk user.
The App Engine SDK does support exchanging XML messages as well, so you could exchange messages with another XMPP enabled application, for instance to start processes as done in the Linked Process project.
Other use cases could be to integrate applications such as Google Wave with chat using XMPP.
I put together a small application that notifies a Google Talk user of changes in a wave, so you can get notified if something changes inside a wave that you are participating in.
XMPP support in GAE is still relatively basic, but it is nonetheless a very powerful extension to the toolbox on the app engine and I believe that XMPP support is a vital ingredient in connecting different applications on the net.
#1 by Daniel Graversen on 16/09/2009 - 13:20
Quote
It is really easy to use to send messages. And if you use google talk it is also easy to receive. My large fear was that i needed know about xmpp, but this just works.
Pingback: Tweets that mention Chatting with Google App Engine ยป Miracle Geekhouse -- Topsy.com
Trackback: ALFONSO