title: High Latencies In Socket.IO
body: <p>I'm developing a game using Socket.IO . I'm finding that the latencies are very high ie 250 milliseconds even when running on localhost. Is this normal ?</p>
  
  <p>Here is the code I'm using to calculate the latency :</p>
  
  <pre><code>function startLatencyPoller(socket,callback)
    {
        setInterval(function() {
        var startTime = Date.now();
        socket.emit('ping');
        socket.on('pong', function() {
          latency = (Date.now() - startTime) /2; //round trip time / 2
          callback(latency);
    });
  }, 2000);
  
  }
  </code></pre>
  
  <p>My question is whether its normal for Socket.IO to have latencies as high as 250 ms even on a local network and whether there are ways to  reduce it further. </p>
  
  <p>EDIT:
  I checked the transport and right now I'm using websocket as the transport.</p>
  
  <p>EDIT:
  Problem has been fixed after editing code as per below answer (remove listener avoid them piling up) :</p>
  
  <pre><code>setInterval(function() {
  
       var startTime = 0;
       var socketFunction = function() {
          latency = (Date.now() - startTime) /2; //round trip time / 2
          callback(latency);
          socket.removeListener('pong',socketFunction);
      };
  
      socket.on('pong', socketFunction);
      startTime = Date.now(); 
      socket.emit('ping');
  
  
  }, 5000);
  </code></pre>