Web Sockets

By Ryan Montgomery on 31 01 2011

Building applications for the web means working within the bounds of what is technically feasible over the Internet. For a long time the limitations of the Internet have forced developers to come up with unique approaches (read: dirty hacks) to deliver the best possible user experience they could muster. As the demand for real-time information has grown, so have the number and complexity of the tricks used to provide an interactive experience on the web.

The challenge has been due largely to the very nature of the Internet. A client can request information from a server, but a server cannot send information to the client. This is due to the fact that the protocol powering the web, HTTP, is a connection-less protocol. That is to say after every request made to a web server, the connection with the server is closed -- for very good reason. If all of the millions of connections created every second across the globe were never closed, the hardware that powers the Internet would fail under the stress.

There has to be a better way

What if there was a better way to deliver real-time information in a way that scales reliably and efficiently? Well, HTML5 has some awesome new features to help designers and developers do just that. HTML5 WebSockets is one of these new features that enables full duplex bi-directional messaging. That means no more AJAX long polling, or other hacks that mimic two way communication. Instead we have a true single socket, two way stream of information from the client to the server and the server to the client.

What does this newfound ability give us?

Well, immediately you can start to eliminate the waste of your applications that make AJAX polling requests every second for new changes to data. AJAX polling is what gives many tools the feeling that they are being updated “live” whenever someone makes a change, but it also means they’ve had to deal with hundreds of thousands of unnecessary requests. A great example of this is PivotalTracker. Using an HTTP Proxy like Charles you can see all the requests PivotalTracker is making even when you’re not doing anything.

Besides updating our old polling applications, we can also start building real-time user experiences from the outset our applications. Not that we didn’t try before, but now we’ll be able to do it more easily which will allows designers and developers to push the bounds of what is possible. I believe that as the web moves in this direction and the new features of HTML5 become more reliable, we will begin to think about web applications very differently. The ability to push data to devices will bring a whole new dynamic to application design and that makes me very excited to be a web developer today.