Understanding Dynamic Webpage Updates
There’s a lot of ways to make your webpage update without requiring the user to refresh the page. The way Facebook updates your news feed while you’re looking at it. Or Google+, or Twitter, or whatever.
I’m going to talk about just three ways: Polling (with Ajax), Web Sockets, and Comet (or “long polling”). There’s a ton of different variations of ways to may pages update dynamically. I’m not going to talk about Flash, or Java, or other icky things (I’m looking at you, Silverlight).
Polling (via Ajax)
Ajax is a combination of a few technologies, and can be a bit confusing to talk about… so let’s distill it down. Basically, your browser asks the server, “hey, did (X) change?” to which the server would mostly respond, “no.” When there’s a change, some part of the displayed page would get that updated bit of information. Pretty over-simplified, but that’s the gist of it.
The problem is that the server is getting asked a lot of questions. It’s like reloading the webpage constantly to see if there’s a change: given enough people, that puts a heavy burden on the server. To fix this, Comet and Web Sockets were created.
The concept of web sockets is, in its simplest form, that the server will respond when something has changed. Instead of asking every second for 30 seconds to get a single response (asking 30 times if something has changed), web sockets kinda says, “hey, just let me know when something changes.” The server only sends back when something actually has changed: in our scenario, that means we’ve eliminated 29 questions.
Unfortunately, Web Sockets is a feature that isn’t 100% supported. Well, it is mostly supported in the newest browsers, but not necessarily completely. Not in all of them. And older browsers? Yeah, they’re screwed.
So polling is cool, but it’s fairly taxing on the server. Web Sockets is a cool idea, but isn’t completely supported. In comes Comet to create that middle ground.
Essentially, comet is just like polling, but with a twist: instead of returning a response immediately, as with polling via Ajax, the server would hold on to the request, and only respond after a certain amount of time or after something changed. Basically the same as Web Sockets, except… well, different.
The great thing about comet is that pretty much all modern browsers support it. In fact, even most of the older browsers support it!