Consider using WebSockets for instantaneous data without the browser refresh
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.
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.
5. Financial tickers
The finance world moves fast โ microsecond fast. Our human brains canโt keep up with processing data at that speed, so we write algorithms to do it for us. Even if youโre not dealing with high-frequency trading, however, stale information can only hurt. When you have a dashboard tracking companies youโre interested in, you want to know what theyโre worth now, not 10 seconds ago. Use WebSockets to stream that data and no one needs to wait.
6. Sports updates
While weโre on the topic of silly things people are passionate about, letโs talk sports. Iโm not a sports guy, but I know what sports guys want. When the Patriots are playing, my brother-in-law surrenders himself to the pace of the game. Itโs a trancelike state prone to violent, visceral reactions. I donโt understand it, but I respect the intensity of the connection, so the last thing I want to do is add latency to his experience. If youโre including sports information in your Web app, WebSockets can keep your users up to speed.
7. Multimedia chat
While thereโs no substitute for holding a meeting in person, videoconferences are about as good as it gets when we canโt get everybody in the same room. The videoconference route is plug-in heavy, though, and full of proprietary โgoodness.โ Iโm a fan of the open Web, so using WebSockets with getUserMedia APIโs and the HTML5 audio and video elements is an obvious win. WebRTC, the logical predecessor to the combination I just outlined, looks promising, but the lack of current browser support eliminates it as a candidate.
8. Location-based apps
More and more developers are leveraging the GPS capabilities of mobile devices to make their Web apps location-aware. If youโre tracking user locations over time (such as running an app that tracks your progress along a route), youโll be collecting fine-grained data. If you want to update a Web dashboard in real time (say, a track coach monitoring the progress of runners) HTTP is going to be unnecessarily bulky. Leverage the TCP connection a WebSocket uses and let that data fly.
9. Online education
School keeps getting more expensive, while the Internet keeps getting faster and cheaper. Online education can be a great way to learn, especially if you can interact with teachers and other students. WebSockets is the natural choice, allowing for multimedia chat, text chat, and other perks like collaborative drawing on a digital communal chalkboard.
These are just a few examples to help get the juices flowing. What else can you see being a great use of WebSockets?
This article, โ9 killer uses for WebSockets,โ was originally published at InfoWorld.com. Keep up on the latest news in application development and read more of Andrew Oliverโs Strategic Developer blog at InfoWorld.com. For the latest business technology news, follow InfoWorld.com on Twitter.


