This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->
node logo

Hello everyone! This is the eighth post in my new node.js modules you should know about article series.

The first post was about dnode - the freestyle rpc library for node, the second was about optimist - the lightweight options parser for node, the third was about lazy - lazy lists for node, the fourth was about request - the swiss army knife of HTTP streaming, the fifth was about hashish - hash combinators library, the sixth was about read - easy reading from stdin, the seventh was about ntwitter - twitter api for node.

This time I'll introduce you to I bet that most all of you already know, however I had several people message me to do an article on, so here it is. makes websockets and realtime possible in all browsers. It also enhances websockets by providing built-in multiplexing, horizontal scalability, and automatic JSON encoding/decoding. is written by Guillermo Rauch, who's the co-founder of LearnBoost. always chooses the best realtime communication method possible. Here is the list of all the communication methods it supports:

  • WebSocket
  • Adobe┬« Flash┬« Socket
  • AJAX long polling
  • AJAX multipart streaming
  • Forever Iframe
  • JSONP Polling

For example, if you're using Chrome, then will use websockets. If your browser doesn't support websockets, it will try to use flash sockets, then it will try long polling, etc.

Now let's look at a very basic example:

var io = require('');
var express = require('express');

var app = express.createServer()
var io = io.listen(app);


io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
  socket.on('disconnect', function () {
    console.log('user disconnected');

This example uses the awesome express node web framework (I'll blog about it soon) to setup a web server on port 80, and attaches to it. then listens for new connections and when a new connection from the browser is created, it emits news event that sends the { hello: 'world' } hash back to the browser.

It also setups a listener for my other event and it listens for disconnects. When the web application emits this event, calls the function (data) { console.log(data); } callback, that just prints the data to console. When the client disconnects, it logs this event to console also.

Here is the client side (in the web browser):

<script src="/"></script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    socket.emit('my other event', { my: 'data' });

First we include the script, and then we create a connection to http://localhost. Here chooses the best communication method that the browser supports. If it's Chrome then it will be websockets, then if you have flash, then it will try flash sockets, then long polling, then multipart streaming, then forever iframe method, and finally jsonp polling. Then we listen on news event and when we receive it, we emit my other event.

This way you can build all kinds of awesome realtime applications, such as web chat servers and web irc clients.

There are many other features that supports, such as namespaces, volatile messages, message confirmations and message broadcasting. See the documentation to learn all about this awesomeness!

You can install through npm as always:

npm install on GitHub:

Also take a look at dnode that allows to call functions over!

Sponsor this blog series!

Doing a node.js company and want your ad to appear in the series? The ad will go out to 14,000 rss subscribers, 7,000 email subscribers, and it will get viewed by thousands of my blog visitors! Email me and we'll set it up!


If you love these articles, subscribe to my blog for more, follow me on Twitter to find about my adventures, and watch me produce code on GitHub!

This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->


thanh long Permalink
July 06, 2013, 07:59

is app.listen(80); must come before var io = io.listen(app);?
and is 'emit('abc')' function means trigger socket.on('abc') event?

Ganjar Permalink
October 14, 2014, 03:42

nice. d'you have an example of usind socket with Mysl in nodejs ? i meant to create a timeline like twitter

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "cloud_292": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.