wilson wilson
wilson wilson

Reputation: 153

How to get the latency time for a one way trip from client to server

I was wondering if there was a way to get the time (in ms) between the client sending a message to the server and the server receiving that message

I cannot compare the time in milliseconds with the server and client using Date.now() because every device might be off by a few seconds.

I can find the time for a two way trip, logging the time when I send a message and logging the time again when I receive a message in return from the server. However, The time it takes for a message to get from the client to the server may not be the same as it is for the message to get from the server to the client on a two way trip. So I cant just simply divide this time by 2.

Any suggestions on how I can find this time or at least the difference between Date.now() on the client and the server?

Thanks in advance.

Upvotes: 6

Views: 1380

Answers (3)

Stijn de Witt
Stijn de Witt

Reputation: 42085

  • Start a precise timer (see Performance.now)
  • Request the server time from the server
  • The server responds with the time on his clock
  • When you receive the response from the server, stop the timer
  • Divide the timer reading by two to get the average latency between the client and the server
  • For the two clocks to be in sync, we expect the server time to be the average latency smaller than the time on the client clock when it received the response.
  • If not in sync, calculate the difference between the server clock and the client clock and adjust the client clock to match the server clock

To increase accuracy, send more than one request and average the latency over multiple requests.

Upvotes: 0

Brad
Brad

Reputation: 163262

I was wondering if there was a way to get the time (in ms) between the client sending a message to the server and the server receiving that message

No, there is not. At least, not without a common clock reference.

If I were to mail you a letter, you know what day you received the letter on but you don't know when it was sent. Therefore, you have no idea how long it took the post office to route and deliver the letter to you.

One possible solution is for me to date the letter. When you receive it, you can compare the received date to the date I sent it and determine how many days it was in transit. However, what if I wrote down the wrong date? Suppose I thought it was Friday when it was really Wednesday. Then, you can't accurately determine when it was sent.

Changing this scale back to computers, we'll have to use our realtime clock (RTC) to timestamp the packet we send. Even with reasonable accuracy, our RTCs might be set a minute off from each other. I could send you a packet at 01:23:01.000Z my time, and you might receive it 10 milliseconds later... at 01:23:55.00Z your time and calculate that it took 54 seconds to reach you!

Even if you synchronize with NTP, over the internet, that's potentially 10s to 100s of milliseconds off.

The way very accurate clock synchronization is usually done is via GPS receivers, which by their nature serve as an extremely accurate clock source. If you and I were both very accurately sychronized to GPS receivers, I could send you a packet and you could calculate how long it took.

This is generally impractical, which is why when we ping stuff, we use round-trip time.

Upvotes: 0

mihai
mihai

Reputation: 38543

You can achieve this if you first synchronize the clocks of both your server and client using NTP. This requires access to an external server, however you can configure NTP to be installed on your server as well (see ntpd)

There are several modules that implement NTP in node: node-ntp-client or sntp

Here's an example with node-ntp-client:

var ntpClient = require('ntp-client');

var clientOffset = 0;

ntpClient.getNetworkTime("pool.ntp.org", 123, function(err, date) {
    if(err) {
        console.error(err);
        return;
    }

    clientOffset = Date.now() - date;
});

When sending data to the server, send the timestamp as well:

var clientTimestamp = Date.now() - clientOffset 

Server would have its own offset. When receiving the package, it can calculate latency using:

var latency = Date.now() - serverOffset - clientTimestamp;

Upvotes: 1

Related Questions