amulllb
amulllb

Reputation: 3166

socket io node js to get server time every one second performance

I want to display realtime server time on my webpage and would like to know if I can use socket.emit periodically (every 1 second) for that? And if I do use it, what is the performance impact?

Alternatively, I can simply get the timezone once from the server and just use browser's date object to get current time and convert the timezone to display realtime. But, in this case, I am assuming that my server's date-time setting is correctly configured.

Basically, I just want to know whats the performance impact on the server/client when i run a socket.emit every 1 second using setInterval

Upvotes: 0

Views: 1231

Answers (1)

jfriend00
jfriend00

Reputation: 707396

There's no point in sending server time constantly. Just time a round trip packet, send the server time once, add the transit time and then compare the server time to the local time to create an offset. Then, just use the local time + the offset from then on. It will be at least as accurate as constantly sending the server time and will be more efficient.

The idea is that the client clock and the server clock run at the same speed so you just need to know what the offset is between the two and then you can use the client clock plus the offset.


The performance impact of sending a small message every second to one client is probably no big deal, but if you have lots of clients connected, it could start to be significant and further could cause delays between the time each client is sent the packet thus rendering the sent time not that accurate.

Upvotes: 3

Related Questions