1

I started using socket.io for the web game.

Using nodejs express server I tried to write simple test. In this test server writes timestamp to the client and client returns this timestamp. After this, server logs time used for sending there and back.

Result was: ping 500 ms +- 10 ms. And the same results were when I tried to do it with another computer in the local network.

So, I think, delay is so hi because of TCP Nagle algorithm or something similar. Does anybody knows how to tell nodejs server to flush data as it appears without waiting?

Server code:

var express = require('express'), socketio = require('socket.io');
var app = express.createServer();
var io = socketio.listen(app);
app.use(express.static(__dirname + ''));
app.listen(80);
io.set('transports', ['websocket', 'flashsocket']);
io.sockets.on('connection', function (socket) {
     socket.json.send({'message' : 'start'});
     socket.on('message', function (msg) {
         var time = (new Date()).getTime();
         console.log("PING WAS: "+(time - msg.timestamp)+" ms");
         socket.json.send({'timestamp' : (new Date()).getTime()});
     });
});

Client code:

window.onload = function() {
    socket = io.connect('http://localhost:80');
    socket.on('message', function (msg) {
        socket.json.send({'timestamp' : msg.timestamp});
    });
};
Kelly S. French
  • 12,198
  • 10
  • 63
  • 93
VorobeY1236
  • 11
  • 1
  • 2
  • possible duplicate of [nagle's algorithm in socket.io](http://stackoverflow.com/questions/9676344/nagles-algorithm-in-socket-io) – josh3736 Mar 30 '12 at 17:50
  • Yes, may be. But it's bad if Nagle algorithm is off only for web sockets. I need to use flashsockets also. – VorobeY1236 Mar 30 '12 at 18:18
  • I'd assume that socket.io would apply such an option to any transport implementation. Also consider that disabling Nagle only applies to your server -- you can't disable it on the client. – josh3736 Mar 30 '12 at 18:20

0 Answers0