Take the 2-minute tour ×

small question. How can I calculate the ping of a WebSocket connection? The server is set up using Node.js and node-websocket-server, if that matters at all.

share|improve this question
What is "calculate the ping" supposed to mean? –  Jerry Coffin May 1 '11 at 18:31
To calculate the latency the connection has. –  Alexander May 1 '11 at 18:46
add comment

4 Answers

up vote 4 down vote accepted

To calculate the latency you really should complete the round-trip. You should have a ping message that has a timestamp in it. When one side or the other receives a ping it should change it to a pong (or gnip or whatever) but keep the original timestamp and send it back to the sender. Then the original sender can compare the timestamp to the current time to see what the roundtrip latency is. If you need the one way latency divide by 2. The reason you need to do it this way is that without some very sophisticated time skew algorithms, the time on one host vs another is not going to be comparable at small time deltas like this.

share|improve this answer
add comment

There is few ways. One that is offered by Raynos - is wrong. Because client time and server time are different, and you cannot compare them.

Solution with sending timestamp is good, but it has one issue. If server logic does some decisions and calculations based on ping, then sending timestamp, gives risk that client software or MITM will modify timestamp, that way it will give another results to server.

Much better way, is sending packet to client with unique ID, that is not increment number, but randomized. And then server will expecting from client "PONG" message with this ID. Size of ID should be same, I recommend 32bits (int). That way server sends "PING" with unique ID and store timestamp of the moment message sent, and then waiting until it will receive response "PONG" with same ID from Client, and will calculate latency of round-trip based on stored timestamp and new one on the moment of receiving PONG message. Don't forget to implement case with timeout, to prevent lost PING/PONG packet stop the process of checking latency.

As well WebSockets has special packet opcode called PING, but example from post above is not using this feature. Read this official document that describes this specific opcode, it might be helpful if you are implementing your own WebSockets protocol on server side: http://tools.ietf.org/html/rfc6455#page-37

share|improve this answer
Why downvote, any comments on that? –  Maksims Mihejevs Apr 20 '12 at 11:47
add comment

You can send a request over the web socket with Date.now() as data and compare it to Date.now() on the server.

This gives you the time difference between sending the packet and receiving it plus any handling time on either end.

share|improve this answer
Is there any efficient way to do that so it would automatically update? –  Alexander May 1 '11 at 19:08
@Alexander continuous polling or attaching time stamps to every web socket request. Do you need the latency for anything but information? It should only be used for logging not for changing the way your app works –  Raynos May 1 '11 at 19:10
It's just for logging, to see how the variations between the latency that the connected clients have, and to show on the page itself. –  Alexander May 1 '11 at 19:19
You can't compare date on the other end of the communication. It has to be compared on the origin of the message. Server and Client may not have the same time. –  Loïc Faure-Lacroix Sep 14 '12 at 23:47
add comment

Websockets have a ping type message which the server can respond to with a pong type message. See this for more info about websockets.

share|improve this answer
add comment

Your Answer


