Apr 172011

Like many developers out there, I’ve been interested in the potential of real-time, high-throughput client/server communication in web apps using node.js for some time. Once I started researching, I was quickly lead to socket.io as well, which rocks…hard. What’s that?  Not dealing with transport mechanisms, and getting cross-browser (including mobile!) support for free? Yes please!

In this post I’ll go through the installation of node.js and socket.io on Mac OSX, then in my next post we’ll make a simple test app from scratch (which helps me to learn far better than just running a pre-made example). Since I’m in OSX, I can make things super easy and use brew to install node. Brew is an extremely handy little utility that can install UNIX tools that were not included in OSX. If you already have brew installed, skip this step. If not, go ahead and install it:

ruby -e "$(curl -fsSLk https://gist.github.com/raw/323731/install_homebrew.rb)"


Now that we have brew installed, installing node becomes very easy:

brew install node


No muss, no fuss. This will install node.js at



The only potential downside is that brew installs the current stable version of node, which is 0.4.6 as I’m writing this, though 0.5.0beta does exist. This probably won’t bother you, but if you want the latest and greatest, get the source from github and build it yourself. Here are some instructions. You may also need to go the github route if for some reason brew doesn’t work for you.

Want to master Node.js? Try my highly-rated online course Learn Node.js by Example. Click here to get 50% off on screencasts, interactive projects, and more!

Now its time to install a little enhancement called Socket.io. Socket.io is only one of a number of packages that have been written for node, so the easiest way to install it and potentially other useful packages is with the Node Package Manager, NPM. Install NPM using:

curl http://npmjs.org/install.sh | sh


We can now use NPM to install socket.io. Here we go:

npm install socket.io


Ok, we’re all set. You now have both node.js and socket.io installed. At this point, you could start an example node server by going to


and issuing the command

node server.js


You should see output similar to this:


This starts the test node.js socket.io server, which will be default listen on port 8080. To see the demo, navigate to


in any web browser and poke around. There is a nice chat example that will allow you to watch server output in the terminal as communications between the client (your browser) and the server commence.

Now you have a working node.js and socket.io installation on OSX, and have possibly gotten some other handy tools like brew and NPM in the process. Congratulations! Now you can dive in and look at the code of the example/server.js we just used to start the node server to try to understand the inner workings. This always falls a bit short for me – I like to get my hands dirty. So, in my next post we’ll build a small node server and client, adding to it as we go to illustrate the basic socket.io API and the basic functionality of socket.io and node.js.

Until then…

Continue here if you are using Socket.io v0.7, v0.8, or above.

If you are working with Socket.io v0.6, continue on to this version of the tutorial.

  • Pingback: Codehenge » Blog Archive » Getting Started With node.js and socket.io – Part 2

  • Thomas Sileo

    great introduction!

    • http://www.codehenge.net Constantine Aaron Cois


  • Pingback: Getting Started With node.js and socket.io (v0.7+) – Part 2 | Codehenge

  • DS kanth

    Wanna try this…

  • Camron Roberts

    hello i am having a bit of trouble while trying to install socket this message came up

    [ws v0.4.22] Attempting to compile blazing fast native extensions.
    [ws v0.4.22] Native code compile failed (but the module will still work):
    [ws v0.4.22] The native extensions are faster, but not required.
    [ws v0.4.22] On Windows, native extensions require Visual Studio and Python.
    [ws v0.4.22] On Unix, native extensions require Python, make and a C++ compiler.
    [ws v0.4.22] Start npm with –ws:verbose to show compilation output (if any).

    then when i entered this in the terminal: /usr/local/lib/node/socket.io/example/

    i received this: -bash: /usr/local/lib/node/socket.io/example/: No such file or directory

    I then tried this: node server.js

    and got this:

    throw err;
    Error: Cannot find module ‘/Users/camron/server.js’
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
    (null):~ camron$

    Can you please help thanks

    • http://www.codehenge.net Constantine Aaron Cois

      Hi Cameron. The issue you are having is that the examples are no longer packaged with socket.io. Since it looks like you have Node, npm, and socket.io installed ok, just move on to the next part of the tutorial:


      Here I will be showing you how to build something form scratch using node and socket.io.


44 queries in 0.895 seconds