WebSockets (HyBi): The End of Cometing and Polling?
Building "real-time" apps on the web today is a nightmare. We use "Long Polling," where the browser keeps a request open until the server has something to say. We use "Comet," we use hidden iFrames... it’s all a bunch of hacks to get around the fact that HTTP was designed for requesting documents, not for two-way communication. But the "HyBi" working group (part of the HTML5 effort) has a better way: WebSockets.
Full-Duplex over a Single Socket
WebSockets start as a standard HTTP request, but they use an "Upgrade" header to switch to a binary, bidirectional protocol. Once the connection is established, the client and server can send messages to each other at any time. No more headers, no more cookies on every request, just raw data.
// The future of real-time web looks like this
var socket = new WebSocket("ws://example.com/chat");
socket.onmessage = function(event) {
console.log("Server says: " + event.data);
};
socket.send("Hello Server!");
The overhead is tiny-just a few bytes for the framing. This makes it perfect for things like stock tickers, chat apps, and multiplayer games that would have been impossible or incredibly laggy with standard Ajax.
The Browser Support Challenge
Right now, only the latest "nightly" builds of Chrome and Safari support it. IE and Firefox are still lagging behind. Plus, there are concerns about how transparent proxies and firewalls will handle this new protocol. Some might see it as "invalid" HTTP and kill the connection.
Looking Ahead
WebSockets are the final piece of the "Web as an Application Platform" puzzle. Combined with Canvas and WebGL, we finally have the tools to build apps that are just as responsive as desktop software. We’re moving away from the "Request/Response" cycle and toward a "Stream" model. It’s going to be a few years before we can use it everywhere, but the foundation is being laid today.
Aunimeda develops websites and web applications for businesses - corporate sites, e-commerce, portals, and custom platforms.
Contact us to discuss your web project. See also: Web Development, E-commerce Development