Skip to content

Instantly share code, notes, and snippets.

@sahat
Last active February 23, 2022 17:09
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save sahat/8364120 to your computer and use it in GitHub Desktop.
Save sahat/8364120 to your computer and use it in GitHub Desktop.
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');
});
});
@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