Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decrease socket.io room size

I am using socket io for my chat application one to one. I am facing the issue right now. When user will disconnect or end chat manually by click the end chat button. But from socket that user not remove. Each time room size remains same. How can i remove that user from socket connection.

like image 412
Rahul_Dabhi Avatar asked Dec 28 '25 18:12

Rahul_Dabhi


1 Answers

 socket.on('create or join', function(room)
    {
        numClients = io.sockets.clients(room).length;
        console.log("user joining the room");
        console.log(numClients);

        if (numClients === 0)
        {
            socket.join(room);
            socket.emit('created', room);
        }
        else if (numClients == 1)
        {
            io.sockets. in (room).emit('join', room);
            socket.join(room);
            socket.emit('joined', room);
        }
        else
        {
            socket.emit('full', room);
        }
        socket.emit('emit(): client ' + socket.id + ' joined room ' + room);
        socket.broadcast.emit('broadcast(): client ' + socket.id + ' joined room ' + room);
    });
like image 76
Rahul_Dabhi Avatar answered Jan 01 '26 14:01

Rahul_Dabhi



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!