What are WebSockets?
WebSockets represent a long awaited evolution in client/server web technology. They allow a long-held single TCP socket connection to be established between the client and server which allows for bi-directional, full duplex, messages to be instantly distributed with little overhead resulting in a very low latency connection.
A brief history of the web (from a developer’s perspective)
The Internet wasn’t built to be all that dynamic. It was conceived to be a collection of HyperText Markup Language (HTML) pages linking to one another to form a conceptual web of information. Over time the static resources increased in number and richer items, such as images, began to be part of the web fabric. Server technologies advanced allowing for dynamic server pages - pages whose content was generated based on a query.
Then along came WebSockets.
Why WebSockets are a game-changer
Finally WebSockets represent a standard for bi-directional realtime communication between servers and clients. Firstly in web browsers, but ultimately between any server and any client. The standards first approach means that as developers we can finally create functionality that works consistently across multiple platforms. Connection limitations are no longer a problem since WebSockets represent a single TCP socket connection. Cross domain communication has been considered from day one and is dealt with within the connection handshake. This means that services such as Pusher can easily use them when offering a massively scalable realtime platformthat can be used by any website, web, desktop or mobile application.
WebSockets v AJAX
WebSockets don’t make AJAX obsolete but they do supersedeComet(HTTP Long-polling/HTTP Streaming) as the solution of choice for true realtime functionality. AJAX should still be used for making short-lived web service calls, and if we eventually see a good uptake in CORS supporting web services, it will get even more useful. WebSockets should now be the go to standard for realtime functionality since they offer low latency bi-directional communication over a single connection. Even if a web browser doesn’t natively support the
WebSocket object there arepolyfill fallback optionswhich all but guarantee any web browser can actually establish a WebSocket connection.