NodeJS and Websocket IO

Hello all,

on my current project I want to have more server driven realtime website for browsergames (like user actions that changes database content, so the user does not have to reload the page). This is a more general question then specific to BabylonJS.
After you login with your account, I use POST to switch between via including different content.

  1. Now I wonder if you connect by websocket io, would it be faster to connect just once?
  2. If it would be faster, then what could I use instead of POST and include()? Or can I keep connection after POST and just change active listener (socket.on)?

Hi Takemura.
Yes, websocket gives you a much faster connection - esp. if you want to sync between two players in near real time. Generally, you would listen for a change either on the client side or on the server side and run a function to handle that change as soon as it happens. There are some services that make it a lot easier than building from scratch. See:


However, if you’re just updating an occasional value in a database, no need to use sockets, http works just fine for that.


Currently everything is in 2D, like one page with a 2d map where you can view the world with images of (town, mountains, lakes, forests), i.e. some building is finished crafting the points of a town changes and this should be up-to-date if the user hovers over a town. I would let NodeJS server do the update in database of a finished building and sent to all users who have map page open. Also you would need to view armies (image) moving from town to town and they should disappear if they reached their destination and appear if someone sends an army.
Another page would be for overview of moving armies and buildings currently crafting, especially the armies would need to be up-to-date and no longer displayed in overview if they reached their destination. What would you recommend for this “simple” use-case?

If you have anything moving from place to place (x,y,z coordinates updating) and those coordinates need to be synced to other players so that everyone sees the same thing in near real time, websockets is the way to go.

This is a great video showing how to build a 2d multiplayer game using Firebase Realtime Database: Build a Multiplayer Game with JavaScript & Firebase - YouTube

1 Like

Hmm I don’t update coordinates, it would be enough if I send timestamps of start / end and points of start / destination, not sure if there would be disadvantages on this approach. Also I only use x and y not z coords.

If it’s just an occasional database update at the end of a build process, no need to use websockets.

1 Like

I think it wouldn’t be bad to go websockets regardless, especially if it gets necessary for idk health of units. I only know ajax post and get, from client to server, but i am curious how would you send from server to client?

The colyseus docs and tutorials for Babylon are really nice. If you go through, I think will see how easy that becomes.

Summed up though: With colyseus(and others) state is synchronized automatically for you between clients. You send simple messages with payloads from the clients, process them on the server and apply them to the state everyone shares ( or deeper in, can be more selective about who gets what )


Does Colyseus provide Real Time Database or I have to combine it with Firebase?

As far as I know Colyseus doesn’t provide any databases, only server stuff.

1 Like

I think for your concerns, the main difference between the two is that firebase writes to disk, colyseus is in memory on the server and data will be lost when it shuts down. colyseus is just a node websocket server, there’s nothing really special about it. The benefit it has over just plain json data is that when you define the schema using the type decorators, it encodes it into a smaller format than json so the network traffic is lower. Same concept as message pack, protocol buffers, flatbuffers, etc.

Also fwiw, there are many other ways to get firebase-like updates with self hostable databases like mongodb or postgres. Mongodb has built in change streams or if using pg or mysql, there are several libraries that will set it up for you. They pretty much just read the WAL / tail log and send the latest change.


I think firebase is more what I was looking for, but I don’t get it to run. I followed the steps until “firebase deploy”, but I always end up at this page:

Firebase Hosting Setup Complete

You're seeing this because you've successfully setup Firebase Hosting. Now it's time to go build something extraordinary!

Not sure what it is. Really difficult to setup a simple game example on firebase. Is there maybe a more simpler alternative that I can use on my local (XAMPP & NodeJS) server without all this hassle? Or maybe I set it up on github and someone has time to look through?

I think using your node server is probably best. Firebase hosting is not what you want, thats the web file hosting service like github pages, netlify, vercel, s3 and cloudfront, etc. For example, if you put an index.html in a public folder and ran firebase deploy, it’d be hosted at something like If you do want to setup a db u’ll need to add auth and the sdk on your client and add the real time db to your project in the firebase console

1 Like

You can read/write with a remote sql database in your Colyseus server’s Room code

Ex: A person connects to the room and provides you some authenticated ID. In the Colyseus code, you query their saved state from your database with said ID and set that on the object(s) representing them stored with the room. Everyone in the room now sees that player and their state. When a player does something you want to save, when their message arrives in your colyseus room, store whatever it is in your sql server and also make sure the Room’s state is updated.

1 Like

Thank you both for your help.

Regarding Firebase: Thanks I think I get it now. Also this helped me out:

On Colyseus: This means I have to somehow connect my mySQL db (or my nodeJS server, which uses the db?) and then Colyseus’s Room is some kind of image/copy of my db on clients and server, that updates mySQL db on changes?

The colyseus route:

Well your sql db is remote and you just connect remote from inside your colyseus “room” code that handles just about everything related to the session.

Also use an API/ORM over it that you build and use if not comfortable with raw sql as I would be :slight_smile:

1 Like