The key to build good rich internet applications (RIAs) is zero lag connectivity to the server and the ability to get realtime data from server as and when needed. The request and response architecture of the HTTP protocol prevents the web applications running on browsers to achieve that same degree of user experience that native desktop applications enjoy. In an ideal condition the server should be able to push data to client through a persistent connection. To address the connectivity issue of web apps a good number of techniques were employed like the Comet model which involves using long polling (asynchronous polling) and quite a few browser plugins like java applets, flash and silverlight.
What is a Websocket ?
Websocket is an exciting new feature in HTML5 which allows bi-directional (duplex) communication over a single TCP socket connection. In simple words the web application client can maintain an always open connection with the server and data can be sent and received at the same time. Exciting isn’t it !
It runs via port 80/443 (will work even if the user is behind a firewall or using a proxy), the tcp handshakes are HTTP compatible and it also integrates with cookie based authentication. The message headers have been kept small (only 2 bytes overhead per frame) and latency from establishing new connection every time as in HTTP have been dealt with by using a single persistent connection. All these result in impressive network throughput. Apart from that Websockets API is much simpler to code than the XmlHttpRequest(). Bottomline, you can make the process of web application development more responsive and interactive with lesser effort.
Sounds cool! How to make this work?
You have to get the Websocket server working first. There are a various websocket server implementation available:
mod_pywebsocket (Apache extension)
Socket IO-node (node.js)
They will work alongside the http web server. Grab any of these and fire them up. You might have to set the listening port in the configuration and you will have your server ready.
Currently all browsers do not support the Websockets API. List of browsers that support them are:
- Chrome 4.0
- Firefox 4.0 beta
- Opera 11
- Safari 5.0.3
- IE9 will support websockets (Microsoft claims)
For other browsers it’s not so clear but support will come as major browsers are already in line.
Overall things appear exciting as web applications will become realtime and allow better resource utilization allowing developers to build interactive and responsive applications.