title: Setting up a socket.io room for a specific group based on url. Node.js, Express, Angluarjs
body: <p>I have a web application running on node using express, angular, mongodb and socket.io where users belong to groups. In those groups I need to set up a chat socket whereby only the people who belong to that group can see the messages specific to that group and only send messages to other users in that group. At the moment I have managed to set up the socket.io chat room based on the guidelines on their site, but the messages sent are visible to all other groups. I have read that I need to set up certain rooms, but I am not sure how to set it up so that the room is specific to the group.</p>
  
  <p>The group is held at a domain: for example... www.mydomain.com/groups/groupId/room</p>
  
  <p>The groupId is a unique Id which is generated by MongoDB. I am using Angularjs on the client side, and have a group controller for the groups. If anyone has any ideas how to achieve this or could point me in the right direction I'd be extremely grateful.</p>
  
  <p>My current server side code:</p>
  
  <pre><code>var io = require('socket.io').listen(server);
  
  io.sockets.on('connection', function(socket) {
  
      socket.on('connectToServer', function (data) {
          var name = data.name;
          io.sockets.emit('user-join', name + " has connected to the group");
      });
  
      socket.on('send msg', function (data) {
          io.sockets.emit('get msg', data);
      });
  
  });
  </code></pre>
  
  <p>and my client side code in the groupController, in which I access a Global service which stores the current user's details is as follows:</p>
  
  <pre><code>var socket = io.connect();
  
  $scope.msgs = [];
  
  $scope.sendMsg = function () {
      socket.emit('send msg', $scope.msg.text);
      $scope.msg.text = "";
  }
  
  socket.on('get msg', function (data){
      $scope.msgs.push(data);
  });
  
  var currentUser = Global.currentUser();
  var name = currentUser.username;
  
  socket.emit('connectToServer', {name: name});
  
  socket.on('user-join', function (data) {
      $scope.msgs.push(data);
  });
  </code></pre>