javascript – socket.io get rooms and connected sockets

Question:

there is a code like this:

var static = require('node-static');
var http = require('http');
var file = new(static.Server)();
var app = http.createServer(function (req, res) {
    file.serve(req, res);
}).listen(1234);

var io = require('socket.io').listen(app);

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

    socket.room = room;
    socket.join(socket.room);

    socket.broadcast.to(socket.room).emit('room', room);
});

how to get data about rooms?

Answer:

you can get rooms like this: let rooms = Object.keys(socket.rooms);

Scroll to Top