title: Socket.io with multiple namespaces?
body: <p>I have a route that listens to a particular route id.</p>
  
  <pre><code>/**
   * Chat app routes.
   */
  app.get('/games/:id', gameChatController.chatRoom);
  </code></pre>
  
  <p>Based on the route <code>id</code> I can determine what game the chat is about. </p>
  
  <p>My question is how do I use this to pass into socket.io?</p>
  
  <p><strong>bin/www</strong></p>
  
  <pre><code>/*
  * setup socket.io, and socket-session
  */
  var socketIO = require('socket.io');
  var io = socketIO(server);
  var SessionSocket = require('session.socket.io');
  var sessionSockets = new SessionSocket(io, app.sessionStore, app.cookieParser);
  var setEvents = require('../events');
  setEvents(sessionSockets);
  </code></pre>
  
  <p><strong>events/index.js</strong></p>
  
  <pre><code>'use strict';
  var redis = require('redis');
  var sub = redis.createClient();
  var pub = redis.createClient();
  sub.subscribe('chat');
  
  module.exports = function(io) {
  
      // here io basically is sessionSockets
  
      // TODO: Set a namespace, make it dynamic
      var theRoom = 'fifa14';
      var namespaceRoom = io.of('/'+theRoom);
  
      namespaceRoom.on('connection', function(err, socket, session) {
          if (!session.user) return;
  
          console.log(socket);
  
      }
  }
  </code></pre>
  
  <p>So what I am asking is how can I build this so that I can easily pass the current state of the app to match the state of the socket namespace, so users inside a particular game can only chat inside that?</p>
  
  <p>Because I dont think doing something like the below is very efficient</p>
  
  <p><strong>controllers/chat.js</strong></p>
  
  <pre><code>/**
   * GET /
   * ChatRoom page.
   */
  exports.chatRoom = function(req, res) {
  
  /*
  * setup socket.io, and socket-session
  */
  var socketIO = require('socket.io');
  var io = socketIO(server);
  var SessionSocket = require('session.socket.io');
  var sessionSockets = new SessionSocket(io, app.sessionStore, app.cookieParser);
  var setEvents = require('../events');
  // so here I can pass the route parameters, not efficient!
  setEvents(sessionSockets, req.params.id);
  
    res.render('chatRoom', {
      roomName: req.params.id
    });
  };
  </code></pre>
  
  <p>So now I can pass a new game id into the <code>setEvents()</code>, but I think this can be better.</p>
  
  <pre><code>'use strict';
  var redis = require('redis');
  var sub = redis.createClient();
  var pub = redis.createClient();
  sub.subscribe('chat');
  
  module.exports = function(io, gameId) {
  
      // here io basically is sessionSockets
  
      // Now I can pass the proper id
      var namespaceRoom = io.of('/'+gameId);
  
      namespaceRoom.on('connection', function(err, socket, session) {
          if (!session.user) return;
  
          console.log(socket);
  
      }
  }
  </code></pre>