Introducing HTML5 Web Sockets – taking bidirectional communication on the web to the next level
For a long long time we have tried to be able to push information from the server out to the end users without having to poll the server all the time.
Background
My first memory of pushing out information to the end user was a feature in Internet Explorer 4 when you could subscribe to information and get it pushed out to you. If my memory serves me correctly, it wasn’t really seamless, but nevertheless, it was my first experience of it.
Therefore, in 2005 when the AJAX hype hit us all, and we got a cross-browser way to dynamically update portions of a web page, we kept on sending requests to server to check for new information since there was no way to push things from the server when it was necessary. This lead to a lot of superfluous HTTP requests, header information being sent and way too much server load.
Then some smart people came up with the Comet approach where you use long-lived HTTP connections to be able to send data to the web browser when you wanted to and try to capture that and take action on it.
Enter Web Sockets
What we have needed, though, is a native approach, both on the client- and server-side to be able to have bidirectional communication. This is where Web Sockets step into the picture and I am certain it will in the long run revolutionize web browser communication.
What is needed on the server side is to support two new URI schemes, which are ws
and wss
(unencrypted and encrypted connections).
Web Sockets with JavaScript in the web browser
From a client-side web developer perspective, working with Web Sockets couldn’t be easier. First, what you do is create a Web Socket object with a URL to where you want to establish a connection to:
var ws = new WebSocket("ws://robertnyman.com/wsmagic");
From there on, you have a couple of useful methods:
// Send data ws.send("Some data"); // Close the connection ws.close();
And to complement that, there are a number of events:
// When connection is opened? ws.onopen = function () { console.log("Connection opened!"); };?? // When you receive a message ws.onmessage = function (evt) { console.log(evt.data); }; // When the connections is closed ws.onclose = function () { console.log("Connection closed"); }; // When an error occurred ws.onerror = function () { console.log("An error occurred"); };
And that’s all you need! The key things are of course the send
method whenever you want to send some information to the server, and the onmessage
event when something from the web server has been pushed out to the client.
A simple example can be seen in Remy Sharp’s Web Sockets demo.
Web browser support
Web Sockets are currently supported in:
- Google Chrome 5+
- Safari 5 (and in upcoming iOS 4.2)
- Firefox 4 (soon upcoming)
- Opera 11 (soon upcoming)
So far, there has been no word from Microsoft about Web Sockets and Internet Explorer 9, but gathered from talking to some people at Microsoft, my guess it’s not going to be in IE9.
Fallback options
However, as always, some smart people have worked on options that utilize Web Socket in web browser that support it and fall back to alternative technologies for web browsers lacking the necessary support:
Getting better communication on the web
I think that Web Sockets offer what AJAX really should have had from the beginning: a way to both send things to the server in a dynamic fashion, but, more importantly, a way for the server to push out information. Start playing out with it now! π
[…] This post was mentioned on Twitter by Robert Nyman, JPlana and Antonio Moratilla, Gavin Taylor. Gavin Taylor said: RT @robertnyman: Introducing HTML5 Web Sockets β Taking Bidirectional Communication On The Web To The Next Level: http://bit.ly/amuBc1 […]
Robert,
Thank you for a simple clean introduction to Web Sockets. Since client-side usage is based on call-back function (onmessage email event handler) it looks very similar to Ajax, so that callback to it in Internet Explorer shouldn’t be too difficult, right?
Vladimir,
Thanks, glad you liked it!
It is very easy to similar, although it does require support for the
ws
protocol as well. It shouldn’t be too hard to get it into IE, but I don’t think it will be in IE9.[…] Introducing HTML5 Web Sockets – taking bidirectional communication on the web to the next leve… […]
[…] Introducing HTML5 Web Sockets β taking bidirectional communication on the web tothe next level (Robert’s talk | Oct 22, 2010) […]
[…] everything is in blocking! You can make a Websocket, that works event based without this problem How Websocket works The secound part will be spdy for a quicker communication in https and less overhead. Spdy […]