title: Node.js and Socket.io Creating Room
body: <p>can i ask for your help. My codes doesn't work for creating a room using socket.room, first I've declared <code>var rooms = ['Lobby'];</code> and I would like to create rooms and push the created room names to <code>rooms[]</code>. My goal is to allow users to create their own room and delete it if possible. I used the code of Michael Mukhin <a href="http://psitsmike.com/2011/10/node-js-and-socket-io-multiroom-chat-tutorial/">here</a> as a reference.</p>
  
  <p><strong>Here is the code of my server.js</strong> </p>
  
  <pre><code>var redis = require('redis');
  var express = require('express');
  var app = express();
  var http = require('http');
  var server = http.createServer(app);
  var io = require('socket.io').listen(server);
  var publish = redis.createClient();
  var subscribe = redis.createClient();
  
  server.listen(8080);
  
  
  app.get('/', function (req, res) {
    res.sendfile(__dirname + '/index.html');
  });
  
  var usernames = {};
  
  var rooms = ['Lobby'];
  
  io.sockets.on('connection', function (socket) {
  
      socket.on('adduser', function (username){
          socket.username = username;
          socket.room = 'Lobby';         
          usernames[username] = username;
          socket.join('Lobby');          
          subscribe.subscribe(socket.room);   
          socket.emit('updatechat', 'SERVER', 'you have connected to Lobby');
          socket.broadcast.to('Lobby').emit('updatechat', 'SERVER', username + ' has connected to this room');
          socket.emit('updaterooms', rooms, 'Lobby');
      });
  
      socket.on('create', function (room) {
          var room = 'test';
          rooms.push(room);
          });
  
      socket.on('sendchat', function (data) {
  
          io.sockets.in(socket.room).emit('updatechat', socket.username, data);
          if (socket.room === 'Lobby') {
              publish.publish('Lobby', data);
          } else {
          publish.publish(socket.room, data);
          }
      });
  
      socket.on('switchRoom', function(newroom){
          socket.leave(socket.room);
          socket.join(newroom);
          subscribe.subscribe(newroom);
          socket.emit('updatechat', 'SERVER', 'you have connected to '+ newroom);
          socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', socket.username+' has left this room');
          socket.room = newroom;
          socket.broadcast.to(newroom).emit('updatechat', 'SERVER', socket.username+' has joined this room');
          socket.emit('updaterooms', rooms, newroom);
      });
  
  
  
      socket.on('disconnect', function(){
  
          delete usernames[socket.username];
          io.sockets.emit('updateusers', usernames);
          socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
          socket.leave(socket.room);
      });
  });
  </code></pre>
  
  <p><strong>Here is the code in the client index.html</strong>
  I would like to create a room and add it to the list of <code>rooms[]</code>.</p>
  
  <pre><code>&lt;script src="/socket.io/socket.io.js"&gt;&lt;/script&gt;
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
      var socket = io.connect('http://localhost:8080');
  
  
      socket.on('connect', function(){
  
          socket.emit('adduser', prompt("What's your name: "));
          test();
      });
  
  
          //this is my create room to be called when i click a button
          socket.on('createroom', function () {
              socket.emit('create', room);
          });
  
  
  
  
      socket.on('updatechat', function (username, data) {
          $('#conversation').append('&lt;b&gt;'+ username + ':&lt;/b&gt; ' + data + '&lt;br&gt;');
      });
  
  
      socket.on('updaterooms', function (rooms, current_room) {
          $('#rooms').empty();
          $.each(rooms, function(key, value) {
              if(value == current_room){
                  $('#rooms').append('&lt;div&gt;' + value + '&lt;/div&gt;');
              }
              else {
                  $('#rooms').append('&lt;div&gt;&lt;a href="#" onclick="switchRoom(\''+value+'\')"&gt;' + value + '&lt;/a&gt;&lt;/div&gt;');
              }
          });
      });
  
      function switchRoom(room){
          socket.emit('switchRoom', room);
      }
  
  
      $(function(){
          $('#datasend').click( function() {
              var message = $('#data').val();
              $('#data').val('');
              socket.emit('sendchat', message);
          });
  
  
          $('#data').keypress(function(e) {
              if(e.which == 13) {
                  $(this).blur();
                  $('#datasend').focus().click();
              }
          });
      });
  
  &lt;/script&gt;
  &lt;div style="float:left;width:100px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;"&gt;
      &lt;b&gt;ROOMS&lt;/b&gt;
      &lt;div id="rooms"&gt;&lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div style="float:left;width:300px;height:250px;overflow:scroll-y;padding:10px;"&gt;
      &lt;div id="conversation"&gt;&lt;/div&gt;
      &lt;input id="data" style="width:200px;" /&gt;
      &lt;input type="button" id="datasend" value="send" /&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Please help me guys I've already read socket.io documentation. But I found it hard to understand. I hope you can help me guys. Thanks in advance, i'm sorry but i'm still learning socket.io and node.js. Thanks.</p>