IT IS HERE! Get Smashing Node.JS on Amazon Kindle today!
Show Posts
← Back to homepage

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.

24 Comments

Cool stuff, Guillermo!

As you might or might not know, I’ve created a minimal Web Socket server for local use, that is located at http://github.com/alexanderte/websocket-server-node.js.

The goal for my project is to keep it simple, even local‐only. If there are people who need more than just the protocol and local usage, then I’ll point them to your project.

I should probably change over Really Cloudy (http://github.com/revis/Really-Cloudy) so that it uses web sockets to distribute data and return results.

However I’m concerned about making it more complex to implement a custom back end for it. At the moment the demo queue server is a node.js implementation but setting up a http/JSON queue server for your language of choice is pretty easy (and very well supported by just about every framework).

That means I’ll be really interested in a client side web socket library with graceful degradation :-)

Great work!

fyi, specifying javascript strings at the command line (bash) didn’t work for me quite like you showed in your README. When I entered this:

node runserver.js –port=’8080′ –host=’some_other_host’

The shell was dropping the quotes, so the argument was turning into:
–host=some_other_host

This happens whether I use single quotes or double quotes. Thus, it was attempting to eval the hostname as a symbol rather than as a string (at tools.js:38). Backslashing the quotes fixes the problem…

I hooked your websocket server implementation up to web-socket-js (which includes Flash-based websocket emulation as a fallback when native WebSocket is missing). It works great now in Chrome, Firefox, and IE.
http://github.com/gimite/web-socket-js

    Guillermo Rauch said

    Thanks! I have a blog post of an example like that in the works :)

thanks.good artickle

Ted said

I’d like to run the examples but am getting this error:
node runserver.js
TypeError: Cannot read property ‘Connection’ of undefined
at [object Object].connect (/Users/tedx/Downloads/node.websocket.js/lib/redisclient.js:55:32)
at Object. (/Users/tedx/Downloads/node.websocket.js/log.js:66:8)
at Module._compile (node.js:704:23)
at Module._loadScriptSync (node.js:716:16)
at Module.loadSync (node.js:615:10)
at loadModule (node.js:560:16)
at require (node.js:683:12)
at Object. (/Users/tedx/Downloads/node.websocket.js/websocket.js:14:14)
at Module._compile (node.js:704:23)
at Module._loadScriptSync (node.js:716:16)

I’m using node v0.1.33-158-g52b2954. It looks like a redisclient issue what would you suggest?

The goal for my project is to keep it simple, even local‐only. If there are people who need more than just the protocol and local usage, then I’ll point them to your project.
’ll be really interested in a client side web socket library with graceful degradation

However I’m concerned about making it more complex to implement a custom back end for it. At the moment the demo queue server is a node.js implementation but setting up a http/JSON queue server for your language of choice is pretty easy (and very well supported by just about every framework).

thanks man

ashikcse said

can you please provide any simple code like this to consume asp.net webservices.
Thanks,
Ashik

Your thoughts?

About Guillermo Rauch:

CTO and co-founder of LearnBoost / Cloudup (acquired by Automattic in 2013). Argentine living in SF.