Exploring Reverse AJAX

Ajax dramatically improved the usability and interactivity of the web and the hype still goes on. But there is a new technology called Reverse Ajax, which shows a lot of promise in taking that interactivity further.
Ajax allows the user to have an almost seamless user interface, improving usability and interactivity. Reverse Ajax goes further and allows web pages to have real-time content, attracting more users and retaining their attention. Reverse Ajax will possibly open up areas of website development which up to now have not been possible.

Firstly, an introduction to Ajax
There was a time on the Internet when you clicked a button and waited a second or two while the page loaded. It was all a bit slow and stilted, and web applications most certainly did not have the speed or usability of desktop based software. Every time the user clicked they ended up staring at a blank screen for a couple of seconds. Not good.

Then along came AJAX and sped it all up. Asynchronous Javascript And Xml (Ajax) allows users to see a much more seamless web application, almost on a counterpart to desktop speed. But Ajax alone is not the total solution. We still cannot get real-time information arriving instantly into our browser.

The one major problem with Ajax is that the user still has to initiate contact. i.e. the application/website is only doing actions in response to the user. At no point does the server decide to do something and update the user automatically. This is because servers cannot initiate contact with browsers.

The whole process of asking for and getting a web page, or indeed any other web resource, can be seen more easily in the following diagrams (in the style of Jesse James Garret's original Ajax explanation).

As you can see the client transmits data to the server when the user requests it. While the server is processing and trying to return a new fresh web page to the user, the user is left with a blank screen and a 'still loading' icon in the status bar which is shown in our diagram by the horrible breaks in the user activity line
. Ajax gets around this by using Javascript to handle all the background processes, and allows us to have no breaks in the user activity line, translating into a smoother and more aesthetic user experience.
As can be seen, Ajax allows for an altogether smoother experience. It allows client side processes (Javascript functions embedded in the web page) to capture all the user activity and then pass back relevant information to be processed in the background by the server. But notice that again, just like the classic synchronous web application model, all the server processing is done at the request of the user.



An introduction to Reverse Ajax
Despite all the hype, Ajax cannot get real time information directly from the server. If some information arrives after the user has loaded a page they will never see it, unless they refresh the page.

We could easily solve this problem in the traditional way, by having a having a little bit of Javascript in the webpage that automatically refreshes the page every few seconds. But refreshing an entire page every few seconds is overkill. If the millions of users of a large site were to refresh every few seconds it would increase the bandwidth dramatically. The effect on the servers would be similar to a DOS attack!

We really just need the small piece of real time data to be updated on the browser as soon as it arrives in the server. The simplest way would be to have the server tell all the browsers that the data has been updated. But as we discussed in the introduction to Ajax, servers cannot initiate contact with browsers. Hence we need a solution which gets around this problem.



Welcome to Reverse Ajax
Like DHTML, AJAX, LAMP and SPA, Reverse AJAX is not a technology in itself, but a term that refers to the use of a group of technologies together. The term, as Prokata explains, was coined by DWR, the Java open source library. Reverse Ajax is composed of existing technologies for pushing data, which have been used before in other areas of website design. By implementing these technologies in Ajax we have created Reverse Ajax. Reverse Ajax allows servers to push content out to browsers immediately it becomes available. The following is a brief introduction to each of the three technologies.



Polling
Polling is the repeated querying of the server by a client. In an Ajax environment on refreshing only as small section will have to be updated, and not the whole page. Polling could easily be implemented by having a javascript function called by a timer every few seconds. When the function is called it queries (polls) the server.

The above diagram shows the same client-server event model as I used for explaining Ajax, but it also has a "Continuous Server Application". This is meant to represent the continuous process of real-time activity on the server, for example stock market prices being updated or the latest football scores arriving etc.. When real time information arrives on the server it raises an 'event', which is passed to the 'Server Processing'. Server Processing is the area where client requests are handled, processed, by the server.

As you can see from the above diagram, once the page is loaded, the client side javascript polls the server every few seconds. This repetitive polling is shown on the diagram as dashed lines. If the server has no new information it returns an empty response, shown by the returning dashed lines. If the server does have some new data, for example the real time data coming from the "Continuous Server Application", it is stored until the next time a client polls it.

When the next poll occurs its response returns data, shown by the solid returning line, and passes the content up to the client. An interesting point to note, if the client does actually makes some input, then this can be passed to the server as an Ajax application normally would. Shown on the right part of the diagram.

The problem with this is it requires the client to constantly hassle the server every few seconds, and the problem would only grow if millions of users were using the site. The server would have to handle millions of requests from millions of users every few seconds, on top of the normal traffic it would handle. This could lead to overloading on the server and waste valuable bandwidth.



Comet
Apparently Comet is a really old technology for constantly updating a webpage. The technology requires that the connection between the client and server is never closed. Once the client has requested the web page the server returns the data as slowly as possible, trying to maintain an open connection for as long as possible. It is kind of like phoning up and being put on hold, you are still connected but not very much actual talking is going on.

But how does it work if the page is never loaded? Surely a server which never returns a requested page is failing as a server? Well, you would be right. The trick to Comet is to return a page with an IFrame in it. The page is returned as normal, but the contents of the IFrame are returned as slow as possible. The IFrame is hidden from the user, so the user does not notice any difference from a regular website.

In this way the user gets a normal page and a continuously open connection. It is down this connection that the server can send data to the browser, this data can be handled by Javascript on the normally loaded page.

The above diagram shows what can be done with the permanently open connection, all continuously update server side events can be passed up to the browser. There is far more information on Comet on Alex Russell's blog.



Piggyback
The final of the three technologies behind Reverse Ajax is piggyback. As the name suggests, it is the piggy backing of new data on top of an unrelated response to a client's request. When new information arrives at the server it is stored until the client next makes a request to the server. When the server replies to the request it also adds all the new information which was updated.
The above diagram shows piggybacking in the context of a classic web application model for simplicity, but for reverse Ajax it should have another client side Javascript layer. This technique is not as popular as Polling or Comet as it requires the user to do some activity before it can be updated. If the user is doing a time intensive task, such as reading a blog, they may not do any activity for a long time and so the web page would not be updated at the exact time the new content became available.



Ajax and Reverse Ajax - Putting It All Together
Ajax allows the user to have an almost seamless user interface, improving usability and interactivity. Reverse Ajax allows users to have real time content, attracting users and retaining their attention. One final diagram shows the two working together, using the Comet technology to provide Reverse Ajax.
Walking through the diagram from left to right, we see the dashed line representing the initial request for the page by the client to the server. This opens up the Comet event bus, allowing server side events to travel up to the client and update the browser UI. The client can also provide Ajax events, two examples of the 'event' and 'display' are shown. The second also shows the sending of information through to the "Continuous server application" - this itself is real time information which could be used to update other browsers.

Further Reading
Beyond Ajax
Changing the Web Paradigm
Comet Wikipedia Article
Reverse Ajax Wikipedia Article (initially created by this author)
Reverse Ajax on Ajaxian
Trackback URL : http://apollo89.com/blog/trackback/348
openclose