Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic rooms in Socket.io

I'm new in node.js and socket.io. I didn't understand how to work with rooms. I'm creating something like a Private Messages. Each pair of users have their unique room.

From documentation:

io.on('connection', function(socket){
  socket.join('some room');
});

But I need to create a room from client side. Because it's dynamic. How?

I met some examples https://gist.github.com/crtr0/2896891

server.js

io = socketio.listen(server);

io.sockets.on('connection', function(socket) {
    socket.on('room', function(room) {
        socket.join(room);
    });
});

room = "abc123";
io.sockets.in(room).emit('message', 'what is going on, party people?');

This line drives me crazy. It seems to me stupid. Because server never know which room to use. Server just can handle multiple rooms.

room = "abc123";

Please, help or explain.

like image 278
Daryn K. Avatar asked Dec 25 '16 16:12

Daryn K.


People also ask

How many rooms can Socket.IO have?

socket.io rooms are a lightweight data structure. They are simply an array of connections that are associated with that room. You can have as many as you want (within normal memory usage limits). There is no heavyweight thing that makes a room expensive in terms of resources.

Is Socket.IO emit asynchronous?

JS, Socket.IO enables asynchronous, two-way communication between the server and the client. This means that the server can send messages to the client without the client having to ask first, as is the case with AJAX.

How many players can Socket.IO handle?

Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).

Is Socket.IO better than WebSocket?

Socket.IO is way more than just a layer above WebSockets, it has different semantics (marks messages with name), and does failovers to different protocols, as well has heartbeating mechanism. More to that attaches ID's to clients on server side, and more. So it is not just a wrapper, it is full-featured library.


1 Answers

In you above code you have fixed room to abc123 , that you need to make it dynamic for all connected clients . You can provide room create option to user also you can provide logic to change/rename/leave/join room from client. Basically in your client and server you can apply below logical changes.

Client logic to update or change room :

socket.emit('switchRoom', newRoom);

socket.on('updaterooms', function(rooms, current_room) {
  // Update new room logic

}

Server Logic to handle room change :

socket.on('switchRoom', function(newroom){
        // leave the current room (stored in session)
        socket.leave(socket.room);
        // join new room, received as function parameter
        socket.join(newroom);
        socket.emit('updatechat', 'SERVER', 'you have connected to '+ newroom);
        // sent message to OLD room
        socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', socket.username+' has left this room');
        // update socket session room title
        socket.room = newroom;
        socket.broadcast.to(newroom).emit('updatechat', 'SERVER', socket.username+' has joined this room');
        socket.emit('updaterooms', rooms, newroom);
    });

Refer Below Example :

Socket.io Multi-room Example

Rooms and Namespace in socket.io

like image 56
Sumeet Kumar Yadav Avatar answered Sep 17 '22 14:09

Sumeet Kumar Yadav