Multi Player game using Nodejs and Socket IO

Posted by Kishorevarma on Stack Overflow See other posts from Stack Overflow or by Kishorevarma
Published on 2012-08-10T16:59:52Z Indexed on 2012/10/16 17:02 UTC
Read the original article Hit count: 180

Filed under:
|
|

I am trying out multi player racing game using Node and Socket IO ,express . So I have tried simple example to see the latency between node server and the clients. I have a draggable image in client . when I move the image ienter code heren one client ,it has to move in all clients. so basically when I am moving the image I am sending the image position to the node server in a json format , then from there I am broadcasting to all clients. there is a ~approx 300ms latency from the time. following are the results.

Client 1 sending data to server at : 286136 (timestamp) Server received at : 286271

Client2 received data at : 286470 Client3 received data at : 286479 Client4 received data at : 286487 Client5 received data at : 286520

the latency between move from client1 to client5 is 384ms. its too hight for a racing game .. here is my server code.

var app = require('express').createServer();  
var io = require('socket.io');  
var http = require('http');  
var http_server = http.createServer();  
var server = http.createServer(app);  
server.listen(3000);  

var socket = io.listen(server,{ log: false });

socket.sockets.on('connection', function (client) {    
  client.on('message', function (data){  
        console.log("data arrived to server",new Date().getTime());  
// Below  both statements are giving same latency between the client 1 and client 5  
        client.broadcast.emit('message',data);    
        //socket.sockets.emit('message',data);  
  });  
});  

1) Is there any way to optimize the server code to reduce the latency?
2) is this expected latency using node and websockets ?
3) is socket io can't broadcast the data asynchronously (I mean at a same time) ?

Thanks Kishorevarma

© Stack Overflow or respective owner

Related posts about node.js

Related posts about socket.io