When you are using comet forever iframe techniques, you will see the browser throbber of doom keep spinning and showing progress indicator all the time while loading the iframe data. It is the same in IE, firefox, or webkit based browsers like chrome and safari. In order to overcome the problem, you have to handle differently for different browsers.

Firstly, let’s look at how comet was implemented usually.

Normally, in comet iframe, you will define some call back javascript functions in the parent windows, this are javascript functions that are going to be used to update the contents on the web pages when data are pushed from server. On the server side, the server will push html that contains javascript functions calls back to the client. E.g.

Client Side javascript:





In the comet.js:

var iframe = document.getElementById(this.iframe_id);
if(!iframe)
{
     iframe = document.createElement("iframe");
     iframe.name = iframe.id = "streaming_frame";
     document.body.appendChild(iframe);
}
iframe.style.display = "none";
iframe.src = "http://server/action=subscribe&channel=time"; 

Once the iframe src has been added, the server client communication has started.

On the server side, there are quite a number of open source package for you to choose.grizzly in glassfish, Jetty, Meteor, Node.JS, just to name a few. The example below assumes that you use glassfish as the server:

PrintWriter writer = response.getWriter();
writer.print("");
writer.flush();

Here we write the following line of javascript back to iframe before we push any other data.

function p(data, div_id){parent.p(data, div_id);}

The reason is that when later the data is pushed to the client, the content of the data can be written as

p("10:50pm", "time")

But actually, p is referring to the parent windows call back method p.

Alright, so this is how basically comet works. The following are hacks to different browsers to stop the browser from spinning.
1. Firefox
In firefox, before you trigger the call back method to update the content, you have to create a temp iframe and attach to the document body, then removes it. By keep doing this before you trigger the event, the browser throbber goes away. The modified p function looks like this:

p(data, div_id) {
  firefox_hack();
  document.getElementById(div_id).innerHTML = data;
}
function firefox_hack () {
    var state = pushPage.engine.state;
    if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
          var fake_iframe;
          if (fake_iframe == null){
              fake_iframe = document.createElement('iframe');
              fake_iframe.style.display = 'none';
          }
          document.body.appendChild(fake_iframe);
          document.body.removeChild(fake_iframe);
    }
}

Read the rest of this entry »

If you want to write a chatting application or an application showing the real time stock market price to the user. How would you do that? There are a few ways to achieve that.

1. Pull Technology
This kind of method is called pull technology because at the client side, you will continuously send request to the server to check new data. You can use Ajax to send a XMLHttpRequest to the server, update the page content once the request is complete and at the same time, you can set a timeout to send the XMLHttpRequest again. So it is a forever loop. How fast you update the content on the web page depends on the timeout you set.

However, there are quite a number of drawbacks for using this method.

Firstly, the client side javascript would be busy keep sending request and the server has to keep responding to the request. If there are too many users connecting to the server, the server will be unable to handle such heavy load.
Secondly, everytime, the XMLHttpRequest is completed, you have to update the page because the server will return the updated data. Imagine that some of the data is not updated and is the same as previous data, you will still have to update the data. It will take up quite large bandwidth.

2. Push Technology
This is also called comet technology. It is a neologism to describe a web application model in which a long-held HTTP request allows a web server to push data to a browser without browser explicitly requesting it. The followings are specific techniques that can achieve this:

Long Live (Forever Iframe)
It is called hidden/forever iframe because once the browser (using javascript) send a HttpRequest to the server, the server will hold the connection and at the client side, you have to use javascript to generate a hidden iframe and attach to the html body. Once the server has new data available, it will send the data back to the hidden iframe, typically those data are javascript function calls. So once the server update the iframe those javascript function, those javascript function will update those data that needs to be updated.

Long Poll

Long polling is a variation of the traditional polling technique and allows to emulate information push from a server to a client. The client send request to the server in a similiar way as long live. The server will hold the connection and only response when new data is available. Once the client receive the request it will close the connection and resend the request. Long polling is itself not a push technology, but can be used under circumstances where a real push is not possible.

Check out the lightstreamer stock price streaming example here. Lightstreamer is a very powerful example of implementation of comet technology.

If you knows how to use firebug, you will see that the browser sends a persistent connection with the server. The time of the request is keep increasing. And if you go and view the source code, you will see that there is a hidden iframe. Below is the screen shot of the firebug.

click on the image to view full size image

Read the rest of this entry »