Question: 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 ?

Here is the code I'm using to calculate the latency :

```
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);

}

```

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. 

EDIT:
I checked the transport and right now I'm using websocket as the transport.

EDIT:
Problem has been fixed after editing code as per below answer (remove listener avoid them piling up) :

```
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);

```

Answer: 
There is a problem with this function. If you setinterval a socket.on it means you are adding many listeners to the same event and that will be increasing the execution at each iteration. So first thing, remove that socket.on to outside the setinterval. Isn't your code blocking somewhere also? Assuming your cpu is not bogged with other stuff running, it can be some other event delaying it.
1. Make sure you wait a second or so before measuring ping as the initial connection socket can clog up system resources, resulting in a longer ping time. Might want to add a timer to your ping check.
2. Localhost/development environment should have some overhead ping as it is probably not a dedicated webserver.