# NodeJS client gameloop running slightly faster than server gameloop

So I'm working on a real time multiplayer game in NodeJs (Client and Server). Both loops handle the same "physics" (movement at a constant rate) and both are running at 40hz or 40 times per second. I am using setInterval on both client and server with a delay of 40ms. The issue is that my server average delta between each tick is ~41 and my client average delta between each tick is ~40. This leads to results like this:

See in the last result, there is a difference of 46 ticks meaning the client is rendering about 1.8 seconds ahead of server time. My first guess on solving this is to sync the client every so often but that would lead to it "teleporting" backwards quite often due to how much it is desyncing.

• You've constrained the problem (specific to your ENV and your current attempt), but regardless this represents a massive, longstanding challenge in multiplayer networking that is difficult to solve in the abstract. The solution will probably depend more on tactics that address the specific needs of your game rather than on any "correct" answer. So it might not be a great question for SO's question format. That said, there are many questions here on the subject, a lot of research available elsewhere. For example, mod Andrew Russell has a whole video series on his solution to the problem. – Seth Battin Dec 23 '18 at 21:14

let currentTime = Date.getTime();