Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the response time (latency) of a client in NodeJS with sockets (socket.io)?

I'm trying to create a multiplayer game with NodeJS and I want to synchronize the action between clients.

What would be the best way to find the latency (the time that a request take to come back to the client) between the client and the server?

My first idea was that the client #1 could send a timestamp with is request, so when client #2 will receive the action of the client #1 he will adjust is action speed to remove the delay of the request. But the problem is that maybe the system date time of the two clients are not identical so it is not possible two know the reel delay on the request of client #1.

The other solution was to use the timestamp of the server, but now how can I know the latency of a client?

like image 657
FR6 Avatar asked Nov 01 '10 17:11

FR6


People also ask

How do I monitor socket IO?

Use telnet to connect to monitor.io and control a real-time list of sockets connected to socket.io . Use hjkl to scroll verticall/horizontally through the list of sockets, e to emit data to a specific socket, and b to broadcast data to all sockets.

What is Ping interval in socket IO?

Just an FYI to anyone coming across this answer but the newest version of socket.io has a default pingTimeout of 5000 ms (5 sec) per the docs linked to in this answer.


1 Answers

After reading all these answers...

...I still wasn't satisfied. I visited the official docs and well, well, well - the solution is already built-in.

You just need to implement it - check out mine:

Client

// (Connect to socket).  var latency = 0;  socket.on('pong', function(ms) {     latency = ms;      console.log(latency); });  // Do cool things, knowing the latency... 

 Server

var server = require('http').Server(app);  // "socket.io": "^1.7.1" // Set pingInterval to whatever you want - 'pong' gets emitted for you! var io = require('socket.io')(server, {pingInterval: 5000}); 
like image 74
Luka Avatar answered Oct 20 '22 00:10

Luka