Hi I am currently investigating in using NodeJS for a real time web app. It seems like a good choice to me for performance reasons and mainly because i can code backend and frontend in the same language. This should lower the learning curve for new developers significantly. I basically followed this guide to install nodeJS: http://expressjs.com/guide.html Here are the steps I had to take for the long way to a running nodeJS server:

Download and install nodeJS Open a cmd and cd\ md nodetest cd nodetest

This step is only for people behind a company proxy (like me). You’ll have to configure npm to use your proxy using the following commands: npm config set proxy http://user:pass@ip:port npm config set proxy-https https://user:pass@ip:port

use  “npm info socket.io version” and “npm info express version” to find out the latest versions Create the following file called package.json in the nodetest folder { “name”: “hello-world”, “description”: “hello world test app”, “version”: “0.0.1”, “private”: true, “dependencies”: { “express”: “3.3.4”, “socket.io”:”0.9.16” } }

Run npm install Now you should have the expressJS and socket.io libraries in a folder “node_modules”. Next you’ll have to create a folder called “public” in your nodetest directory. Now copy the contents of   C:\nodetest\node_modules\socket.io\node_modules\socket.io-client\dist  to C:\nodetest\public\socket.io    (obvious step, right?) Create server.js in the nodetest directory and sender.html, client.html in the public directory. get jquery and jquery ui and copy them into the public directory Fill server.js, sender.html, client.html with the code later on the page open the htmls in a browser and enjoy. (remember, my code uses port 8080 by default)

  server.js: var express = require('express'); var app = express(); var path = require('path'); var server = require('http').createServer(app); var io = require('socket.io').listen(server); //this caused me some nightmares server.listen(8080); app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); }); app.configure(function(){ app.use(express.static(path.join(__dirname, 'public'))); }); // note that i force socket.io to fall back on long polling //for reliability reasons io.configure(function () { io.set(“transports”, [“xhr-polling”]); io.set(“polling duration”, 10); }); io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); //this is my custom event socket.on('my other event', function (data) { console.log(data); //whenever someone uses sender.html to send something, this line will //broadcast it to everyone else except the original sender socket.broadcast.emit('my other event',data); }); }); The comment above the server.listen command indicates that I had some problems with expressJs. It seems that when you use app.listen() instead of server.listen() you get a weird problem. I did not take the time to shed light on the cause of the problem but what happens is that you get a HTTP 404 on socket.io ‘s URL in you browser side scripts. Socket.io s URL looks something like this: http://IP:8080/socket.io/1/?t=1375254151901 So if you get a HTTP 404 on this, then using server.listen() might be the solution to your problem :-) Finally here is my very simple sender.html code:

data: test

Senden message:

var host= window.location.host; console.log('hostname:'+host); var socket = io.connect('http://'+host,{transports: ['xhr-polling']}); /tell me when you connect *the way this works is that your callback gets called whenever *the socket receives a 'connect' event. */ socket.on('connect', function(client){ console.log('connected'); }); / Ths is taken from one of the examples. */ socket.on('news', function (data) { console.log(data); var div = document.getElementById('data'); div.innerHTML=data; //emit sends custom events socket.emit('my other event', { my: 'data' }); }); //use jquery ui to make a button $( “#button” ).button().click(function(){ var text= $('#message').val(); if (text=== undefined || text===””) {text=”null”;} console.log('sending:'+text); /send whatever text someone entered to the server first *The server will take care of broadcasting this for us. */ socket.emit('my other event', { my: text }); });

And last but not least the simple client.html file. It does not even use jQuery just yet :-)

data: test

var host= window.location.host; console.log('hostname:'+host); var socket = io.connect('http://'+host,{transports: ['xhr-polling']}); socket.on('news', function (data) { console.log(data); var div = document.getElementById('data'); div.innerHTML=data; }); //whenever you receive something just display it in a div socket.on('my other event', function (data) { console.log(data); var div = document.getElementById('data'); div.innerHTML=data.my; });