<< Ajax in Content Management Systems | Home | The case of James Gosling and the missing Javascript Debuggers >>

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?

Tags :


Re: DWR 2.0 milestone 1 does Reverse Ajax

Looking good Joe - Keep it up!

Just gave it a try...

I said I would give it a try - ok, I register all "user" (User persistence Object) and there script session in a EJB component.

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

How do you get the return value for the javascript function you call? (i.e. if receiveMessages() returns something )
How do you switch between polling and Comet?
Thanks

Re: DWR 2.0 milestone 1 does Reverse Ajax

Right now you return values are not handled automatically - there is a slightly manual solution where you arrange for your Javascript to call a remoted method with the reply. You can change the method currently using the following: DWREngine._pollComet = true; We'll have a better method for this in the next release. Joe.

Re: DWR 2.0 milestone 1 does Reverse Ajax

YEAH - i just suggested implementing "Reverse Ajax" for a project using DWR - you guy's just saved me hours of coding! Proposed it about 2 weeks ago, but other things had a higher priority.

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

i tested my application with DWR 1.1 to DWR 2.0 milestone 1, and test the "comet", it seems there's some bug in M$ IE, it doesn't poll, but ran perfectly in Firefox.

Re: DWR 2.0 milestone 1 does Reverse Ajax

The for loop looks like it could be refactored into the DWR code. I would be nice to have the Reverse Ajax API be something like this:
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

Re: DWR 2.0 milestone 1 does Reverse Ajax

We do have some ideas, but you'll have to wait until milestone 2 for them!

Re: DWR 2.0 milestone 1 does Reverse Ajax

There's some problem with M$ IE, the status bar displays it refreshes every 20 seconds (is it iframe or something??).

Re: DWR 2.0 milestone 1 does Reverse Ajax

Now I'm sure that is an iframe. when I use it with other js libraries, like jquery, The biggest problem is that when the status bar displays: "Done", instead of: "Opening ......", the IE will definitely delay 1 script the scriptSession send out. Then if the scriptSession send out another(the next) script to the browser, the browser will receive 2 scripts, the delayed previous one and the right-time one. Every 20 seconds it happens. Is it the "fairly serious IE hole" you've mentioned? For the moment my way is to test poll connection every time the iframe reloads. Hope you can finally fix this problem. Appreciate for your excellent work.

Re: DWR 2.0 milestone 1 does Reverse Ajax

This is very cool - and a nice compliment to an awesome framework. So, I have a question. Is there a clean way to do a push that's not initiated through an AJAX call? In other words, I can currently only get push working from browser to browsers - because of the necessary Container setup/management that DwrServlet does. What I'm trying to do is receive a web service call and push a message programmactically to the listeners. I see that the DwrServlet/DwrController sets up a WebContextBuilder for each request - so it's not available when I try to get a WebContext outside of that servlet. Thanks for any info.

Re: DWR 2.0 milestone 1 does Reverse Ajax

Nevermind - I found the same problem being addressed in a more appropriate forum: https://dwr.dev.java.net/servlets/BrowseList?list=users&by=thread&from=454974