Yesterday was the first JavaScript Hack Night at the Stamford Innovation Center, and I proposed to the group we try setting up push notifications using node.js and socket.io. We had a heck of a time getting it up and running, even having referenced installation articles and google groups about the matter, so I’m posting my findings here.
I feel compelled to point out, though, our friend Joe got a node.js example site up and running using the node windows installer and Microsoft WebMatrix in a matter of a few minutes. However, I don’t believe he had socket.io running.
Symptom?
After installing node, npm and getting socket.io from git, running “node app.js” for the socket.io chat example kept producing the following error:
module.js:340 throw err; ^ Error: Cannot find module 'socket.io-client' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:362:17) at require (module.js:378:17) ...
Problem?
We were failing to install socket.io dependencies. The following combination of versions eventually worked for me:
node 0.8.7 npm 1.1.49 socket.io 0.9.10
Step-by-Step Installation
Dependencies, Git and working directory
I was working with a clean Ubuntu install, so I had to first run the following:
sudo apt-get install g++ curl libssl-dev apache2-utils git-core cd ~ mkdir node-socket.io cd ~/node-socket.io
Installing Node
Get node.js. This may take a while to build, depending on your system:
cd ~/node-socket.io git clone https://github.com/joyent/node.git mv node node-0.8.7 cd node-0.8.7 git checkout v0.8.7 ./configure make sudo make install
Installing npm
Grab a copy of npm; we may be able to grab a newer one:
cd ~/node-socket.io git clone https://github.com/isaacs/npm mv npm npm-1.1.49 cd npm-1.1.49 git checkout v1.1.49 ./configure make sudo make install
Version check
At this point, we should see the following:
>node -v v0.8.7 >npm -v 1.1.49
socket.io chat example
Let’s download and run the socket.io chat example:
cd ~/node-socket.io git clone https://github.com/LearnBoost/socket.io.git cd socket.io sudo npm cache clean npm install . cd examples/chat npm install .
Be sure to run both npm installs! It will install all dependencies for the chat application. We were failing to run the highlighted step.
Start the application
cd ~/node-socket.io/socket.io/examples/chat node app.js
You should see output similar to:
info - socket.io started app listening on http://0.0.0.0:3000
Now you can visit http://localhost:3000 in your browser and play with the chat application.
Let me know if you have any problems.