Jump to content

Multiplayer game using phaser 3 and socket.io


Recommended Posts

I have problems writing a multiplayer game with phaser 3 and socket.io

I’ll explain it using a simple example: 2 players P1 and P2, try to kill each other using bullets B1 and B2.

Scenario A: Use Pure Javascript and socket.io, without any game engine.

A1. The socket.io server calculates the position of all players and bullets, and send these information to all players for every 30ms.

A2. Each player send [inputs] to the server on real time basis.  Of course, those inputs may arrive the server later then expected.

A3. Each player show objects in screen only according to the information provided by the server, without calculating the objects’ position on the client side.

A4. The game runs without any BIG problem since the Unique Game State is only calculated on the server side.

A5. However, without using game engine, I need to calculate the [Physics] of all objects by myself on the server side.


Scenario B: Use Javascript with phaser 3 and socket.io

B1. At time 0.000 second, the server ask both players start the game at the same time.

B2. At time 0.030 and 0.040, both players start the game accordingly.  (As you can see, the network of player P1 is faster)

B3. At time 0.200, player P1 fires a bullet B1 and starts to update screen using the phaser 3 engine’s [Physics].

B4. At time 0.280 (here the server takes 10ms to transfer this action from P1 to P2), P2 gets this action from the server, and starts to update screen using the game engine’s [Physics].  (You may probably notice that at this moment, the screens of P1 and P2 are different!)

B5. Suppose the bullet B1 is able to hit P2 at time 1.000.  At time 0.950, P2 moves away from the bullet and continues playing the game.

B6. As you may already figure out, the action that P2 moves away, arrives P1 at time 1.030, at that time, P1 already win the game!  At this moment, in P1’s screen, P1 win the game, but in P2’s screen, P2 is still playing the game!


It seems that phaser 3 is not suitable for multiplayer games.  Did I miss anything?  Thanks in advance for your help.

Link to comment
Share on other sites

1 hour ago, rich said:

The server should be the absolute authority on what is happening and when - never the client.

There are many successful multiplayer games created in Phaser.

Thanks for your response.  I do find some more information about writing multiplayer games with Phaser after my previous post.  I just don't know which algorithm I should choose.

For example, on the client side, an object A from the left hit another object B on the right and bounce back.

Algorithm 1: This event catches by the server (by running the Phaser physics on the server side), and sent to the client.  Before the client received this event, object A goes straight through object B to the right hand side of object B, then after the client received this event, object A "Suddenly" appear at the left hand side of object B to "Correct" its position.

Algorithm 2: This event "Predicts" by the client, it bounces back first and send this event to the server.  Then when the Phaser physics on the server side also got the same result, everything is correct.  However, it is possible that on the server side (may be the movement of object B is controlled by another player), object B has already moved away, so the bounce should never occur, in this case, the server sends the corrected positions of the objects back to the client, and the object A "Suddenly" appears at the right hand side of object B, to correct its position.

In either case, the movement of objects are not smooth but the final result is correct.  It seems that no matter which algorithm I choose, I need to run the Phaser physics on the server side.

So my question is, can I run the Phaser 3 physics on the SERVER side (node.js) for these kind of fast paced multiplayer games?


By the way, my son is able to run the Phaser 3 Single player game in his Cordova project, he uses a special technique so that the game can be run as an Android app, or an iPhone app, as well as running inside any PC browsers in Windows or Linux, with the same source codes!  Which means that it can be written once and run anywhere.  His Cordova project is a "Whatsapp" like chat app(text, images, voice messages and WebRTC), and he wants to add some games into it so that friends in the same chat group can play together.  So his only way is to find a Javascript game engine, instead of other game engines such as Unity, which is not suitable for this kind of app.

We really hope that Phaser can fullfill our requirement, and thanks again for your help.  My son also wants to thank you all for your effort in Phaser, he was able to write a simple game in pure Javascript in days, now he can write the same game in hours using Phaser!

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...