This example demonstrates the basic use of Node.js and Sockiet.io to create socket connections between client and server.
Node.js is a server-side JavaScript framework created by Ryan Dahl. It’s based off of googles V8 JavaScript engine. Node.js provides a framework for running non-blocking server sided JavaScript and is geared for building scalable network programs. For more information on Node.js head over to their site. They have full api documents and a wiki.
Socket.io provides an asynchronous data connection between the client and server. It was created by Guillermo Rauch at LearnBoost. Head over to their site for more information.
You will need to install node.js and the socket.io node module. You can install the module using NPM (node package manager).
Install NPM
curl http://npmjs.org/install.sh | sh
Follow it up by installing the socket.io module
npm install socket.io
The code below creates an HTTP server that listens on port 80 and responds with statistics on the current number of socket connections. The code also listens and accepts socket connections. It logs the connection and disconnect of the clients by incrementing client count in the connection function and decrements client count in the disconnect function.
Create a new file with js extension and add the code below. Replace the <127.0.0.1> with an IP if needed. Run the code using the command node <filename>.js
To connect a client to the server add the html code at the bottom of the post to a file with an html extension and open it with a browser. Notice the connect and disconnect functions getting fired while you open and close the file using a browser.
//server that uses sockiet.io
var http = require('http');
var io = require('socket.io');
//setup a count variable to keep track of stuff
count = 0;
server=http.createServer(function (req, res){
if (req.url == '/'){
countit();
}
res.writeHead(200, {'Content-Type':'text/plain'});
res.end('node.js....is running!\n'+'There are '+clientCount+' client(s) connected\nThis server has handled '+count+' requests');
function countit (){
count=count+1;
console.log("Connection...increment count "+count);
console.log("http version: "+req.httpVersion);
}
});
server.listen(80,"127.0.0.1");
clientCount=0;
var socket=io.listen(server);
socket.on('connection',function(client){
clientCount++;
console.log("client is here "+clientCount);
client.on('message',function(){
console.log("client message");
client.send("message");
console.log("sent message");
})
client.on('disconnect',function(){
clientCount--;
console.log("client disconnected "+clientCount);
})
});
console.log("running server");
HTML code
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>This is a test page</title>
<head>
<script src="http://127.0.0.1/socket.io/socket.io.js"></script>
<body>
<script>
var socket = new io.Socket('127.0.0.1');
socket.connect();
console.log('socket connected');
socket.on('connect', function(){});
socket.on('message', function(data){
document.writeln('message recived: '+data);});
socket.on('disconnect', function(){});
</script>
Connecting to Node server!
</body>



XDA members GinoCorleone and Timo Kujala have been working on a Windows Phone 7 themed launcher for Android. For more info head over to XDA Developers to check out