14 December 2009

Socket To Me - The Web Socket Primer


Web sockets are an alternative to Ajax, Comet, and Flash for an interactive user experience and RIAs. The web sockets specification is an HTML5 initiative.

The spec introduced a web socket javascript interface that defines a two-way, single socket connection for messages to be sent between browser and server.

The intent of the web socket standard is to make bi-directional web communication simpler and more efficient.

Among others, Google's Ian Hickson is working on an internet-draft of the the Web Socket Protocol. Following is the abstract of the internet-draft of The Web Socket Protocol:

Abstract
The Web Sockets protocol enables two-way communication between a user agent running untrusted code running in a controlled environment to a remote host that has opted-in to communications from that code. The security model used for this is the Origin-based security model commonly used by Web browsers. The protocol consists of an initial handshake followed by basic message framing, layered over TCP. The goal of this technology is to provide a mechanism for browser-based applications that need two-way communication with servers that does not rely on opening multiple HTTP connections (e.g. using XMLHttpRequest or iframes and long polling).

Anticipated Advantages

The anticipated advantages of web sockets over the use of Ajax or Comet are:

  • Single Connection -- Ajax and Comet are not native to the browser so they must rely on maintaining two connections (e.g., an upstream connection and a downstream connection) to stream data to and from the browser. Web sockets support upstream and downstream communications over a single connection.
  • Less Load- Expect less of a burden on hardware since the equivalent Ajaxy user experience served up from a web server will support twice the number of single connections.
  • Roadblock Clearing -- Web sockets provides the ability to traverse firewalls and proxies so that streaming is possible over any connection. 
  • Efficient Streaming -- Web sockets supports streaming over HTTP, while alternate solutions use long-polling with the associated overhead.
Google Chrome, version 4.0.249.0 or higher, has web sockets enabled. To experiment, you will need the Dev Channel download of the Chrome browser.

More Reading