Phaser on Node.js (new version)

Phaser on Node.js

Allows you to run Phaser 3 Games (including Phaser’s physics engines) on Node.js.

It has been a while since I released @geckos.io/phaser-on-nodejs, but I have now finally added the possibility to set a custom frame rate and to load images and json files (TileMaps) directly into your server side Phaser Games.

phaser-with-nodejs

Custom Frame Rate

// set the fps you need
const FPS = 30
global.phaserOnNodeFPS = FPS

// prepare the config for Phaser
const config = {
  type: Phaser.HEADLESS,
  scene: [MainScene],
  fps: {
    target: FPS,
  },
  ...
}

Loader

preload() {
  // use a relative path instead an absolute
  this.load.spritesheet('dude', '../assets/dude.png', {
    frameWidth: 32,
    frameHeight: 48,
  })
}
4 Likes

Hi, I had a look and tested your games examples and looks working really nice, good work!
I’m just curious on this, don’t get me wrong, but I don’t get the point of why running it on Node.js or what’s the main idea behind this…
Which are the use cases? testing? checking untrusty clients? simply play with it?
Thanks for sharing it!

1 Like

If you are making a multiplayer game which uses physics, you can’t calculate the physics on the client side. Each player would see a different world.

In this case, you have to run your physic on the server and send a snapshot of the world to each client.

Hope this clarifies it :slight_smile:

2 Likes

I followed the tutorial and I get a phaser not being defined server-side. Does that have to do with jsdom and canvas? I haven’t used either before so am a little confused. Any help would be appreciated.

You have to install Phaser as usual npm i phaser :slight_smile:

I just added a basic setup example to the [README.md].(https://www.npmjs.com/package/@geckos.io/phaser-on-nodejs#basic-setup)

Hey Yannick. I appreciate the fast response. Yeah I didn’t require phaser so that make sense. It’s up and running, thank you very much.

1 Like

Hey Yannick. I had another question I was hoping you could help me on. I’m trying to understand general authoritative server architecture when it comes to phaser. Right now I have my client sending player input to the server, but there’s no way to keep the server authoritative since the physics are not running on it. I have phaser-on-node.js running on my server now, but my question is how do I implement my existing code so that I can do say, setVelocity for playerId when player sends movement input? Do I copy and paste the code over to the server? I hope my question makes sense, just having trouble conceptualizing how the headless authoritative server displays client actions on the client’s screen. Thank you.

  • The server runs all the physics.
  • The client does not run physics at all. He just receives the positions of the images and renders them on the screen.

Basically, the client send the inputs > the server adds the velocity to the player and updates the physics > the server send the new position of the player to the client > the client renders it.

I once made a very basic example. Hope it helps :slight_smile:


There are also some more complex techniques to apply like “Snapshot Interpolation”, “Client-Side Prediction and Server Reconciliation” and “Lag Compensation”. But these are advanced and not needed in most multiplayer games. See this video for more information.

I’ve always appreciated your insight and contributions. I was wondering if you’ve seen what others are doing with Networked Physics??

Thanks :slight_smile:

Yes, I’m familiar with it. This is Glenn’s Website. The same guy as in the video I posted above.

1 Like

how to use with multiple active phases?

Just use multiple new Phaser.Game(config)

the project is very good, but I can’t import using directories
class MainScene extends Phaser.Scene { constructor() { super('MainScene') } preload(){ this.load.image('sky', './public/assets/sky.png'); //error this.load.image('sky', 'public/assets/sky.png') // error }

error /
C:\Users\name\Desktop\server\node_modules@geckos.io\phaser-on-nodejs\lib\fakeXMLHttpRequest.js:21
throw err;
^

[Error: ENOENT: no such file or directory, open ‘C:\Users\name\Desktop\server\node_modules@geckos.io\phaser-on-nodejs\lib\public\assets\sky.png’] {
errno: -4058,
code: ‘ENOENT’,
syscall: ‘open’,
path: ‘C:\Users\name\Desktop\server\node_modules\@geckos.io\phaser-on-nodejs\lib\public\assets\sky.png’
}

use path to resolve the the correct path.

const path = require('path')

// check if one of them gives you the right path
console.log(path.resolve(__dirname, '../public/assets/sky.png'))
console.log(path.resolve(__dirname, 'public/assets/sky.png'))
1 Like

work