Category Archives: NodeJS

NodeJS for Socket communication.

NodeJS shell script to run

/etc/init.d/nodejs-start (chmod 755)

# Source function library.
. /etc/rc.d/init.d/functions

start() {
	/usr/local/bin/node /var/www/html/node_socket_server.js >/dev/null 2>&1 &
	echo "started Successfully."
	return 0
}
stop() {
	killproc node
	echo "Stopeed"
	return 0
}
status() {
	echo "Status"
}
restart() {
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status
		;;
	restart)
		restart
		;;
	*)
		echo $"Usage: $0 {start|stop|status|restart}"
		exit 1
esac

Make link to rc3.d
rc3.d # ln -s /etc/init.d/nodejs-start /etc/rc3.d/S100nodejs

filename start with ‘S’ => start (Run when server start)
start with ‘K’ => kill (Run when shutdown)

Posted in NodeJS, Tech Note | Tagged , | Leave a comment

Simple sample socket IO using NodeJS

For my project, I need long polling socket connection.

So I choose NodeJS for this project. It is very nice and it make big difference to my web programming.

Here is my note for NodeJS

Firstly, we need install Nodejs and NPM (express and socket.io)

(
## Install NodeJS ##

# cd /root
# wget http://nodejs.org/dist/node-v0.4.12.tar.gz
# tar -xzvf node-v0.4.12.tar.gz
# cd node-v0.4.12
# ./configure
# make
# make install

## Install NPM ##

# curl http://npmjs.org/install.sh |sh

## Install Socket.io (Install on html root folder)##

html# npm install socket.io

## install express (Install on html root folder)##

html# npm install express
)

Server Side :

var url = require("url");
var express = require('express');

var http = require("http"),
	 io = require("socket.io"),
	 server = express.createServer();

server.configure(function () {
  server.use(express.static(__dirname + '/public'));
});

server.get('/', function (req, res) {
  res.render('index', { layout: false });
});

server.listen(3000, function(){
	var addr = server.address();
	console.log('   app listening on http://' + addr.address + ':' + addr.port);
});	 

var socket = io.listen(server);

socket.sockets.on("connection", function(client){
	var user;
	console.log('Connected!');

	client.send("hello! Welcome to Socket server!");

	client.on("message", function(msg){
		console.log("Arrived Message from Client!",msg);
		client.send("I got  : "+msg);
	});

	client.on("setId", function(id){
		user = id;
	});

	var sendingMsg = setInterval(function(){
		client.send("Your ID : "+ user);
		client.broadcast.emit("broadcast","This message is for everybody!");
	},1000);

	client.on("disconnect", function() {
		console.log("Client Disconnected!");
	});
});

Client Side :

[script src="http://10.0.111.10:3000/socket.io/socket.io.js"][/script]

[script type="text/javascript"]
function connectSocketServer(){

	var conn = io.connect('http://10.0.111.10:3000');

	conn.on('connect', function(){
		console.log('Connected');
	});

	conn.on('error', function (e) {
	  console.log('System', e ? e : 'A unknown error occurred');
	});

	conn.on('message', function(msg){
		console.log("Received Msg : "+msg);
	});

	conn.on("broadcast", function(msg){
		console.log("Broadcast Msg : "+msg);
	});

	conn.emit("setId","ABC");

	conn.on('disconnect', function(){
		console.log('Disconnected');
	});

	conn.send('Hello, Socket Server');
}
[/script]


*How to sent message to only specific user?

When client access, save client to array object.

and use the object when send message,

Ex )

var user = {};

socket.sockets.on("connection", function(client){
var id;
.
.
.
client.on("setId", function(uid){
		user[uid] = client; // Save client object
		id = uid;
	});

 client.on("disconnect", function() {
        delete user[id];
        console.log("Client ID : "+id+" Disconnected!");
    });


var sendingMsg = setInterval(function(){
		user['ABC'].send("This message deliver only to ABC user!");
	},5000);
Posted in NodeJS, Tech Note | Tagged , | 2 Comments