Search code examples
javascriptbrowserwebsocketmultiplayer

Common "tick" in online multiplayer games how to?


Here is a situation. Let's say I'm building an online 2 player game for browsers and one of the goals is to spawn something (an object) for both players on the same spot on the screen and make it go in a linear trajectory with same speed. Would it be best to implement "server tick" which moves the object (or objects) or to make that a job for a browser. I understand the bad implications of leaving it to the browser but I also fear for a big load on the server or something. Any ideas on how that is generally implemeted?


Solution

  • Conceptually, there are three ways to achieve that:

    • Pure Server-side Mode
    • Pure Client-side Mode
    • Hybrid Mode

    Let's see a few details on each of them.

    enter image description here enter image description here enter image description here

    Applying this to your case in the three different modes would mean:

    • In pure server-side mode, you send the coordinates of the trajectory in real time to both the browsers. Each browser simply receives the coordinates and displays the object at the received position. You can see a live example of this approach. Go to http://push.lightstreamer.com/ with two different browsers and move around the character in the right-bottom demo. All the movements of the characters and of the ball are decided, coordinate by coordinate, by the server and broadcast in real time to all the clients.

    • In pure client-side mode, you would share an initial world state, then each browser would calculate its own evolution of the world, with possibile divergence.

    • In hybrid mode, the browsers do their own calculations, as in pure client-side mode, but they periodically sync through a low frequency server-side broadcast. So, the physics run on both the server side and client side.

    The easiest solution is pure-server side mode, which is perfect in not-too-complex scenarios. To make it scalable and tolerant to network delays, you need a specific server, like that used in these demos [full disclosure: I'm involved with such server]

    To get more details, you can have a look at some resources I prepared in the past: