Skip to content

Instantly share code, notes, and snippets.

@sahat
Last active Feb 23, 2022
Embed
What would you like to do?
Calculate client-server latency using socket.io
var socket = io.connect('http://localhost');
var startTime;
setInterval(function() {
startTime = Date.now();
socket.emit('ping');
}, 2000);
socket.on('pong', function() {
latency = Date.now() - startTime;
console.log(latency);
});
io.sockets.on('connection', function (socket) {
socket.on('ping', function() {
socket.emit('pong');
});
});
@Foxhunt
Copy link

Foxhunt commented Jan 5, 2015

Hi,

what do you think about using the "pong" function as a callback in the "ping" emit?

Like so:

Client side:

        socket.emit('latency', function () {
            latency = Date.now() - startTime;
            console.log(latency);
        });

Server side:

        socket.on('latency', function (fn) {
            fn();
        }); 

@flower1024
Copy link

flower1024 commented Feb 2, 2015

sounds very dangerous to allow clients to send code ;)

@Jimmy-Dinis
Copy link

Jimmy-Dinis commented Mar 2, 2015

Yes, very dangerous !

@wiledal
Copy link

wiledal commented Mar 14, 2015

You are not sending code, the callback will only run on the client 💃

@maffiou
Copy link

maffiou commented May 5, 2015

Not sure about the code:

  • You add a new handler every single time you send a ping
  • If your ping time is more than 2s, you'll get rubbish (might be an issue on mobile device where latency is large or if you want to ping more often than 2s, with this implementation, you cannot ping faster than the round trip, which you don't necessarily know).

I would:

  • move the socket.on before the setInterval()
  • send the timestamp with the ping and change the server side to return it with the pong, and do the math upon receiving the response...

@NewCompte
Copy link

NewCompte commented May 29, 2015

Also he didn't put a "var" before latency.

@ColonelBundy
Copy link

ColonelBundy commented Dec 1, 2015

@flower1024 @steel-finger sounds like you two don't know what a callback is. very dangerous !

@johnpattison
Copy link

johnpattison commented Dec 8, 2015

Can be kind enough to put an example here that isn't "very dangerous"?

@danneu
Copy link

danneu commented Dec 13, 2015

@jpattisoninc Foxhunt's example isn't dangerous.

// Client

socket.emit('latency', Date.now(), function(startTime) {
    var latency = Date.now() - startTime;
    console.log(latency);
});

// Server

socket.on('latency', function (startTime, cb) {
  cb(startTime);
}); 

While it looks magical that the server can "call" cb(), calling cb() on the server really just sends a message back to the client telling it to execute its callback with some given arguments.

It's more clear if you try console.log(cb.toString()) on the server.

@tcaer
Copy link

tcaer commented May 10, 2017

Is this is milliseconds? Thanks!

@ForgeableSum
Copy link

ForgeableSum commented Jul 22, 2017

ping and pong events are already used by socket.io for heartbeats so it is not advisable to create custom ping/pong events (name them something different). socket.io will send ping/pong messages (heartbeats) automatically and you can control the frequency with the connection options object (I believe default is 20 sec).

@aRandomKiwi
Copy link

aRandomKiwi commented May 13, 2018

Hi @ForgeableSum, so how can we use Socket.io builtin ping/pong to calculate latency ? i didn't found any documentation about that.

@NeXTs
Copy link

NeXTs commented Sep 25, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment