To all my readers: Fortunately for me I'm currently in Brazil scaling systems, soaking up 90º F (32.2º C) weather, and having my fill of feijoada and caipirinha. Meanwhile, to keep this blog up to your standards, I've enlisted my main man Jonathan Freeman to educate you while I'm away. Without further ado, here's Jonathan -- front-end guru, big data specialist, and jazz musician. Enjoy and see you in a couple weeks! -- ACO
Users now demand information as soon as it's available. If you have to refresh the page to get new information, it's already too late. Luckily, a protocol supported by all modern browsers allows for direct data exchange: WebSockets.
No other solution exists that provides true bidirectional communication like WebSockets, yet many Web developers still rely on hacks like AJAX long polling. (For the record, I think long polling is highly creative and functional, but a hack nonetheless.) Lack of enthusiasm for WebSockets may be related to a security vulnerability years ago or the lack of browser support at the time, but both issues have been addressed.
[ Work smarter, not harder -- InfoWorld has the tips and trends programmers need to know in the Developers' Survival Guide. Download the PDF today! | Keep up with the latest developer news with InfoWorld's Developer World newsletter. ]
Determining whether to use WebSockets for the job at hand is simple:
- Does your app involve multiple users communicating with each other?
- Is your app a window into server-side data that's constantly changing?
If you answered yes to either of these questions, consider using WebSockets. If you're still unsure and want some inspiration, here are a few killer use cases.
1. Social feeds
One of the benefits of social apps is knowing what all your friends are doing when they do it. Sure, it's a little creepy, but we all love it. You don't want to wait minutes to find out a family member won a pie-baking contest or a friend has become engaged. You're online, so your feed should update in real time.
2. Multiplayer games
The Web is quickly coming into its own as a gaming platform. Without having to rely on plug-ins (I'm looking at you, Flash) Web developers are now able to implement and experiment with high-performance gaming in the browser. Whether you're dealing with DOM elements, CSS animations, HTML5 canvas or you're experimenting with WebGL, efficient interaction between players is crucial. I don't want to discover my opponent has moved after I pull the trigger.
3. Collaborative editing/coding
We live in the age of distributed dev teams. Working on a copy of a document used to suffice, but then you had to figure out a way to merge all the edited copies together. Version control systems like Git can help with certain files, but you'll still have to track people down when Git finds a conflict it can't deal with. With a collaborative solution like WebSockets, we can work on the same document and skip all the merges. It's easy to see who is editing what and if you're working on the same portion of a document as someone else.
4. Clickstream data
Being able to analyze how users interact with your website is critical to improving it. The cost of HTTP has forced us to prioritize and collect only the most important data. Then, six months down the line, we realize we should've been collecting a different metric -- one that looked unimportant but would now shed light on a critical decision. With the overhead of HTTP requests out of the way, you can be less restrictive about the kind of data you're sending from the client. Want to track mouse movement in addition to page loads? Just send the data through a WebSocket connection to the back end and persist it in your favorite NoSQL store. (MongoDB is good for logging events like this.) Now you can play back customer interactions to see what was really going on.