title: Unity3D Socket.IO Basic Movement Latency
body: <p>I'm trying to make a server using with Node.JS and Socket.IO. I want to make an online game that just has simple movement event. I wrote some code, but it doesn't work very well. I have tonnes of latencies. You can check the video in below. By the way, I test it on my DigitalOcean servers or even in my LocalHost. What is the trick about my problem? I really would like to learn Network programming but I always get stuck.</p>
  
  <p><a href="https://youtu.be/3Hj4M_alBek" rel="nofollow noreferrer">Latency GAMEPLAY YouTube Link -- Especially check 13th seconds</a></p>
  
  <p><a href="https://github.com/gkaragoz/Unity-Node-JS---Movement" rel="nofollow noreferrer">Project GitHub link</a></p>
  
  <p>I will explain all of my works on below. But If you want to check details codes. You can visit GitHub project link.</p>
  
  <p>On the client side, I used Unity3D. If user key press to any arrow keys like upArrow, rightArrow than I send this information to the Server. By doing so the server knows which direction I would like to go.</p>
  
  <pre><code>if (Input.GetKey(KeyCode.UpArrow))
      {
          Movement = Vector3.up;
  
          JSONObject data = new JSONObject();
          data.AddField("x", Movement.x);
          data.AddField("y", Movement.y);
          socket.Emit("move", data);
      }
  // if Input.GetKey(KeyCode.RightArrow)
  // if Input.GetKey(KeyCode.RightArrow) .. etc
  </code></pre>
  
  <p>On the server side, I just used Node JS and Socket.IO. I create an interval function that only sends all players to clients. This interval function fires 60 times every 1 seconds. You can see the code in below.</p>
  
  <pre><code>setInterval(function() {
    io.emit('state', players);
  }, 1000 / 60);
  </code></pre>
  
  <p>By the way, when the server receives any move event it does this:</p>
  
  <pre><code>socket.on('move', function(data) {
    var player =players[socket.id] ||{};
  
    player.x =player.x+(data.x*0.1);
    player.y =player.y+(data.y*0.1);
    });
  </code></pre>