Simple server event publish and subscribe example

The cometd client in the Dojo Toolkit provides a way to receive server events. The cometd client communicates through the Bayeux protocol to a Bayeux-enabled server. The Bayeux protocol supports the standard publish and subscribe operations of subscribe, unsubscribe, and publish. Subscriptions link incoming server events to the Dojo event and topic system. Using the cometd client is straightforward. A simple test example is provided in the following example. This sample sends an event to the server when you select the test button, and the browser receives the event and passes it to the message function for an alert message.

Simple cometd example

<html>	<head>
    <script type="text/JavaScript" src="dojo/dojo.js"></script>	
    <script type="text/JavaScript">
    
       dojo.require("dojox.cometd");
       
        dojo.addOnLoad(function(){
           	dojox.cometd.init("cometdTest");
            dojox.cometd.subscribe("/testtopic", window, "alertMessage");
        });
        
        function alertMessage(message) {
            alert("Message: " + message.data.test);
        }
        
    </script>
    
</head>
<body> 

    <button onClick="  dojox.cometd.publish('/testtopic', { test: 'Data'});">Test</button>

</body></html>
      


The first step to using the cometd client in a Web page is to include the cometd client. The dojo.require("dojox.cometd"); statement includes the cometd client in the Web page. The next step is to initialize the cometd client. The cometd.init function take one arguments. This argument is the cometd initialization uniform resource identifier (URI). The URI specified must point to a cometd-enabled server endpoint. All Bayeux communication goes through this URI. The last argument is an optional bind argument for the XmlHttpRequest initialization call to the cometd server.

After initialization, the Web page can then publish, subscribe, and unsubscribe to server topics. The subscribe and unsubscribe functions take four arguments. The first is the topic name. The second argument is a true or false option that indicates whether to use local topic names. If use local topic names is set to true, then events can be linked to the Dojo topic system so that any Dojo logic can subscribe to receive incoming server events just like a typical local Dojo event. The third and fourth arguments specify the object name and function call when messages are received from the server in the case of the unsubscribe function. The object and function are received to unsubscribe from the topic. The publish function takes two arguments, the topic name, and the data to publish in JavaScriptTM Object Notation (JSON) format.

The previous example demonstrates basic usage of the cometd client. The cometd init function is called in the process of the dojo.addOnLoad function after any Dojo initialization is complete. The subscribe function typically occurs after cometd initialization, yet can be called before initialization. Any subscribe and publish function called before initialization is run after initialization is complete. The subscribe function links incoming events to the alertMessage function, but does not link event delivery into the local topic system. In the alertMessage function, the .data array index is used to access message array information. In the previous case, the message.data.test variable displays the data of interest.