"Pushing" the web

published on September 6th, 2008 · more from the blog →

So Recently I've been delving into the marvelous world of Pushing data to web browsers.

Ok, before I get flamed, what I mean by pushing (for now) is the browser requesting data and the server sending new events periodically as they happen. Other technologies in this area are things like Comet and Orbited. Many people are already using various methods to implement this, services such as Mibbit, GMail/GDocs, Facebook, Highrise and others.

While this isn't a discussion about why I didn't use the existing approaches, I will say some of them are because the servers are based on Java or Twisted (python), which is bulky and I felt it could be simplified.

For now the general thought process is that the clients web browser requests data from the server via AJAX or JSONP, Flash or an IFrame, and when the server has some data to send, it plops it out, the web browser reads it and everyone is happy...

Limitations of these methods...

To do anything dynamic in a web page you NEED to use Javascript. Javascript is great and is being sped up in browsers with every new release, however, it has a few built-in limitations to protect a user from security issues.

XSS or Cross Site Scripting - Javascript inherently can't cross domains. What does this mean for us? Well the page the server connects to has to be on the same domain AND port number, the domain problem can be overcome using a subdomain and setting the document domain, but that is buggy and won't work everywhere, and the port issue, probably can be overcome somehow but I haven't pursued this fully.

Ajax - This is the method Highrise currently uses, they keep polling the server for data.
Pros:

  • Really Simple

Cons:
  • Not real time
  • Lots of requests to server

IFrame - You embed an iframe into a web page, you can then fire off requests to the parent frame as soon as they arrive, great!
Pros:

  • It works

Cons:
  • Must use same(ish) domain and port.
  • Limited concurrent browser connections, again can be overcome by using subdomains

JSONP - You use script tags to call a script on ANY domain, AWESOME!
Pros:

  • Fabulous, cross domain, the script can return a function call with the new data

Cons:
  • You have to close the connection for each message from the server, meaning you could miss messages whilst reconnecting

Flash - XMLRequest in Flash can open a connection and have 2 way communication, Javascript can talk to it, great!
Pros:

  • It actually works in most situations (GMail uses it for chat)

Cons:
  • Requires 3rd party application
  • Wont work on mobile devices or browsers that don't have support for Flash

I believe in the spec for HTML 5 there is the ability to have this kind of activity built in to browsers so all this data will soon (whenever "soon" is, we'll deal with that when it arrives) become obsolete.

As a conclusion, I wish I had one, but I don't know what to do! I've wrote my own simple socket / web server using Python and it works great, but the browser side is the real issue i'm having, comments and suggestions would be great!