Archive for the AJAX Category

We have a new release of the GWT/Spring Shine Reference project out here (ver 0.3), which is an important improvement on the previous releases.

  • Now includes proper Data Layer : using Spring 2.5 annotations, Hibernate 3.2 and integrated HSQLDB runtime memory database.
  • Upgraded GWT gui to include save/delete/select functionality linking into the exposed Spring services
  • Maven now builds deployable war files for both the presentation (gwt) and service (spring) layers that can be dropped into a standard Tomcat install.

All of this should “just work” straight out of the box - tested on linux (fedora), mac and windows systems. Follow the “Getting Started” instructions on the project page.

Ruby on Rails comes with a lot of nice helper methods for generating the JavaScript driving the AJAX calls to your controllers. Handling the responses from the HTTP server becomes a snap too, with Rails providing a few simple callbacks to handle the response from the server:

But what if the server doesn’t get a chance to respond at all? What if the user’s browser has been unexpectedly disconnected from the Internet? What if your server - god forbid - has crashed?

Most AJAX applications in the wild will simply sit there forever waiting for a response. Certainly, that’s what happened to us when we first tested such a scenario. This is unacceptable: if the browser is unable to communicate with the server, we need to let the user know somehow.

Internally, Rails uses a cross-platform Javascript library called Prototype to do the heavy lifting when making AJAX calls. When you call a helper method like link_to_remote, Rails generates code to instantiate a Prototype Ajax.Request object (or a Ajax.Updater object in some cases) to make the remote call when a certain event occurs. In the case of link_to_remote, this will be when a user clicks on the generated link.

Many of the helper methods accept a common set of options which provide callbacks for handling success or failure using the following callbacks:

  • :success will be executed if the server responds to the AJAX request with any sort of 200 HTTP OK code.
  • :failure will be executed if the server responds to the AJAX request with anything *but* a 200 code.
  • :complete will be executed after the AJAX request has finished, irrespective of the result.

These callbacks can be mapped to corresponding callbacks in Prototype on a one-to-one basis. For the above callbacks, the corresponding Prototype callbacks are called onSuccess, onFailure and onComplete, respectively. Unfortunately, only the “complete” callback will be triggered if some fatal error occurs when communicating the server - “failure” will only be triggered if we get a response back from the remote host indicating a server-side. We have no way of determining what will happen if the browser is unable to communicate with the server at all.

So how do we detect server outage? Well it just turns out that if your browser is unable to communicate with the server, an exception will be thrown. We can actually use this as a basis for detecting server outages and other classes of errors that might occur browser-side. For this purpose, Prototype provides onException which is triggered by your browser if any sort of exception is thrown while the AJAX call is in progress. If we can tap into this callback, we can ensure our AJAX methods will dutifully report any problems that might occur.

Unfortunately Rails doesn’t yet provide access to this callback out-of-the-box, so there is no “:exception” callback available to the Rails helpers. We don’t want to clutter our templates with ugly JavaScript: we’d prefer to keep with the existing Rails conventions if we can. So what can we do?

PrototypeHelper#build_callbacks is used by remote_function to generate the Javascript responsible for making an AJAX call in helper methods like link_to_remote, form_remote_tag and others. It turns out that this little method exposes virtually all the callbacks available to Prototype *but* onException.

This is the hook we’re looking for: here we override and adapt the original code in PrototypeHelper#build_callbacks to add the following method to our ApplicationHelper:

include ActionView::Helpers::PrototypeHelper # ... def build_callbacks(options) callbacks = {} options.each do |callback, code| name = 'on' + callback.to_s.capitalize if CALLBACKS.include?(callback) callbacks[name] = "function(request){#{code}}" elsif callback == :exception callbacks[name] = "function(request,exception){#{code}}" end end callbacks end

This makes the :exception callback available to Rails using the familiar notation seen for the other types of callback. :exception is somewhat different in that it provides the exception object thrown internally available to your callbacks as the “exception” variable. Outside of that, it’s just like any other callback.

With this new callback available to us, we can then override the AJAX helper methods to include our error handling code in ApplicationHelper. As an example, here’s how we’d do it for form_remote_tag:

alias old_form_remote_tag form_remote_tag def form_remote_tag(options={}, &block) # In the event of a network failure (or some other error), we want to display an error message options[:exception] = (options[:exception] || '') + 'alert("Anerror occured while communicating with the server. Please try again later.");' + # Bubble the javascript exception up to prevent the caller from continuing execution (failure # to do this may result in multiple error messages being displayed!). 'throw exception;' old_form_remote_tag(options, &block) end end

With this code in place, any attempts to submit a form via AJAX will include our special error detection code, so we can rest at ease knowing our users will be notified if something goes horribly wrong at the network or language leve.

When using this technique to check, you have to be quite careful in your JavaScript code. Where any language- or browser-level exception that was thrown during the AJAX request was once ignored, it will be caught by the :exception callback. This means syntax errors in the response Javascript, bad content types (e.g. a content type of “text/javascript” with a body of HTML) or other errors will trigger the :exception callback. Unfortunately there doesn’t seem to be an obvious way to get more information about the nature of the error. For our purposes, however, this seemed to be enough if we were careful with the AJAX responses.

A further improvement to this approach might use a timeout mechanism to provide a hard limit for AJAX requests: if the server doesn’t respond with data within X seconds, abort the request and display an error. This would remove the reliance on the browser reporting errors via an exception, resulting in somewhat more robust code - the feedback may not be as immediate as if one were using exceptions, however.

Don’t leave your users out in the dark: keep them informed if things go wrong with an AJAX request.

Once in a while new technologies mature and coalesce together to provide a new and viable platform that radically change the traditional development landscape. In recent years Ruby on Rails has been the poster child of all that is new and groovy; the “must learn” skill for developers, leaving behind those “legacy” Java systems built over the last ten years.

It looks like GWT (Google Web Toolkit) is challenging those ideas. We’ve written an article explaining the steps involved putting together a multi-tiered system using GWT and Spring together (with both layers built by Maven). This also comes with an Open Source reference project showing in detail how to get up and running very quickly. Full details here.

What makes me particularly happy about all this is , is that it looks like there is still life in the old Java dog yet :)

Recently we completed a very interesting project with one of our major clients to develop a Firefox Plugin to give an alternative channel for information for the clients members. Some more details on the Press Release and here is a link to the actual plugin on the Flybuys Website.

During development process and promotion into production a number of points became very clear to us

  • Its Easy! - Fundamentally writing plugins / widgets etc is very straightforward, with lots of documentation and samples available on developer forums and websites. If you are comfortable with a bit of Javascript and a small amount of AJAX then you should have no problems getting a plugin up and running in a short amount of time
  • Its Only Easy on a good architecture… - We were in a fortunate position to be building upon a modern architecture designed by ourselves which had all the important attributes like security and clean interfaces already available to us. The system already has interfaces in place for IVR, WebServices, Public Website and so piggy backing on top of these ensured that the project only ran for weeks and not months
  • Multiplatform - The pluging has been sucessfully tested on Linux, Mac and Windows - and the great thing is that this is a single build that works unchanged across all platforms as it relies on all the hard lifting being performed by the Mozilla community and the Firefox platform. And as predominantly an Enterprise Java Company its interesting there isnt a single line of Java code…
  • Its Fun.The plugin has created alot of interest and sparked more ideas about how best to maintain relationships with your clients. For too long large institutions have relied upon single channels of communication - usually a single monolithic website - but the landscape is changing with channels such as Mobile, Interactive TV, Blogs, Forums etc becoming the norm for an increasingly savvy internet generation. We had a great time working with the marketing department on this one and are looking forward to version 2!
  • In summary, we are very fortunate to be working with such a forward thinking organisation who are willing to try new technologies and different approaches to their members. Using this and building upon a solid architecture with a bit of creative thinking has led us to a product which hopefully is useful to thousands of Flybuys members.

Segway Gran PrixIn a common theme from JavaOne, it appears you need to be spruiking a product to get a gig as a presenter, so I was almost lulled into a false sense of security when I went to a session on AJAX performance.  The presenter went through some interesting aspects of AJAX performance, and I will highlight them here.

Reducing Start-up Time
The main aspect for AJAX is the size of the JavaScript file.  There was a discussion about how to reduce the size via a number of methods such as combining your files into a single file since this is more efficient (fewer HTTP requests which are expensive).  You can do this by creating a servlet that combines you different files on the fly, or use the development environment to build the combined file.  The Dojo toolkit was recommended as a way to do the build time version, since it can handles issues like script dependencies for you.

In addition, you can reduce start-up time by making the file smaller by removing white space and comments.  There are some tools that can do this (Dojo, safe compress), with the major caveat that you need to ensure all lines end with a semicolon since these tools remove the carriage return and some script relies on carriage return to indicate the end of the line.

Finally, if the file can be gzip’d then this will also reduce the start-up time.  Most newer browsers can support this (HTTP 1.1 with stated gzip support), but you will probably need some client and server script to detect the browser and see if you can provide the compressed version.

(He also suggested distracting the user with flashy graphics, but I don’t think that is a technical approach -  leave that to the Project Manager to suggest)

Reducing Run Time
There were a few technical hints to speed up actual code (such as using native DOM parsing in the browser, that string comparisons in IE are 4 times slower than Firefox) but at this point the presentation mostly turned into the product spiel for jsLex (darn!  another spruik) which will help you run tests against your code and identify poor performance areas.  This seems to be pretty helpful, and is probably worth a look if you need to optimise AJAX code.
Summary

Overall, it was interesting the the combination and compression of the JavaScript files seemed to be very important.  After that it comes down to ‘normal’ code optimisation and in that situation a tool is always going to make that easier.

The main beanbag areaThe AJAX Push session was interesting, although right now I can’t see too many situations where Shine would need this kind of technology. Let’s put this in perspective with the decision tree for even thinking about it:

  • your application is a web application
  • your application uses AJAX
  • your application has events that can happen on the server
  • events on the server need to be communicated to the user in near real time
  • the user has a browser open all the time simple polling from the client to the server is either too slow (you can’t poll fast enough to meet the latency requirement)
  • simple polling places too much load on your server and you can’t just scale via hardware

So, as you can see there aren’t too many situations left! In reality only sites like Google Mail, GTalk and Meebo definitely need it due to their massive number of users and equally massive budgets.

Still, the key point is that there are two main approaches, the ‘long poll’ or ’streaming’. Since a server cannot arbitrarily call a particular browser, the term ‘push’ is a little misleading. The ‘long poll’ implements an approach where the browser puts in a call to the server when it loads, then the server responds whenever it likes. After the response the client starts a new call back and again waits until the server has something to send. This is a simple request - wait - response cycle. In ’streaming’ the client again opens a request, but the server just responds with chunks of data down the stream and only closes when the session ends.

Long story, but really only the ‘long poll’ is practical. There are enough technical difficulties with streaming that I can’t see how it will practically work (small example: IE doesn’t stream, so it won’t work).

Overall, interesting to see how it is done but as I mentioned at the start I am not holding my breath for a commercial opportunity where Shine would need to use it.

banner.JPGDay two at JavaOne and I’ve been amazed at how many times presenters (including many Sun employees) have advocated Ruby on Rails during their presentations, be it running on JRuby or the native C Ruby implementation.

The thing that I’m curious about is if and how this support fits in with JSF and jMaki. Are Sun intending that JRuby on Rails be a lightweight alternative for building small apps, whilst JSF and JMaki are for the big boys? Or are they just going to sit back and see what happens? I think it’s a good idea that they’ve embraced JRuby, but their support is certainly low-key compared to what they’ve done for JSF in the past. It’s almost like they’re hedging in case JSF and jMaki fail.

Not that this is a bad thing. It’s not like JSF has taken off. I’ve always been interested in what it was theoretically capable of, but frankly it wasn’t until ‘JSF in Action‘ came out that I was actually able to understand it. The same can be said of jMaki: in theory it’s seemed like a promising integration of JSF and Ajax. But when Mark attended an introductory session on jMaki today he came away disappointed. For at this point in time, when it comes to building a rich Ajax interface, jMaki comes up short compared to RJS templates or GWT.

California FlagThe Google Web Toolkit (GWT) session today was very interesting. Without personal experience using it I am not going to comment on it too much, but there were some interesting things that came out of the session.

Firstly, the GWT guys outlined the fundamentals behind their approach. It was interesting to see that their focus is on the end user experience, and for them that means speed and reliability. They are happy that they don’t have the most ‘gee whiz’ widgets if in any way they are not fast and cross browser compatible. To a certain extent, their compromise is to the ‘lowest common denominator’ however with the work they are putting in I am not sure that there are too many compromises. They are also big fans of static compilation. Not just because they like it, but because it allows them to better optimise the code produced. For them the optimisation allows even smaller downloads and faster execution.

The other interesting thing was their focus on the compilation process. For those that don’t know in GWT you write Java code (very Swing like) and the GWT compiler actually creates all the JavaScript you need. What I didn’t know is that it creates an include file specific for each browser and language in order to keep the client download size small. Also, in a recent release they have adopted a pretty funky image optimisation where all images in the view are merged into a single image. The code then ‘windows’ this large image to only show what it is supposed to. Why do this? Well according to them HTTP requests are expensive in terms of performance. By downloading one file once the browser is likely to cache this file well and subsequent visits or AJAX requests never have to download an image again. Sweet.

Another interesting point was their focus on testing. There is now a tool that allows the code to be deployed out to a number of machines and browsers to test performance.

They also talked up the changes in the last year, particularly in performance. Like the image compression above, they have done several other changes to create even more efficient code. They claimed performance improvements of between 30% and 80% over the same code of the year prior. Best part is that no changes were required in the application code, it was all achieved through better compilation techniques. Just get the latest version, re-compile and you are done! They think there is still ‘lots more low hanging fruit’, so it will be interesting to see how much faster it can be.

In their own words, they believe GWT is software engineering for AJAX. In the Java space it certainly seems like a compelling story and one well worth further investigation.