Sun GlassFish Enterprise Server v3 Prelude Developer's Guide

Bayeux Protocol

The Bayeux protocol, often referred to as Cometd, greatly simplifies the use of Comet. No server-side coding is needed for servers such as Enterprise Server that support the Bayeux protocol. Just enable Comet and the Bayeux protocol, then write and deploy the client as described in the following tasks:

ProcedureEnabling Comet

Before running a Comet-enabled application, you need to enable Comet in your application server by adding a special property to the http-listener element of the domain.xml file.

The following steps tell you how to add this property.

  1. Open domain-dir/config/domain.xml in a text editor.

  2. Add the following property in between the http-listener start and end tags:

    <property name="cometSupport" value="true"/>
  3. Save domain.xml and restart the server.

ProcedureConfiguring the web.xml File

To enable the Bayeux protocol on the Enterprise Server, you must reference the CometdServlet in your web application's web.xml file. In addition, if your web application includes a servlet, set the load-on-startup value for your servlet to 0 (zero) so that it will not load until the client makes a request to it.

  1. Open the web.xml file for your web application in a text editor.

  2. Add the following XML code to the web.xml file:

       <servlet-name>Grizzly Cometd Servlet</servlet-name>
             expirationDelay is the long delay before a request is
             resumed. -1 means never.
       <servlet-name>Grizzly Cometd Servlet</servlet-name>

    Note that the load-on-startup value for the CometdServlet is 1.

  3. If your web application includes a servlet, set the load-on-startup value to 0 for your servlet (not the CometdServlet) as follows:

  4. Save the web.xml file.

ProcedureWriting, Deploying, and Running the Client

The examples in this task are taken from the example chat application posted and discussed at

  1. Add script tags to the HTML page. For example:

    <script type="text/javascript" src="chat.js"></script>
  2. In the script, call the needed libraries. For example:

  3. In the script, use publish and subscribe methods to send and receive messages. For example:

    cometd.subscribe("/chat/demo", false, room, "_chat");
    cometd.publish("/chat/demo", { user: room._username, chat: text});
  4. Deploy the web application as you would any other web application. For example:

    asadmin deploy cometd-example.war
  5. Run the application as you would any other web application.

    The context root for the example chat application is /cometd and the HTML page is index.html. So the URL might look like this:

See Also

For more information about deployment in the Enterprise Server, see the Sun GlassFish Enterprise Server v3 Prelude Application Deployment Guide.

For more information about the Bayeux protocol, see Bayeux Protocol.

For more information about the Dojo toolkit, see

For information about pushing data from an external component such as an EJB module, see the example at Using this Grizzly Java API for Cometd makes your web application non-portable. Running your application on a server that doesn't support Grizzly Comet will not work.

For information about RESTful (REpresentational State Transfer) web services and Comet, see RESTful Web Services and Comet.