Dojo HttpChannels Module

by Kris ZypJuly 18th, 2008

HTTP Channels is implemented in Dojo 1.2 with the HttpChannels module. HTTP Channels is intended to provide data synchronization and update notifications by leveraging the ubiquitous HTTP REST semantics. Within Dojo, HTTP Channels are particularly useful, integrating directly with Dojo Data for live data synchronization. The HttpChannels module is very flexible as well; it can be used in several different ways:

Live REST Updates

The easiest and highest level use of HTTP Channels is in conjunction with the JsonRestStore. HttpChannels can be used as an enhancement for the REST service, to enable “live” data (real-time updates that directly alter the data in caches/indexes). To add enable live updates through HTTP Channels messages to the data in JsonRestStore, simply add HttpChannels after loading the JsonRestStore:

dojo.require("dojox.data.JsonRestStore");
dojo.require("dojox.cometd.HttpChannels");

Now, we can create and use a JsonRestStore exactly the same way as you would without HttpChannels:

var myStore = new dojox.data.JsonRestStore({target:"/myTable"});

This is all that is needed on the client side (of course on the server side you need a HTTP Channels capable storage system, like Persevere). All resource/data retrievals made by the JsonRestStore will automatically trigger subscriptions to the resources, and data updates that are sent from the server will automatically trigger the appropriate Dojo Data notifications for updates in widgets.

This means that building an application with Dojo that can load data from a server and receive updates on data changes and update the UI with these changes can be as simple as plugging a JsonRestStore data store into a widget like a grid, and letting Dojo handle all the communication and notification routing. Plugging a “live” data store into a grid can be done like:

<table dojoType="dojox.grid.DataGrid" store="myStore" ...

We can see a very simple example of using a live data store with a grid widget.

Cometd Transport

HTTP Channels is a Bayeux compliant transport, and the HttpChannels module can also be used as a Cometd transport. Simply load HttpChannels after Cometd and it will auto-register itself as a Cometd transport:

dojo.require("dojox.cometd");
dojo.require("dojox.cometd.HttpChannels");
// initialize cometd
dojox.cometd.init("/cometd");
// subscribe to a channel
dojox.cometd.subscribe("/weather/slc", function(){ ... });
// publish to a channel
dojox.cometd.publish("/news/slc", {title:"Hot Weather"});

By loading the HttpChannels module after Cometd, HttpChannels will register itself as an available transport. If the server advertises the ability to handle the “http-channels” transport, the Cometd module will use HTTP Channels for handling subscriptions and publishing messages. It is important to note that HTTP Channels integrates pub-sub messaging with REST resource locators, and therefore publishing messages and subscribing are executing by HTTP requests directly to the target path. With an HTTP Channels compliant server, the example above would result in HEAD request with a subscription header to the relative URL “/weather/slc”, and POST request to the relative URL “/news/slc”.

Standalone Protocol

To use the HttpChannels module standalone, you first get an instance of HttpChannels, the default instance (which is used with the REST services and Cometd by default) can be retrieved:

var httpChannels = dojox.cometd.HttpChannels.defaultInstance;

You can now directly access the HttpChannels API. The primary HttpChannels functions are get, subscribe, unsubscribe, and receive. The subscribe function allows you to subscribe to a resource and provide a callback for notifications. The get function also does a subscribe, but it gets the target resource at the same time as sending a subscription request. The receive function is called whenever data is received from the server. You can use dojo.connect to listen for all messages from the server. For example:

dojo.require("dojox.cometd.HttpChannels");
var httpChannels = dojox.cometd.HttpChannels.defaultInstance;
 
// get the current weather and subscribe to future changes
httpChannels.get("/weather/slc", function(weatherReport){ 
	// this is called for the initial response and all updates
}); 
dojo.connect(httpChannels,"receive",function(message){
	// this is called for all notifications from the server
});

Dojo’s new HttpChannels module provides an efficient implementation of HTTP Channels with strong integration with other modules in Dojo including Cometd and JsonRestStore. HttpChannels can be used for rapidly developing data-centric applications that rely on real-time updates; Dojo provides all the facilities for creating a Comet connection with the server, handling server-sent messages, appropriately updating data, delivering notifications through data stores, and updating widget user interfaces in response to these notifications.

4 Responses to “Dojo HttpChannels Module”

  1. mic Says:

    your Local url is not accessible across the net (http://localhost:8080/examples/customer.html on “example of using a live data store with a grid widget “.

  2. Lars Trieloff Says:

    Hi Kris, does the cometd-jetty server-side implementation already support HttpChannels? I would love to see some wider adoption aside from Persevere.

  3. Jack Kwok Says:

    Hello Kris,

    HttpChannels is a great addition to dojo. But we are using jetty with cometd (not persevere) on the server side. What is needed to make HttpChannels work in my environment?

  4. Kris Zyp Says:

    @Jack: Seehttp://www.sitepen.com/blog/2009/06/15/using-rest-channels-with-cometd/


Copyright 2015 Comet Daily, LLC. All Rights Reserved