Getting Started With node.js and socket.io - Part 1
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:
Now that we have brew installed, installing node becomes very easy:
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:
We can now use NPM to install socket.io. Here we go:
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
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
http://localhost:8080
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.