title: Client-server communication, websockets latency
body: <p>Short question:</p>
  
  <p>Can latency, when a client is sending packets to the server suddenly change a lot? </p>
  
  <p>Long question(the problem I've got):</p>
  
  <p>I'm making a html game using websockets and currently working on client side input prediction, I'm using the same code for client and for server to check for collisions, calculate new player positions, 30times/second. This is how I update player movement on both server and client side:</p>
  
  <pre><code>if(controls.pressingRight === true)
  {
      if(controls.dx &lt; settings.MAX_X_SPEED)
      {
          controls.dx += settings.DELTA_X_SPEED;
      }
      else
      {
          controls.dx = settings.MAX_X_SPEED;
      }
  }
  if(*noCollisions*)
  {
      game.me.x += controls.dx;
  }
  </code></pre>
  
  <p>This way the player movement is kind of smooth. When a user press and holds down "D" keyboard button conrols.pressingRight value is changed to true, and it is true until player releases the "D" button. The reason I wrote my code is because I'm not sure if the problem is in my code or it has something to do with the change of latency. For example I click D button and quickly release it and my player on my client canvas moves like ~7pixels BUT on my server side player moves like 20pixels or more to the side(though sometimes both client and server side moves the same amount of pixels). I'm guessing that the problem is because when I press button down to move the player the latency might be low, let's say 20, but when I release the ping might be like 200 which means the packet was delivered later than it should've and because of that server still had my value of "pressingRight" as true. Am I missing on something?</p>