Jump to content

Phaser + NodeJs + Websocket = Multiplayer (Demo inside)


Recommended Posts

Last night I installed NodeJS and wanted to see if a multiplayer real time phaser game could work.

(Or more to the point, if I could make that work)


I think it will be possible.


Check out this small proof of concept:




Edit: The test is offline for now.



If you move around the stuff on the screen, everybody currently on the page should see it move too.

(And you should see their moves.)

If nobody is there (quite likely) a second browser tab will do the trick.

(although you might not see the dragging animations because phaser stops on losing focus and it will only update when you refocus that browser.)



Let me know what you guys think.


Is there interest in short tutorial, any questions? :)

Has this been done already, are there tipps and tricks out there? (please post links if possible)


My next goal is a multiplayer falling Blocks (see signature for singleplayer version).

Link to comment
Share on other sites

I used to play with this lib and love it :-)


If you want to use WS, keep in mind to implement some sessions so your players could share the right game session.

You can do that in the ws message (msg.session, msg.typ, etc) or you can use the ws-protocol header (ex : "jpfllblck978583256') although the RFC tells to use only IANA names suffixed by your domain (ex : "xmpp.jppresents.net"). The server won't complain as ws-protocol is just a string and you can write whatever you want : it's your service after all !


Then server-side, you can filter and broadcast browser messages only to their own sessions.


You can broadcast messages or send messages to every connected client except the one which just emitted the current message... or only specific clients. It needs a bit more logic though.


not sure my english is really clear so far, arf


I attended a french academic network conf last december and talk about ... ws

If you can understand some french :

video : http://video.renater.fr/jres/2013/index.php?aid=12

slides : http://jerome.bousquie.fr/ws_slides.pdf

article : http://jerome.bousquie.fr/ws_article.pdf


But there's plenty of good documentation in english on the Web :-)

Link to comment
Share on other sites

Thanks for the input.


Session Management is exactly what I'll be looking at next.


I am not sure yet how to figure out which client is which. 

When receiving a message it's easy, I can just place it in the message as you described.

I am just not sure .

When trying to write down my problem with identifying who to send to, I went over the source again, and I guess I figured it out. :)


My french isn't anywhere near the level that I could listen to tech talks. (15 years ago I nearly failed the class in school .. and haven't used it since)


The good news, both our english is good enough to discuss websockets, even though it's a second language to both of us :) - and our JS seems fine too ;)

Link to comment
Share on other sites

Thank you :-)


I tested many libs (including the famous socket.io) and I used this one for my prototypes : https://github.com/Worlize/WebSocket-Node

It's as fast as einaros one but it has got a better documentation and give access to more protocol parameters imho.

doc : https://github.com/Worlize/WebSocket-Node/wiki/Documentation


Here are some tips to manage session or communicate to a very specific connection (client) : https://github.com/Worlize/WebSocket-Node/wiki/How-to:-List-all-connected-sessions-&-Communicating-with-a-specific-session-only

It can be easily adapted to einaros.


Have fun with WS and JS  :D  !

Link to comment
Share on other sites


I am actually working on multiplayer games with websockets and I am having problems to hosted them.


Most of the free hosting fall down to xhr-polling (using socket.io and node)

i notice your game is working fine using real websockets, could you let me know which host are you using?


Thanks in advance!

Link to comment
Share on other sites

Nice work


your server side logic seems to broadcast every 16ms which is the common desktop browser framerate... this doesn't guarantee the connected clients will receive the messages every 16ms due to network latency which could differ between clients


It's very hard to implement a real synchronized server game engine for all clients especially on the Web.

Maybe is it better not to focus on accurate synchronization besides when designing HTML5 games ?


Staying on javascript (and nodejs), there are some, more or less complex, game server frameworks if you don't want to reinvent the wheel :






I didn't test any one of these ...

Link to comment
Share on other sites

  • 1 month later...
  • 3 months later...
  • 8 months later...

A while ago I did a small test game which uses node and websockets as well. 


You can try it here: http://xicombd.github.io/phaser-multiplayer-game/public/


And the source code is here: https://github.com/XicoMBD/phaser-multiplayer-game



Hope it is useful for someone  :)


Thanks for that clean working example xicombd.  Almost a year later, any fresh insights on your approach?   Thx.

Link to comment
Share on other sites

hello! :)


i'm doing a pong game with phaser, node.js and socket.io... i was wondering two things:


1) how to make phaser not pause the game when the browser loses focus?

2) [most important] May i have to drop socket.io in favour of https://github.com/websockets/ws ? i have the dead line on next week and my game is still not running fine on localhost. may i need a faster socket library? does it supports events and rooms like socket.io?

Thank you for your time!

Link to comment
Share on other sites


  • Recently Browsing   0 members

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