javascript – Creating Rooms in Socket.io

javascript – Creating Rooms in Socket.io

Rooms in Socket.IO dont 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 dont need to store this room object anywhere, because its 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);
  });
});

javascript – Creating Rooms in Socket.io

Leave a Reply

Your email address will not be published.