The chat.html file provides the basic layout for the page, including this text and a bunch of
div
elements that define the layout of the chat room. This html will render well
without any javascript etc.
The chat.html file includes the default.js file, which simply includes javascript library of Behaviour.
The chat.html file includes the chat.js script which is the client side smarts of the chat room:
Using Behaviour, the DOM model of chat.html is enhanced with onclick and onkeypress functions to trigger the dynamic interations.
Using XHR, a long polling loop is started for events.
Using XHR, any chat entered is sent to the server.
On the sever side, the ChatFilter is instantiated to filter requests to chat.html:
The ChatFilter extends AjaxFilter, which identifies requests that are AJAX requests and gives assistance with formatting the AJAX responses. All other requests are passed normally.
The ChatFilter provides methods for join, leave, chat and getEvents. Most of these are fairly straight forward and just manipulatet state and send the appopriate AJAX response.
The smarts are in the getEvents method. This method is a poll from the client for any events on the server for the given member. If there were no events and the server simply replied with an empty response, this poll would become a busy poll and consume CPU on both client and server.
To avoid this, normal AJAX servers will simply wait for a short period (eg. 30 seconds). If an event occurs in this time, the response is sent immediately. If the period times out, then an empty response is sent. Either way the client will then poll again for more events.
But for servlets, this waiting for events, is done in the request handler, when a thread has been allocated. Thus it makes it very difficult to scale an AJAX server to thousands of users as you will need thousands of threads - mostly idle waiting for events.
Thus Jetty 6 introduces Continuations, which allows the handling of a servlet request to be suspended until either a timeout occurs or an asynchronous event calls resume on the continuation. This allows threadless waiting and thus permits an AJAX server to scale without consuming vaste numbers of threads.