73

I would like to ask for your help. I'm having a hard time in my client side of socket.io, I would like to call this code in my client side to create a room in socket.io:

var rooms = [];
socket.on('create', function (roomname) {
    rooms[room] = room;
    socket.room = roomname;
            socket.join(roomname);
    subscribe.subscribe(socket.room);
});

I don't know if this is correct, if not please help me to correct this guys. I'm not that to pro in node js and sockets but i've already read their wikis. Is there any possible way to create room?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Joenel de Asis
  • 1,401
  • 3
  • 15
  • 14

1 Answers1

169

Rooms in Socket.IO don't need to be created, one is created when a socket joins it. They are joined on the server side, so you would have to instruct the server using the client.

socket.on('create', function (room) {
  socket.join(room);
});

In the example above, a room is created with a name specified in variable room. You don't need to store this room object anywhere, because it's already part of the io object. You can then treat the room like its own socket instance.

io.sockets.in(room).emit('event', data);

So to create a room from the client, this is what it might look like:

// client side code
var socket = io.connect();
socket.emit('create', 'room1');

// server side code
io.sockets.on('connection', function(socket) {
  socket.on('create', function(room) {
    socket.join(room);
  });
});
hexacyanide
  • 88,222
  • 31
  • 159
  • 162
  • Thanks hexacyanide. Uhm. In my code above, i store room to rooms so that the room will be listed. I've just edited Michael Mukhin's [Multi Chatroom](http://psitsmike.com/2011/10/node-js-and-socket-io-multiroom-chat-tutorial/) so that the rooms created will be added in the list. – Joenel de Asis Oct 03 '13 at 03:47
  • 5
    @hexacyanide You said: `You don't need to store this room object anywhere` , after restart server how can you restore created rooms? – mahdi pishguy Sep 26 '16 at 18:35
  • 1
    @mahdipishguy A room is created when a socket joins it, so you'll need to store what rooms a socket was in before the server was stopped, and then have all the sockets rejoin the old rooms for them to be recreated. If this is something you have to do often, them it's likely that what you're trying to do with rooms is not what it was designed for. – hexacyanide Oct 18 '16 at 19:50
  • I don't know why you insist use `io.sockets.in(room).emit('event', data);` and then within your example you don't – Fernando Torres Jan 27 '20 at 02:38
  • And where does come from `io`, `sockets` and `in` instances? where those are created?, i just see we have `socket` global variable, could you explain? – Fernando Torres Jan 27 '20 at 02:40
  • @FernandoTorres https://stackoverflow.com/a/41319051/7704650 this answer have a good explanation – emkarachchi Jun 02 '20 at 19:43
  • can a room be empty? If yes, can an empty room be created with no participants using the io object? – nTheta Jul 11 '21 at 15:26
  • No, a room without any participants (or sockets) cannot be created – Jayesh Chandrapal Jul 31 '21 at 13:24