HTML and CSS Reference
Server-Sent Events, also known as EventSource, push messages
sent in real time from the server to the browser client. These are
perfect for applications that need information from a server with-
out necessarily having to wait for any user interaction or input,
like streaming prices or live chart updates, or live information
that you're monitoring.
The WebSockets specification is one of the shiniest new APIs
outside the realm of HTML5, but it's actually really important
for some of the real-time-based web applications that have
WebSockets give you a bidirectional connection between your
server and the browser. This connection is also real time and is
permanently open until explicitly closed. This means that when
the server wants to send your client something, that message is
immediately pushed to your browser.
This is what Comet succeeded in doing. Comet created a real-
time connection to your server, but it would do it using a variety
of different hacks. Ultimately, if none of these hacks worked, it
would eventually fall back down to Ajax polling, which would
constantly hit your server and that doesn't scale up very well.
If you have a socket open, your server can push data to all those
connected sockets, and the server doesn't have to constantly
respond to inbound Ajax requests. This is the move from polling
to pushing, from reactive to proactive. This is what Comet was
achieving through hacks, and this is what WebSockets achieve
natively in the browser.
NoTE If the browser
doesn't natively support
WebSockets, you can fake it
using Flash. Visit https://github.
see Hiroshi Ichikawa's Flash-
based polyfill for WebSockets.
Sockets solve latency of
Low latency is a massive benefit of WebSockets. Since your
socket is always open and listening, as soon as data is pushed
from the server, it just has to make its way to your browser, mak-
ing the latency exceptionally low in comparison to something
like an XMLHttpRequest -based Ajax request.