Wednesday, April 12th, 2006

DWR 2.0: Reverse Ajax

Category: Java, JavaScript, Library

<p>Joe Walker of DWR fame writes in:

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.

The big new feature is 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. 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);

Other new features include cross-domain Ajax, an automatic signatures element for JDK5 generics and a bunch of other changes. For more information see the detailed release notes, or go straight to the download area

Related Content:

  • Using DWR with TIBCO General Interface
    This simple request/response cycle example demonstrates some of the basic utilities Direct Web Remoting (DWR) provides for making interactions between...
  • Critical flaw in Web 2.0, AJAX
    Fortify Software warns in a new report that digital outlaws could use JavaScript to snatch data from Web 2.0 and AJAX-based applications if they're...
  • Ajax Learning Guide
    Chances are, you've been doing JavaScript and XML developer work in Lotus Domino for quite some time. This old/new approach is causing quite a stir in...
  • Ajax Learning Guide
    Are you a Web developer? The time has come to rethink your entire approach to developing Web applications. Find out about the Ajax approach...
  • Ajax security concerns you need to be aware of
    As developers implement Ajax and Web 2.0 applications, they need to understand security issues such as cross-site scripting (XSS), cross-site request...

Posted by Dion Almaer at 9:10 am
15 Comments

+++--
3.9 rating from 43 votes

15 Comments »

Comments feed TrackBack URI

Maybe I am just not understanding this concept right. But I do not see how this can be called “Reverse AJAX” when all it seems to do is have the client javascript keep calling the server, so that when the server has a new update the client can receieve it.

If thats all this does then I don’t really think its “Reverse AJAX.” Its just a client-server events system, but like everything else in the web 2.0 world we have to give this an improper name so that way it seems revolutionary or cool.

Comment by pplante — April 12, 2006

I would think that the only way to do “Reverse AJAX” is to use a socket connection back to the server. No?

Comment by Phill Kenoyer — April 12, 2006

pplante — you aren’t quite getting it right. Their system keeps a socket open indefinately. Think of each client as slowly loading content from the server and displaying it progressively. Except that the data can trickel in over hours. Then you have a server process-to-process communication so that all the other processes that you might want to notify start downloading a more information. This won’t work with PHP, etc., since they don’t have process-to-process communication, and it won’t work with HttpXmlObject (as far as I know, since most don’t implement multi-part-mime needed to make this work), but it will work with Java and Iframes.

Comment by Steven Roussey — April 12, 2006

I’m using sockets for my realtime systems. Currently I’m using Flash8 (AFLAX). What I’m running into is that a lot of MSIE users can’t get Flash 8 to install. About 1 out of 5 right now. I tried some Java applets to do sockets, but I couldn’t get that to work across platforms too well (again MSIE).

Comment by Phill Kenoyer — April 12, 2006

[...] I see that a new version of DWR has been released. And it sounds very familiar for some reason. [...]

Pingback by Neil Mix » Blog Archive » Asynch updates == execute JavaScript from your server — April 12, 2006

I’m using sockets for my realtime systems. Currently I’m using Flash8 (AFLAX). What I’m running into is that a lot of MSIE users can’t get Flash 8 to install. About 1 out of 5 right now. I tried some Java applets to do sockets, but I couldn’t get that to work across platforms too well (again MSIE).

Developing and deploying eLearning encounters the same types of hurdles. You *want* to use the latest and greatest technology but some of your audience just doesn’t have it…nor are they able to install it. The ‘live’ connection between the eLearning course and the server is typically through a java applet that has an open socket connection to the server and a javascript API that makes calls to the applet for storing/retrieving information. The trouble is the JRE. If the user has a version that’s too old, they have to download and install a new one, etc etc.

I feel your pain….

Comment by Jon — April 13, 2006

To pplante – As mentioned in this post, both polling and Comet are sopported. If you use the polling mode, it means you keep requesting server at some interval. That’s the way you said like everything in the web2.0 done. But if you use Comet mode, then it’s like server-push technique. Please refer to Comet

Comment by HU YIQUN — April 13, 2006

So… “the process of using either polling or a long lived connection (Comet)”… why the need to rename it as “Reverse AJAX” other than the cool sound of it? ;)

Comment by Gonzalo — April 16, 2006

A new concept – REVERSE AJAX

新しいコンセプトを勉強した。

Reverse Ajax
Comet Queryies

Reverse Ajaxの意味はAjaxianからの説明を見れば分かると思う。
The big new feature is Reverse Ajax: DWR 1.x allowed you to asynchronously call Java code from Java…

Trackback by ラクラク日記 — April 26, 2006

Has anybody got this working? If yes, do you use “polling” or “comet”
setting? I don’t see any documentation on usage of these except for Chat example which uses “polling” setting. As per my experience, it sometimes work, sometimes doesn’t – pretty unreliable.

So far, I have not been able to get it working with “_pollComet”. Any body had better luck than me? Pls let me know if so.

Comment by Deepak — April 27, 2006

Links: 5-17-2006

Ajaxian » DWR 2.0: Reverse Ajax Prediction: reverse ajax (I prefer rajax) will be big… and hard on servers. (categories: ajax rajax )…

Trackback by Aaron Johnson — May 18, 2006

It says DWR2.0 uses Comet as default, but when I test the Chat demo I find that it also polls the server although at a relative long interval. I am not very clear about how DWR implement the Comet mechanism, is Comet means a long interval polling or a tradeoff between polling and long-lived http connection?

Comment by Irving Lv — September 3, 2006

[...] http://ajaxian.com/archives/dwr-20-reverse-ajax [...]

Pingback by Techhawking » Blog Archive » The RAJAX framework (Reverse AJAX) — November 19, 2006

so..can the dwr provide me with the true push just like lightstreamer?i don’t want my client keep requesting my server for new updates

Comment by newbie — November 22, 2006

@newbie: Try StreamHub Reverse Ajax Server – it uses true push.

Comment by CometDude — August 20, 2009

Leave a comment

You must be logged in to post a comment.