Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.join does not work (socket.io)

So, my socket.join calls are doing nothing. After calling socket.join, and then listing the rooms the socket is in, the socket is only connected to the room defined by it's id (it's own room).

function onJoin(room) {
    console.log("Joining room: " + room);
    socket.join(room);
    console.log(socket.id + " now in rooms ", socket.rooms);
}

Would print for example:

> Joining room: 5aba92759b9ffa9fdf579714d6aa125ddb05cb1172611331775e7a69dab37258
> Q6D4h17DvdOZrbrEAAAC now in rooms  { Q6D4h17DvdOZrbrEAAAC: 'Q6D4h17DvdOZrbrEAAAC' }

If it makes a difference, here's how my socket server is being created:

//app.js
var app = express();
var http = require('http');
var server = http.Server(app);
var io = require('socket.io')(server);
var chat = require('./routes/chat/chat')(io);

//chat.js
module.exports = function(io) {
  io.sockets.on('connection', function(socket) {

    socket.on('join', onJoin);
    ...
}

Where's the issue?

  • Socket.io v1.7.3
like image 521
Juuso Avatar asked Apr 19 '17 22:04

Juuso


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 .

Is Socket.IO difficult?

As explained above, getting started with Socket.IO is relatively simple – all you need is a Node. js server to run it on. If you want to get started with a realtime app for a limited number of users, Socket.IO is a good option. Problems come when working at scale.

How many rooms can Socket.IO handle?

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


3 Answers

The issue was that socket.join is async. So this would work as expected:

socket.join(room, function() {
  console.log("Socket now in rooms", socket.rooms);
});
like image 133
Juuso Avatar answered Oct 22 '22 18:10

Juuso


In the new version, adding a function as a second parameter to the .join() method has been removed, You can use async await.

example

await socket.join(room_Name);
io.to(room_Name).emit(emit_Name);
like image 26
SalihKalender Avatar answered Oct 22 '22 16:10

SalihKalender


Use: io.sockets.adapter.rooms

function onJoin(room) {
     console.log("Joining room: " + room);
     socket.join(room);
     console.log(socket.id + " now in rooms ", getRoomsByUser(socket.id));
}

function getRoomsByUser(id){
    let usersRooms = [];
    let rooms = io.sockets.adapter.rooms;

    for(let room in rooms){
        if(rooms.hasOwnProperty(room)){
            let sockets = rooms[room].sockets;
            if(id in sockets)
                usersRooms.push(room);          
        }
    }

    return usersRooms;
}

After joining 'test' you will see something like this:

AEi6eIlkutIcm_CwAAAB now in rooms test,AEi6eIlkutIcm_CwAAAB
like image 38
Marcos Casagrande Avatar answered Oct 22 '22 18:10

Marcos Casagrande