Comet Application Tutorial

This tutorial demonstrates the simplicity in providing Comet (AJAX push, reverse AJAX) functionality to a WoOF web application.

To demonstrate the ease of using Comet functionality the following simple chat application will be used for this tutorial.

CometApp screenshot.

The application in this tutorial is deliberately simple to allow focus on the Comet functionality.

Download Tutorial Source

Template.woof.html

The below is the HTML for the page.

<html>
	<body>
		<p>Chat:</p>
		
		<center><big>  <p id="chat"></p>  </big></center>
		
	</body>
</html>

ConversationSubscription Class

As a web browser typically will allow only two connections back to the server, a connection can not be used for each Comet subscription.

OfficeFloor handles the Comet subscriptions by multiplexing them over one of the connections leaving the other connection available for servicing the other web page requests. To distinguish between subscriptions each subscription is identified by an interface. For this tutorial the interface is as follows.

public interface ConversationSubscription extends CometSubscriber {

	void sendMessage(String message);

}

The definition of the interface is free to be defined per your need, however it must adhere to the following:

  • Must be an interface
  • Must have only one method with at most two parameters (the first being the event payload and second a filter key)
  • Must extend CometSubscriber (necessary for GWT client side code generation)

EntryPoint Class

The following class is the GWT EntryPoint class for this tutorial. It demonstrates how the above interface is used in both subscribing to Comet events and publishing Comet events.

public class CometAppEntryPoint implements EntryPoint {

	@Override
	public void onModuleLoad() {

		// Vertically align contents
		RootPanel panel = RootPanel.get("chat");
		VerticalPanel chatPanel = new VerticalPanel();
		panel.add(chatPanel);

		// Provide the text area to contain conversation
		final TextArea conversation = new TextArea();
		conversation.setReadOnly(true);
		conversation.setSize("100%", "300px");
		chatPanel.add(conversation);

		// Handle listening for messages
		OfficeFloorComet.subscribe(ConversationSubscription.class,
				new ConversationSubscription() {
					@Override
					public void sendMessage(String message) {
						conversation.setText(conversation.getText() + "\n"
								+ message);
					}
				}, null);

		// Provide means to add message
		HorizontalPanel messagePanel = new HorizontalPanel();
		chatPanel.add(messagePanel);
		final TextBox message = new TextBox();
		message.setWidth("80%");
		messagePanel.add(message);
		Button send = new Button("send");
		messagePanel.add(send);

		// Handle submitting a message
		final ConversationSubscription publisher = OfficeFloorComet
				.createPublisher(ConversationSubscription.class);
		send.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				String messageText = message.getText();
				publisher.sendMessage(messageText);
			}
		});
	}

}

The next sections explain how OfficeFloorComet is used for the subscription to Comet events and publication of Comet events.

Comet Subscription

The OfficeFloorComet.subscribe(...) method registers subscription to Comet events for the provided interface. On the first subscribe OfficeFloorComet will establish a connection with the server and start the long polling for events. On receiving an event OfficeFloorComet will appropriately invoke the handler (interface implementation) with the event. It will then send another long poll request for the next event. This means only the handler implementation is required and OfficeFloor takes care of listening for events from the server.

It is possible to subscribe to more than one interface within the same GWT client. Please see the OfficeFloorComet API for further details regarding multiple subscriptions.

In the case of this tutorial the handle of the event is simply appending the text as the next line in the conversation.

Comet Publication

The OfficeFloorComet.createPublisher(...) method creates an implementation of the interface to enable publishing an event.

OfficeFloor sends the published events immediately. It does this via the second connection from the browser. As OfficeFloor's server functionality sends an immediate response acknowledging the event, the connection is then free to serve further web requests.

In the case of this tutorial the publish sends the message text to the server. The server will then notify the appropriate subscribers of the event. This causes the conversation text box to be updated with the message.

Server Side

The following demonstrates the configuration necessary for supporting Comet on the server:

CometApp Add template dialog screenshot.

After checking the checkbox to enable comet, there is no additional configuration or coding necessary to support a single server providing Comet functionality.

Next

The next tutorial looks at how the Server can publish events to Comet clients and also how the Server can intercept published events.