I am currently creating multiple games by adding objects of type Game as properties of an object called games. The names of these properties are set programmatically - for example, the first game will be a property of games called game1. Sockets joining this game will join the room room1.

Is this the right approach? It feels like there might be a cleaner way.

var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Game = require('./Game.js');
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res) {

var games = {
    //Games get put here

var gameCount = 0;

io.on('connection', function(socket) {

    var i = gameCount;
    var arrayOfGames = [];

        var gameInfo = {};
        gameInfo.name = games["game"+i]["getName"]();
        gameInfo.no = games["game"+i]["getNo"]();

    socket.emit('populate game list',arrayOfGames);

    socket.on('create game',function(options){      
        options.no = gameCount;
        games["game"+ gameCount] = new Game(options);       
        socket.join('room' + gameCount);
        socket.room = 'room'+gameCount;

    socket.on('join game',function(no){
        socket.room = 'room'+no;


var port = Number(process.env.PORT || 3000);
http.listen(port, function() {
    console.log('listening on *:3000');

I can show the client side code and class code if you request, but I don't feel it's relevant here.


1 Answer 1


First I would suggest separating your game server from your static server. You wouldn't want your static server clogging up your game server process. The advantage is that you can easily spawn more processes if you want, across different ports, or even across different machines. And if one goes down, it's just that one, not everything.

So that means the following should be written separately:

  1. Your static server (express)
  2. Your room list server
  3. Game server (which stores the game state)

Static server will be a standalone thing. Room list will share data with the game server. All your room listing server does is just get the active rooms from some DB. The game server will hold the game states across rooms. Creating a room would mean storing it's id to the DB for the server listing.

I also consider using UUIDs instead of counting. Create the id when a game object is created. This ensures that a game object will have a unique id upon creation, without relying on outer logic to supply it one.

  • \$\begingroup\$ Thanks! Where is the best place to learn about this approach? Are there any good examples out there? I've never made multiple node servers that can communicate with each other. \$\endgroup\$
    – Jonah
    Dec 3, 2015 at 23:35
  • \$\begingroup\$ @ANON I learned it from playing games and reading their forums :D. Ever wonder why MMO's can hold a thousand players concurrently? Every wonder why MMO's have status pages in their sites that say "Map server, login server, patch server, game server, chat server"? These are actual separate processes and may be (and most likely) living in separate hardware. \$\endgroup\$
    – Joseph
    Dec 3, 2015 at 23:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.