Running multiple games concurrently with node.js 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) {
    res.sendFile('/index.html');
});

var games = {
    //Games get put here
};

var gameCount = 0;

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

    var i = gameCount;
    var arrayOfGames = [];

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

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

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

    socket.on('join game',function(no){
        socket.join('room'+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.
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:


Your static server (express) 
Your room list server
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.