Jump to content

How to make a Phaser game running on the server side


Sherlautten
 Share

Recommended Posts

Actually I'm making a multiplayer game, using Phaser and socket.io.

The game needs Physics, for both clients and a ball which is inside the game and noone controls, players just interact with it.

Till now I was doing all the work on the client side, and sending through the server the position and velocity of the clients, to others and updating there, but It didn't work well and the ball of course will work even worse, so I have though in running the game on the server side.

Problem is have no idea where to start, the game has several rooms, so I would like to run on the server a undetermined number of Phaser worlds.

I haven't found any tutorial about how to make a Phaser game running on the server of node.js, so plese, does someone knows where to start?

Link to comment
Share on other sites

4 hours ago, roninvn said:

If it is a must for you to use Phaser on server side, the only solution I could find is using it with a headless browser, like ZombieJS or CasperJS.

It is...

Is it very hard to use them? Which one has a better performance? And... there is it any tutorial?

Link to comment
Share on other sites

Honestly, I wouldn't try to use Phaser on the server side.  I would either implement my own physics engine to run on the server, or just use a third party physics library.  Phaser is doing too much stuff that just isn't necessary for your server side code.

Maybe try Box2D running in node, this guy did something similar a few years back:

http://paal.org/blog/2012/07/06/running-box2d-on-server-with-node-js-via-socket-io/

Link to comment
Share on other sites

18 hours ago, fillmoreb said:

Honestly, I wouldn't try to use Phaser on the server side.  I would either implement my own physics engine to run on the server, or just use a third party physics library.  Phaser is doing too much stuff that just isn't necessary for your server side code.

Maybe try Box2D running in node, this guy did something similar a few years back:

http://paal.org/blog/2012/07/06/running-box2d-on-server-with-node-js-via-socket-io/

Thanks, I read a thread about this topic which was made two years ago, and they also said that using Phaser on the server wasn't a nice idea, well I could try to make the game on Box2d physics, but I always though the performance of box2d wasn't very good, algo, I have no idea how to do it, but I have seen that code and I really think it will help me a lot!

I will try it the new weeks to see if I can make it work, thanks!

 

Did you try using Phaser.HEADLESS as your renderer first? In a NodeJS process somewhere locally?

Sorry, but... How's that? There is some example anywhere? I didn't hear about it.

I still don't understand, once Phaser is in a Headless mode, you call the update function depending of what game you want to update.

Link to comment
Share on other sites

Any velocity changes just need to be sent to the server or from the server to notify the players. Everything else is all client-side prediction (for all the players). No need to run a physics engine on the server, unless, you really want to. Scaling would be a PITA.

 

Just think of it as a global 'transmitStatus', look at play.treasurearena.com websocket frames in Chromes console. That will give you a good idea on what to send / receive from the sockets. It works really well too. (Notice, when a ball is bouncing off a wall, those physics are not sent across the pipe), but the action and ANGLE of a player shooting it is. The rest is all client side prediction.

Link to comment
Share on other sites

I don't think using physics on client side works. Latency and inconsistency would make the game hard to develop and maintain. And we are still not talking about anti-cheat. In multi-player model, client-side should be only rendering engine, everything else must be on server-side ( physics, game logic, user management,...)

 

Link to comment
Share on other sites

Phaser is not meant to run on Server Side. Its made to run in browser. And yes you can make a local installation on Node.js, install a http-server extension and run phaser app locally as I did. 

Here is a batch script I wrote to run a http-server extension of Node.js

C:
cd C:\Users\MyUserName\AppData\Roaming\npm\node_modules\http-server\bin
node http-server d:/PathToPhaserInstallationFolder/phaser2/public -a localhost -p 8081

But yes you can probably make a custom script and run in on Node.js if you some some heavy processing and then fetch the results to phaser somehow.

But todays browser perform rather well physics and other heavy duties. Make sure the code is optimized.

Link to comment
Share on other sites

I have never done this, but what I'm talking about is instead of using Phaser.AUTO as your renderer, use Phaser.HEADLESS. Looking at the Game constructor you might still need something that simulates a canvas (which is too bad).

But I literally mean write a node program that runs Phaser, see if that can be done at all. Phaser has issues around "require" so that'd be a hurdle. You also don't need all of Phaser; it sounds like you mostly need a physics world. Maybe you could run that code from Phaser directly.

Worry about communicating with a client second. Get the physics running in a server process first.

Link to comment
Share on other sites

Headless browser? Why would you make a game that has no rendering at all?

Socket.IO has API for client and server. There should be a script that handles Socket.IO on the server and probably you should add a script that handles the client side, connecting and sending data from the Socket.IO server. 

Read this relatively old book: https://www.packtpub.com/web-development/socketio-real-time-web-application-development

They have a web site: http://socket.io/

You need to install Socket.IO extension for Node.js

Ah and by the way, each tab in a browser can be different room, so if you have 3 tabs connected to the Socket.IO server in FIrefox and 2 tabs connected to Socket.Io server in Chrome, there will be 5 rooms, created. But yes if I recall well Socket.IO data structure was mess inside. 

Each room has its own string id.

I have to check again but be careful what you do, don't add rooms for no reason. :)

Link to comment
Share on other sites

On 19/2/2016 at 5:03 PM, spinnerbox said:

Headless browser? Why would you make a game that has no rendering at all?

Socket.IO has API for client and server. There should be a script that handles Socket.IO on the server and probably you should add a script that handles the client side, connecting and sending data from the Socket.IO server. 

Read this relatively old book: https://www.packtpub.com/web-development/socketio-real-time-web-application-development

They have a web site: http://socket.io/

You need to install Socket.IO extension for Node.js

Ah and by the way, each tab in a browser can be different room, so if you have 3 tabs connected to the Socket.IO server in FIrefox and 2 tabs connected to Socket.Io server in Chrome, there will be 5 rooms, created. But yes if I recall well Socket.IO data structure was mess inside. 

Each room has its own string id.

I have to check again but be careful what you do, don't add rooms for no reason. :)

I'm already using socket.io, And I have all the structure of rooms created, using javascript objects to store the rooms and sending packages(Chat messages and updating players's velocity thourh socket.io), all I need actually is to simulate a physics world on the server, box2d or Phaser's arcade/P2... Doesn't matter, I would adjust it to make it work, it's just that I don't know how to make run several functions of update, all the tutorials I have seen are just one only one room.

Link to comment
Share on other sites

If I understand well, what you are actually trying to do is run multiple update() from each tab of the game opened. That is dependent on browser implementation. 

Chrome has something like process per tab created, firefox is one process that collects memory bytes along time and ends up very memory heavy. For other browsers I am not sure. 

Do two tabs process at the same time I am again not sure how browsers are engineered. JavaScript is and probably will always be single threaded except in Node.js where back-end is handled by C++ but you write your programs in JS. 

So the only solution that I can think of is to learn how to write Node.js programs and use its ability to process multiple JS threads at the same time. Unfortunatelly I don't know well Node.js so I cannot help you how and why. 

And yes Node.js will process any ordinary JS code if written properly with its dependencies.

As mentioned previously you can run Phaser in headless mode in multiple Node.js threads. Just an I idea I got. Probably :)

There have been some efforts to bring threading to browsers, but it is still work in progress: http://www.html5rocks.com/en/tutorials/workers/basics/

http://stackoverflow.com/questions/17169742/do-all-tabs-in-a-browser-window-share-a-single-javascript-thread

Link to comment
Share on other sites

15 hours ago, spinnerbox said:

Do two tabs process at the same time I am again not sure how browsers are engineered. JavaScript is and probably will always be single threaded except in Node.js where back-end is handled by C++ but you write your programs in JS. 

So the only solution that I can think of is to learn how to write Node.js programs and use its ability to process multiple JS threads at the same time. Unfortunatelly I don't know well Node.js so I cannot help you how and why. 

JavaScript is single threaded, and thus, so is Node. With no exceptions.

The core event loop of Node is actually a very small piece of code and it is single threaded for a very very good reason.

Node can spawn and access child processes, but they are not threads of the same application. There are libraries that will expose processes as if they were threads, and there have even been some libraries that used C++ bindings to get actual threads, but they are not manipulated by JS.

Web workers within the browser are similar, they are not threads of the JS application, JS merely has a communication protocol with them.

Quote

And yes Node.js will process any ordinary JS code if written properly with its dependencies.

This is correct, the problem is that JS written for the browsers usually relies on globals it expects from the browser, which will throw errors unless shimmed in Node. Shimming and dealing with these consequences is not a trivial experience at the application level.

 

I respect that this is largely a terminology thing, but, JS is single-threaded, with no exceptions.

My two-cents is that you shouldnt even contemplate running Phaser on the server, I like absurd ideas generally as with enough shaking good ideas fall out of them but I'm not sure that will happen here. Restructure so you dont want Phaser on the server, you will never ever need it on a server. (edit: I'll add a caveat for automated testing, although this is still best done by firing up an actual browser i.e. your actual environment).

Link to comment
Share on other sites

If you were to run your game in environment pre-set by Zombie.js, Phaser probably would think there is the canvas element and it will start doing its job, all this run on Node.js. But yes you would not see what is going on visually. To run several instances just treat them as separate browsers or "http clients". So Node will serve each client part by part. 

First try to simulate headless browser and run your phaser game with node.js

I also found this extension, it is called JXCore, multi threaded processing for Node

Check this link with ton of stuff related to game networking and networking in general: http://www.gamenetworkprogramming.com/articles.html

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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