Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs where to start?

Tags:

node.js

I've installed nodejs and ran couple of simple examples like opening a server on a port and listen on that port.

However, I still can not relate nodejs to web development. so to learn and implement nodejs I'm thinking about making tic tac toe using rails and nodejs. Is this possible?

I'm envisioning a multi-player tic-tac-toe game where if person 1 selects something, person 2 automatically sees it on their browser.

where should I start with something like this?

I have experience in rails but none in nodejs or nodejs + rails.

like image 664
josh Avatar asked Oct 30 '10 21:10

josh


2 Answers

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.

like image 97
Rishav Rastogi Avatar answered Oct 12 '22 04:10

Rishav Rastogi


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>
like image 27
Eamorr Avatar answered Oct 12 '22 06:10

Eamorr