title: socket rooms doesn't work
body: <p>I'm trying to create a private tchat room using the concept of socket.io rooms.  When the user clicks on a name of another user, a comparaison will start,if there is a room which contains both users, then it will join the room, otherwise a new room with both users will be created.
  When a message arrives, the message is sent to the corrsponding room, but my code doesn't work: the comparaison is done, the corresponding ids are found, but the messages are send for everyone connected to the tchat.</p>
  
  <p>Here is the code of the server </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var express = require('express');
  var app = express();
  var server = require('http').createServer(app);
  var io = require('socket.io').listen(server);
  
  var port = process.env.PORT || 8080;
  
  var cookieParser = require('cookie-parser');
  var session = require('express-session');
  var morgan = require('morgan');
  var mongoose = require('mongoose');
  var bodyParser = require('body-parser');
  var passport = require('passport');
  var flash = require('connect-flash');
  
  //session-store
  
  
  var MongoStore = require('connect-mongo')(session);
  
  var configDB = require('./config/database.js');
  mongoose.connect(configDB.url);
  require('./config/passport')(passport);
  
  
  var Room = require('./app/models/room');
  var User = require('./app/models/user');
  
  //session
  var sessionMiddleware = session({
    secret: 'anystringoftext',
    saveUninitialized: true,
    resave: true,
    store: new MongoStore({
      mongooseConnection: mongoose.connection
    }),
  
    ttl: 2 * 24 * 60 * 60
  });
  //fonction btoa
  var btoa = require('btoa');
  var atob = require('atob');
  
  //session with socket.io
  io.use(function(socket, next) {
    sessionMiddleware(socket.request, {}, next);
  });
  
  io.on('connection', function(socket) {
  
        socket.on('startchat', function(name) {
  
          //my id
          var idUser = socket.request.session.passport.user;
          //search for my name
          User.findById(idUser, function(err, user) {
            if (err) throw err;
            if (user) {
              //my name
              nameUser = user.local.username;
  
  
              if (name &lt; nameUser) {
                var users = name + ',' + nameUser;
              } else {
                var users = nameUser + ',' + name;
              }
              //encrypt the string
              var roomid = btoa(users);
  
              //searching fo ra corresponding id
              Room.find(function(err, room) {
                if (err) throw err;
                if (room) {
                  var dRoom = '';
                  room.forEach(function(room) {
  
  
                    var idRoom = atob(room.id).split(',');
                    //comparaison
  
                    if ((idRoom[0] == nameUser &amp;&amp; idRoom[1] == name) || (idRoom[0] == name &amp;&amp; idRoom[1] == nameUser)) {
                      dRoom = room.id;
                      console.log(idRoom, room.id);
                      //join the room
                      socket.join(dRoom);
                    }
                  });
  
                  if (dRoom == '') {
                    //we didn't find an id which corresponds so we create a room with this id
                    dRoom = roomid;
                    var newroom = new Room({
                      id: roomid
                    });
                    newroom.save();
                    console.log('created');
                    console.log(atob(roomid).split(','));
                    console.log(roomid);
                    //join the room
                    socket.join(roomid);
                  }
  
                }
  
                //when msg is emitted
                socket.on('message', function(msg) {
                  console.log('message', dRoom);
                  //emit the msgs to this room
                  io.sockets.in(dRoom).emit('msg', msg);
                });
              });
  
            }
  
          });
  
  
        });
  
        app.use(express.static('public'));
  
        app.use(morgan('dev'));
        app.use(cookieParser());
        app.use(bodyParser.urlencoded({
          extended: false
        }));
        app.use(sessionMiddleware);
        //passport and sessions
  
        app.use(passport.initialize());
        app.use(passport.session()); // persistent login sessions
        app.use(flash()); // use connect-flash for flash messages stored in session
  
  
        app.set('view engine', 'ejs');
  
        require('./app/routes.js')(app, passport);
  
        server.listen(port, function() {
          console.log('Server running on port: ' + port);
        });</code></pre>
  </div>
  </div>
  </p>
  
  <p>the client.js</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(document).ready(function(){
    var socket = io();
  
    socket.on('connect', function() {
     // Connected, let's sign-up for to receive messages for this room
     $('#chat').hide();
  
     var start;
     window.start = function (id,name){
       $('#chat').show();
       $('#chatHeader').html(name);
       socket.emit('startchat', name);
    }
  
  
    $('#send').submit(function(event){
      event.preventDefault(); //empeche la page de se recharger
      var msg = $('#message').val();
       if(msg !== ''){
        socket.emit('message', msg);
           }
     $('#message').val('');
  
     });
    });
  
    socket.on('msg', function(msg){
      $('.messages').append('&lt;div&gt;'+ msg + '&lt;/div&gt;');
    });
  
  
  
  
  
   });</code></pre>
  </div>
  </div>
  </p>