Managing multiple games with Node.js and Socket.io

2.3k Views Asked by At

I am a newbie in node.js and socket.io. So I made a small project and tested some things with a small real-time chat, which works very nice. But I ask me, what's the best way to manage multiple games with node.js and socket.io?

Let's say, I want to create a website with some simple multiplayer card games. Each game should be a window with its own chat/room, a list of the players and a history for the game updates.

Is it better to set up all functions of each game in one big server.js file or run each game on its own process on the server (game1.js, game2.js...)?

2

There are 2 best solutions below

0
Alexandre Senges On

The best practice in this case would be to store the game data in a database, and use an authentication method (like cookies/JWT) to know what data to serve to which player at any time. This design pattern allows for much less overhead because the game's logic is completely disconnected from the game's data, which means it can easily scale, and if a crash or a disconnection occurs, nothing is lost. It also removes the dependency to individual connections.

1
Têco On

I recommend you to take a look at some good game networking frameworks like GameSparks, Photon etc to see how they work. Even if you want to make your own server-side code using NodeJS and SocktIO (which I think they are very suitable for your project), the frameworks I mentioned could give you some good clues about how to achieve your objective.

Game Networking

The most used architecture in game market for multiplayer gaming is one central server, often called master server, which is responsible for matchmaking, that is, matching players who want to play together in game rooms.

Once master server had matched players, it spawns a new dedicated service to handle all game logic, which is often called game server.

Making a multiplayer online game is not that simple, once you need to deal with problems like desynch, reconciliation, lag compensation among others. It all depends on your game type/genre. I think a simple card game will not involve much of that, but those are great study topics.

You can see more details about those terms in documentations like Valve's Source Game Networking and this article from Gabriel Gambetta.