WebSocket - Why, what, and - can I use it?
WebSocket enables the next-generation of Web applications that utilize real-time bidirectional messaging for a better user experience.
WebSocket is something that needed to happen.
The Web has developed far beyond its origins as a distribution system for linked documents. Full-scale applications like Email, document editing, chat or gaming - things that used to be restricted to the desktop keep moving into the browser, and there is no end in sight.
It is amazing how far we've managed to come by hacking what was originally intended as a protocol for delivering hypertext: HTTP. But, in the end, all the AJAX, long-polling and whatnot else are just that: hacks. There's a limit to how far these hacks could push HTTP, and how well they scale. The Web needed a protocol which was designed from the ground up for this new breed of applications.
WebSocket is that protocol. It offers connections that are both persistent ("long-lived") and bi-directional ("full-duplex"). This means that, finally, the server can actively push information to the client any time. No more polling. And the client can push information to the server without doing full HTTP requests each time.
WebSocket is designed for low protocol and wire-level overhead - goodbye, chunky HTTP headers. Combined with the fact that messages only get sent when needed, this can dramatically reduce bandwidth usage, which is a boon in general, but especially for mobile applications and for the Internet of Things.
WebSocket eases the pain of doing bi-directional, real-time applications in the browser, and makes a lot of things trivially easy, e.g.
- instant messaging
- synchronisation of content across browsers (some videos + demos)
It also allows uses to scale much better: it's much less resource intensive to have large numbers of persistent WebSocket connections than HTTP connections.
Generally speaking, going forward, WebSocket will be used anytime you go beyond serving static Web pages, and require bi-directional communication between the browser and the server.
Can I use it?¶
As with all HTML5 technologies, after you've first looked at the new shiny, and been dazzled by the capabilities and possibilities, comes The Big Question: Can I Use It Today?
The 30% unsupported are mainly due to the usual suspects when it comes to the legacy web: older versions of Internet Explorer (which here already includes IE9), and the stock Android browser (which is the new IE6.
Support increases some more once you add the fact that for browsers with Flash installed a fallback exists, and is useable. With Flash penetration on the desktop still at 95+%, legacy IE on the desktop is a borderline problem at most. And if Flash is not available, Google Chrome Frame for IE works also.
For almost all platforms, browsers which do support WebSockets are available, so switching browser may also be an option in some cases.
In the end, the only devices which are truly unreachable are ancient iOS devices (< iOS 4.2) and anything running Windows Phone 7.0 - 7.8. For most use cases, this will be negligible.
With so few problematic edge cases, and all modern browsers coming with WebSocket, the question of support will lose importance quite quickly going forward.
WebSocket offers real benefits, and support is wide enough that is deserves a closer look today. Depending on where you find it can help you, there's a high likelihood that you can deploy it right now.
- Find out how Crossbar.io, an open source application router, uses WebSocket to enable you Web and mobile applications powered directly from your Oracle or PostgreSQL database.
1. The people who developed your browser's WebSocket library probably used our Autobahn Testsuite to test their implemenation.
Start experimenting and prototyping for IoT applications with Crossbar.io and Raspberry Pi!
Loaded with all the software you need to make the board part of WAMP applications!
- Crossbar.io's Technology Stack
- Security in the IoT
- WebSocket - persistent across page loads?
- WAMP user language survey
- Mixing Web and WAMP code with Twisted Klein
- Going asynchronous: from Flask to Twisted Klein
- Small demo of a pragmatic use of WAMP in Python