nodejs where to start?

First Just try to implement basic application and get a feel of the framework. There are several tutorials online for example:

http://net.tutsplus.com/tutorials/javascript-ajax/learning-serverside-javascript-with-node-js/

Documentation at http://nodejs.org/

http://howtonode.org/

Also there is a 70 min screencast by peepcode which costs 9$

https://peepcode.com/products/nodejs-i

There are also several apps on github, which you take a look at. Reading code is always the best.


I'd recommend looking at the project Socket.IO and Socket.IO-node. It uses HTML5 WebSockets if available, and falls back automatically and gracefully (no intervention required) to Flash sockets and XHR-polling as necessary

Here's a script to download the files:

mkdir socket.io
cd socket.io
git clone https://github.com/LearnBoost/Socket.IO.git --recursive
git clone https://github.com/LearnBoost/Socket.IO-node.git --recursive

Here's the server.js file:

var http=require('http');
var url=require('url');
var fs=require('fs');
var sys=require('sys');
var io=require('./socket.io/Socket.IO-node');   //adjust path as necessary...

var server=http.createServer(function(req,res){
    res.writeHead(200,{'Content-Type':'text/html'});
    res.write('Hello world');
    res.end();
});
server.listen(8000);

var socket=io.listen(server);

socket.on('connection', function(client){
  onConnection(client);
  client.on('message', function(){
    onMessage();
  })
  client.on('disconnect', function(){
    onDisconnect();
  })
});
function onConnection(client){
  console.log('connection');
  //client.connected;   //tests if connected
  //client.send("message");
  //client.broadcast("message");   //send to all other conns
}
function onMessage(){
  console.log('message');
}
function onDisconnect(){
  console.log('disconnect');
}

});

Run the above server with sudo node server.js

And here is your index.html to be run in a browser:

<script src="./socket.io/Socket.IO/socket.io.js" type="text/javascript" charset="utf-8"></script>   <!--Adjust path as necessary-->
<script>
var host="localhost";
var port=8000;

var socket=new io.Socket(host,{'port':port});

socket.connect();
socket.on('connect',function(){onConnect();}) 
socket.on('message',function(data){onMessage(data);}) 
socket.on('disconnect',function(){onDisconnect();});

function onConnect(){
  ///alert('connect');
}
function onMessage(data){
  //alert('message');
}
function onDisconnect(){
  //alert('disconnect');
  socket.connect();
}

</script>

Tags:

Node.Js