Oracle GlassFish Server 3.0.1 Application Development 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 GlassFish 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:

Enabling Comet

Before running a Comet-enabled application, you need to enable Comet in the HTTP listener for your application by setting a special attribute in the associated protocol configuration. The following example shows the asadmin set command that adds this attribute:

asadmin set"true"

Substitute the name of the protocol for http-1.

ProcedureTo Configure the web.xml File

To enable the Bayeux protocol on the GlassFish 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.

ProcedureTo Write, Deploy, and Run the Client

The examples in this task are taken from the example chat application that is 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 GlassFish Server, see the Oracle GlassFish Server 3.0.1 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 REpresentational State Transfer (RESTful) web services and Comet, see RESTful Web Services and Comet.