Posterous theme by Cory Watilo

Node.JS and the WebSocket protocol « Devthought

After reading Simon Willison’s post on Node.JS, I decided I’d give it a try myself. Today I released node.websocket.js

After a couple of months I’ve been watching the progress on the WebSocket protocol, which gives JavaScript developers full-duplex communications channels in the browser. This is a very exciting alternative to the COMET techniques we’re used to seeing.

Node.JS is a framework for networked, event-driven applications, where JavaScript has a natural fit. This particular implementation is built on top of the excellent V8 engine.

Running the server is as simple as:

$ node runserver.js

Logging (in the Redis storage, an excellent persistent key-value storage mechanism which will be subject of another post) will begin.
Writing a client is equally easy:

var a = new WebSocket();
a.onmessage = function(event){
   console.log(event.data);
};

As you might know or induce already, the wonder of WebSocket is only available to a few selected browsers. To my knowledge only nightly Webkit, Chromium, trunk Firefox (and possibly Opera) have a decent degree of support of WebSocket.

The bigger goal of this project is to provide an API that allows fallback on techniques such as XHR streaming, long-polling, forever iframe (+htmlfile), for cross-browser support and rapid development. Writing the server side modules for node.websocket.js is really pretty easy thanks to the excellent modules API Node.JS provides. And a bunch of projects that allow you to plug in Node.JS to a variety of data sources are already available.

Alexander Teinum also has a very minimal implementation meant for local use, with which he’s empowering tools of the impressive BrevityOS project. Other implementations include Jetty’s and Kaazing, both worth keeping an eye on.

WebSockets and Full Duplex Communication now available to node.js.