-1

I am making a multiplayer card game and am using NodeJS as my server with SocketIO.

My question is how should I be managing multiple game rooms (say an n number of game rooms) ?

Currently I have it setup as the server holds a JavaScript object which will hold all the game objects.

let games: {
    “room367”: Game(),
    “room193”: Game(),
    ....
    “roomN”: Game()
};

So each time a new game is created, this happens

if (games[“room193”].exists()) {
    joinRoom();
}

Should I even have an object to hold all the game objects ? Is there a better way to go about doing this ?

2 Answers2

0

No, you probably should not hold the rooms in memory.

Try to make your apis as stateless as possible. In this case you can imagine having more than a single instance of your nodejs server, so that game requests can be load balanced across multiple servers.

Or, imagine that you have more rooms in play than you can comfortably handle on a single server.

In both situations you don't want to tie a client to a specific server. This means you will want to create the room object per request. Probably persisting it on a database, rather than holding it in memory

Ewan
  • 70,664
  • 5
  • 76
  • 161
  • That makes a lot of sense, I didn’t even anticipate the load balancing across multiple servers. About persisting the game on a database, do you mean as in saving the actual game object ? Sorry if that’s not clear, I’m not sure how else to word it. – lost_programmer Jun 10 '19 at 02:00
  • Super late reply but I am very lost. I am a little lost on where to exactly persist the game states, if I use a DB, the server would potentially be making many thousands of DB requests constantly. Every time a player moves, that move would need to update the DB. When a game is finished, I don't want it to be persisted on a DB, it should just be destroyed, That is why I was leaning towards holding it in memory. I've been looking all over stackoverflow, but I can't seem to find a good answer on how to manage multiple game states on a server. – lost_programmer Aug 22 '19 at 03:41
  • a database is the right answer – Ewan Aug 22 '19 at 06:59
-2

I would recommend you to look into redis. Its a persistent data/central store. It stores the data in RAM/Memory instead of storing it on the disk like traditional DB's.