Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.IO subscribe to multiple channels

Tags:

socket.io

I want to build a simple chat room system on top of Socket.IO where user can create a new chat room and then people can start chatting.

This sound simple but as the Socket.IO 0.9.4 I'm running now, after reading a few SO posts together with the document on socket.io, i'm getting more and more confused. So, I hope that someone can provide me with instruction that WORK with 0.9.4:

  1. I need a way to subscribe to a room. The room name is chosen by user. When a message is posted in a room, the user should receive it. How should I write the server code, how should I write the client code?

  2. A user can join multiple rooms simultaneously.

  3. I want another system to send a message to all user in a certain room. This 'another system' send the message through a request handled by express. How would I write that request handler?

like image 775
Phương Nguyễn Avatar asked May 07 '13 15:05

Phương Nguyễn


People also ask

Can Socket join multiple rooms?

Joining and leaving​ In that case, a union is performed: every socket that is at least in one of the rooms will get the event once (even if the socket is in two or more rooms). In that case, every socket in the room excluding the sender will get the event. To leave a channel you call leave in the same fashion as join .

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 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.

How many concurrent connections can Socket.IO handle?

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


1 Answers

This is all pretty straightforward with the socket.io rooms feature. Take a look at the documentation on LearnBoost wiki.

https://github.com/LearnBoost/socket.io/wiki/Rooms

It allows for being connected to multiple rooms over a single socket. I put together a quick test with the following code.

Server

io.sockets.on('connection', function(client){
    client.on('subscribe', function(room) { 
        console.log('joining room', room);
        client.join(room); 
    })
    
    client.on('unsubscribe', function(room) {  
        console.log('leaving room', room);
        client.leave(room); 
    })

    client.on('send', function(data) {
        console.log('sending message');
        io.sockets.in(data.room).emit('message', data);
    });
});

Client

 var socket = io.connect();
 socket.on('message', function (data) {
  console.log(data);
 });
 
 socket.emit('subscribe', 'roomOne');
 socket.emit('subscribe', 'roomTwo');
 
 $('#send').click(function() {
  var room = $('#room').val(),
   message = $('#message').val();
   
  socket.emit('send', { room: room, message: message });
 });

Sending a message from an Express route is pretty simple as well.

app.post('/send/:room/', function(req, res) {
    var room = req.params.room
        message = req.body;

    io.sockets.in(room).emit('message', { room: room, message: message });

    res.end('message sent');
});
like image 94
Timothy Strimple Avatar answered Oct 18 '22 02:10

Timothy Strimple



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!