For years there was no native support for passing data from a web server to a client. The client would always have to initiate an HTTP request to pull data down from the server. This made realtime applications almost impossible. There have been some clever hacks to work around this limitation such as long-polling but there has never been a true way to push unsolicited information from the server until now. Enter WebSockets. WebSocket is a protocol providing full-duplex communication channels over a single TCP connection. The WebSocket protocol has recently been adopted by all major internet browsers and is part of HTML5. Using WebSockets, a web server can push data to it's connected clients in realtime. a WebSocket also allows the client to pass data over the connection to the server making it a bi-directional channel. WebSockets are initiated just like any other HTTP connection but after the connection is established it is "upgraded" to a TCP connection.
As I mentioned earlier, there are other options that serve the same function as WebSockets but they have been less effective and/or less widely implemented.
- long-polling: This is more of a hack than a standard. It relies on an http connection's timeout limit. But, unlike WebSockets, the connection must be reset periodically after a timeout occurs, or after a succesful server to client message has occured.
- Flash XMLSocket relay: This method relies on embedding a Flash application inside the webpage, enough said.
- server-sent events (SSE): SSE is another HTML5 specification. While SSEs seems like a winner at first glance, they are lacking support in some of the major browsers at this time. SSEs also only send data in one direction from server to client making them less versatile.
Some examples of where WebSockets can improve your user experience by delivery realtime data are:
- Instant Messaging
- Online Games
- Social Media Feeds
- Document Editing Collaboration