DWR with “Reverse AJAX”

In a pre­vi­ous ar­ti­cle I al­ready men­tioned Di­rect Web Re­mot­ing (DWR), the best AJAX-li­brary I en­coun­tered up to now. I just had the time to re-dis­cover it, as I sought some­thing to get back to my Java skills. This li­brary re­ally keeps one promise: it re­ally starts mak­ing fun to de­velop web-ap­pli­ca­tions with AJAX. A short sum­mary: DWR pro­vides a very easy means for call­ing server-side meth­ods which are hosted in a Java Servlet Con­tainer like Tom­cat or Jetty. It masks the com­mu­ni­ca­tion ef­fort by pro­vid­ing client-side ab­strac­tion to the method calls of ex­ported Java meth­ods, so re­mote calls look local to the de­vel­oper.

The new ver­sion 2.0 (re­lease-can­di­date) in­tro­duces a new con­cept, which they call “re­verse AJAX“. Re­verse AJAX al­lows the server to asyn­chro­nously send data to its clients. It works by ei­ther polling, per­sis­tent con­nec­tion or ad­di­tional in­for­ma­tion on your next AJAX call. It means, that the server at any time can push data to the client, with­out the client’s re­quest. In “per­sis­tent con­nec­tion” this works al­most “real-time”, while polling takes at least a poll-in­ter­val to no­tice the new in­for­ma­tion and ad­di­tion­ally in­creases load on the server.

This fea­ture al­lows highly in­ter­ac­tive ap­pli­ca­tions where new in­for­ma­tion can be pub­lished right away, in best case with­out no­tice­able delay. De­vel­op­ing a sim­ple chat-ap­pli­ca­tion be­comes the mat­ter of just a few lines of server-side code.

For an ex­am­ple of what you can eas­ily do now, look at this ex­am­ple (fake) stock ticker, which up­dates the data on the client with­out the client being re­quired to poll for changes. Also note the sim­ple, yet cool, in­clu­sion of script.​aculo.​us ef­fects dur­ing the up­date. (Hint: you need to move the mouse over the table for the ap­pli­ca­tion to start.)

The down­side is, the fea­ture is still in an early stage of de­vel­op­ment, it still has some prob­lems. On my Gen­too Linux box, I can re­pro­ducibly in­crease my CPU load to 100% by du­pli­cat­ing a tab with an open per­sis­tent con­nec­tion in Opera. In Fire­fox, du­pli­cat­ing the tab leads to nor­mal AJAX calls to no longer func­tion with about 50% chance. If Fire­fox en­ters this mode, it will queue up XMLHttpRequest caus­ing a “nor­mal” call to be queued be­hind the per­sis­tent call which will be kept open for around 60 sec­onds if no data is pushed from the server or even longer, if there is data. This of course makes it al­most un­us­able at the mo­ment, un­less you can be sure, users won’t hit your site twice within the same ses­sion and/or are not using tab-du­pli­ca­tion.

I’m re­ally look­ing for­ward to this fea­ture be­com­ing sta­ble, this is so cool and you can do so much awe­some stuff with it. Thanks to all de­vel­op­ers for their great work!

2 thoughts on “DWR with “Reverse AJAX””

Leave a Reply to Martin Carpella Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.