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?
Just gave it a try...
Seems to work, but I can't specify a sync time, in my case I am doing a series of DB request and would like to block DWR messeges until all queries finished and flush everthing afterwards. Is there any way to do this? Reverse Ajax appears to be somehow magically...
Oh: If you need to access the DWR WebContext from your EJB's (beause you want to send JavaScript code within a code block that is not "war"-aware, in my case some ejb code) you must move the DWR library into your .ear file, add it to your application.xml and remove it from your WEB-INF/lib. Otherwise you will get some ugly class not found exceptions.
Anyway great stuff!
Re: DWR 2.0 milestone 1 does Reverse Ajax
Re: DWR 2.0 milestone 1 does Reverse Ajax
Thanks! Thanks! Thanks!
We will switch to dwr 2.0 before the end of this month.
Almost forgotten: Thank you, but could you please add a link to the DWR 2.0 release notes on the download page? It took me some time to find it...
Re: DWR 2.0 milestone 1 does Reverse Ajax
WebContext wctx = WebContextFactory.get();
while( moreMessagesComing ){
// block (using Object.wait() ) until we get notified
messagesMonitor.wait();
//execJavascript( function, argument )
wctx.execJavascript( "receiveMessages", messages );
}
The for loop and outbound variable stuff would be taken care of in DWR.
What do you think? Is this API possible? Is it desirable?
Thanks