title: socket.io for a simple chat application
body: <p>I am making a simple chat application which has rooms and users join the room and chat. I don't have any authentication system so the users are random. I send the room id which I store in mongodb for users to perform join or leave room.  My problem is if I change the room in one browser the room chages in other browser.
  Both the browsers which i open reflect same changes. I guess I am wrong, can someone help me with how to set this up.</p>
  
  <pre><code>   var myApp = angular.module('myApp', []);
  myApp.factory('socket', function(){
      return io.connect('http://localhost:3000');
  });
  myApp.controller('controller', function($scope, $http, $timeout, socket){
      $scope.rooms = [];
      $scope.messages = [];
      $scope.name;
      $scope.roomId ;
      socket.emit('get Rooms', {});
      socket.on('got rooms', function(data) {
          $scope.rooms = data.rooms;
          $scope.$digest();
      });
     $scope.addRoom = function() {
         socket.emit('add Rooms', {roomName : $scope.roomName});
     };
     $scope.changeRoom = function(id) {
         console.log(id);
         socket.emit('change Room',  $scope.roomId, id);
  
         socket.on('room changed', function(data) {
             $scope.roomId = data._id;
  
             socket.emit('sent Msg', $scope.roomId);
             socket.on('get Msg', function(data) {
                 $scope.messages = data;
                 $scope.$digest();
             });
  
         });
     };
  // Adding Users name for first time
      $scope.addName = function(name) {
       $scope.name = name;
          socket.emit('user joined', name);
          socket.on('default group', function(data) {
             $scope.roomId = data._id;
  
              socket.emit('sent Msg', $scope.roomId);
              socket.on('get Msg', function(data) {
                  $scope.messages = data;
                  $scope.$digest();
              });
          })
     };
      $scope.sendMessage = function(msg) {
         socket.emit('send msg', msg, $scope.name,  $scope.roomId);
          socket.on('get Msg', function(data) {
              $scope.$digest();
          });
     };
  
  
  
  });
  </code></pre>
  
  <p>My nodeJs code.</p>
  
  <pre><code>    io.sockets.on('connection',function(socket){
      var roomsNow =[];
      var users = [];
      socket.on('get Rooms', function(data){
          Room.findAllRooms(function(err, doc){
              if(err)
              res.sendStatus(500);
              else if(doc){
                  _.each(doc, function(data){
                      roomsNow.push(data)
                  });
                  io.sockets.emit('got rooms', {rooms: roomsNow});
              }
          });
      });
    socket.on('change Room', function(oldRoom, newRoom){
          socket.leave(oldRoom);
          socket.join(newRoom);
          io.sockets.emit('room changed', newRoom);
  
      });
  </code></pre>