Lots of questions on the socket.io tutorials (part 1)?

So, I've been trying to figure out how to use socket.io. Socket.io official referred me to this tutorial page: https://github.com/LearnBoost/socket.io, and I went through it several times. I came up with the same list of issues, every time, though. Here they are:

Issue 1) "How to install" appears on multiple lines throughout the course of the official socket.io tutorial. The first step in the README.md file at their official git (that's the tutorial I have questions on) is:

How to Install
npm install socket.io

My questions: when, where, on what system, etc.? I've installed node on my Windows XP machine running an XAMPP server. I followed the steps in the tutorial to the letter, and here's a screenshot of the result:

cmd>md nodetmp cmd>cd nodetmp cmd>node node>npm install socket.io {ellipse} node><Keystroke: Enter> {elipse}

As you can see/hear, the result is ellipse points. After the keystroke, , more ellipse points. I left this window open for about 45 minutes. Had breakfast and had a conversation with my wife. Apparently nothing is downloading, and it's not going to do anything more than sit idle. So, when should this work? Why isn't it working? Am I possibly typing the command in at the wrong location? Is this command compatible with Windows? Is it compatible with XAMPP? If so, how?

Issue 2) From README.md

How to use
First, require socket.io:

The natural question is, "Where does the code that follows belong?" I did some research and found at http://socket.io/#how-to-use, there's a tutorial that shadows README.md. There's one additional tutorial there, though. So, we're just back to the first question: where does the code belong? I've tried various locations. Sometimes I get system messages. I wonder if it's the server or the client generating those messages. I'm not able to see any logs in my console.

Here's the server source code:

var app = require('http').createServer(handler) //No handler is required in the express version.  Why have a handler here and not there?
  , io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(80); //Why 80?  The documentation says that the listen port should be either 8000 or 9100.  Is 80 the listen port?

function handler (req, res) {  //What is req?  It doesn't appear anywhere else in the server or client code.
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

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

Now, I don't want to be purely critical. The last 6 lines of code are very clear and easy to understand. I just am not able to get to the point where I can test to see if it works.

Before going on to the additional questions, I'd like to get this first tutorial working, first. Pending success, I can submit a change request or a pull request to them--something so that other people won't have the same frustration in the future.

Here's a sneak preview of the next question:

Do I need to make my own server terminal?

enter image description here

Thank you, for reading.

First off, nothing with node.js works as well on Windows as it does on Mac OS or Linux. Most node.js developers don't use Windows and so nothing is as well tested. You're probably going to continue to run into issues on a Windows machine.

Which version of node.js are you using? You can check by running the following on the commandline on the server that you've installed node on:

> node -v

If it's greater than 0.6 than NPM (the node package manager, read more at http://npmjs.org/) is already installed. Create a new directory for your project

> mkdir myproject
> cd myproject

Then you can use NPM to install socket.io. From the same command line (not node), run:

> npm install socket.io

This will install the socket.io library locally in your myproject directory in a directory called node_modules. You can then require('socket.io') in your .js files where you want to use it.

At this point, the code that you have should run (assuming you've copied the sample correctly). To run your application, you use the node command line tool. Assuming you save your application as app.js, you would run it using:

> node app.js

To answer your other questions:

  1. Express.js is a lightweight framework for node.js that makes creating apps easier and faster. I highly recommend you look into it for your projects. It does a lot of things for you so you don't have to worry about them.
  2. Node.js apps usually listen on a high port so that you don't need rootaccess to run them (particularly on Unix environments, no idea what Windows does). Port 8080 is common, I happen to listen on 3000 for my apps. When you make calls to your server, just make sure to include the port number when using something other than 80 (localhost:3000 for example).
  3. Req has all of the details about the incoming request. You can do console.log(req) within your code to dump it all to the console to see for yourself. You'll want to become familiar with Connect which is a set of middleware for node.js. There are modules for parsing form data, cookies, etc so that you don't have to do that yourself.

Finally, you can write some client code to actually make a connection to your server. In your html file, make sure to include socket.io.js as a standard javascript script and then connect to your server using:

var socket = io.connect(''); // filling in your server info

I also had problems and don't think the instructions were clear enough. I used the express3 example. Here's how I got it to work.

Server Code

  1. I created a project dir we'll call it server_code
  2. Cd server_code
  3. Ran npm install socket.io express
  4. Created a file named app.js in the server_code directory and copied the app.js code listed from the socket.io express3 example section into this file
  5. Had to change the port from 80 to 8080 (or whatever)
  6. Ran node app.js

Client Code

  1. Created a new directory anywhere on your system, we'll call it client_code
  2. Create a new file called index.html and copy the contents over from socket.io webpage
  3. Change localhost to localhost:8080
  4. Create a new directory src in client_code
  5. Copy the socket.io.js file from the server code to client (hardest part for me to figure out)

    cp server_code/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js client_code/src/ -v

  6. Open index.html in browser and check the console. You should see Object hello world.

1st On the client-side you'll need to include Socket.io. Put this in with your HTML. This allows the client to perform the required handshake process to authorize the two way web-socket connection.

script(src="/socket.io/socket.io.js")

2nd The client needs to connect.

var socket = io.connect('');

If you can't download Socket.IO for some reason: Node.js + Socket.io + Windows 7?