DWR 2.0 milestone 1 does Reverse Ajax

We've just released DWR version 2.0 milestone 1 - This is probably the biggest release we've ever done in terms of new features.

Reverse Ajax: DWR 1.x allowed you to asynchronously call Java code from Javascript. DWR 2.0 builds on this to allow you to asynchronously call Javascript code from Java. Reverse Ajax makes writing interactive applications much easier. It can use polling or Comet (long-lived HTTP) queries.

Our 'chat' example contains Java code like this:

// Get the current page
WebContext wctx = WebContextFactory.get();
String currentPage = wctx.getCurrentPage();
 
// 'messages' is a List of recent messages for a browser to display
// Java objects converted to Javascript have a declaration and a declared variable name.
OutboundVariable ov = wctx.toJavascript(messages);
 
// Loop over all the users on the current page
for (ScriptSession otherSession : wctx.getScriptSessionsByPage(currentPage)) {
    otherSession.addScript(ov.getInitCode() );
    otherSession.addScript("receiveMessages(" + ov.getAssignCode() + ");" );
    // receiveMessages is a Javascript function that displays the current messages
}

In essence we are looping over all the users on the current page and sending them some Javascript to update their display. The Javascript is even simpler. You just turn polling on:

DWREngine.setPolling(true);

Chat example (included in the war download) includes the Javascript source to receiveMessages() which is a 4-liner that uses DWRUtil to put the messages on the screen.

Other uses for this technology include progress bars, online games, stock tickers and any system where server state changes and we need to push updates to a browser or browsers.

Cross-Domain Ajax: We now allow script tag remoting to enable cross-domain Ajax. This is in addition to XMLHttpRequest and iframe remoting.

Automatic signatures Element: If you are using DWR 2.0 with JDK5 generics then you can skip the signatures element in dwr.xml. DWR can now work out the correct mapping all for itself.

DWRUtil Updates: The biggest change is to allow template style DOM manipulation. Using cloneNode() you can create a repeated HTML structure from an array of Javascript data.

Other new features: See the release notes for details of the refactoring to the org.directwebremoting package, new script scope for creators and attributes, and the new call meta-data abilities.

For more information see the detailed release notes, or go straight to the download area.

We've got an aggressive list of new features to add to DWR for the upcoming milestones. What would you like us to add?

Comments

Comments have been turned off on old posts