Skip to content Skip to sidebar Skip to footer

Where Is Io.sockets.adapter.rooms In Io Of Nodejs?

https://stackoverflow.com/a/6727354/462608 The short answer: io.sockets.adapter.rooms I analysed io: The sockets output part from io as shown in that answer contains the followin

Solution 1:

I think you are trying to get room before joining it. First You have to Join room and than you can get the rooms in io.sockets.adapter.rooms You can checkout this link to know rooms

let room_id = 111

io.sockets.on("connection", function (socket) {
    // Everytime a client logs in, display a connected messageconsole.log("Server-Client Connected!");
    socket.join("_room" + room_id);
    socket.on('connected', function (data) {

    });
    console.log(io.sockets.adapter.rooms);
    socket.on('qr_code_scan', function (room_id) {
        io.sockets.in("_room" + room_id).emit("qr_code_scan", true);
    });
});

Log of io.sockets.adapter.rooms

{bjYiUV5YZy54VedKAAAA: Room, _room111: Room}app.js:55_room111:Room {sockets: {…}, length: 1}length:1sockets:{-isBAZIB-Sm3jArgAAAB: true}-isBAZIB-Sm3jArgAAAB:true__proto__:Object__proto__:Object-isBAZIB-Sm3jArgAAAB:Room {sockets: {…}, length: 1}length:1sockets:{-isBAZIB-Sm3jArgAAAB: true}-isBAZIB-Sm3jArgAAAB:true__proto__:Object__proto__:Object__proto__:Object

Solution 2:

For the current version of "socket.io": "^4.1.2",

io.sockets.adapter.rooms

is a Map like this:

Map(2) { 'hgdAp3ghn1RQZk3iAAAD' => Set(1) { 'hgdAp3ghn1RQZk3iAAAD' }, 'test' => Set(1) { 'hgdAp3ghn1RQZk3iAAAD' } }

when a room already exist, in this case 'test'.

If you invoke it before a room has been created, then it'd be:

Map(1) { 'w2e2Vnav-zmf6pm4AAAD' => Set(1) { 'w2e2Vnav-zmf6pm4AAAD' } }

Thus the long answer is that it depends on the version you are using, and that for version 4.x the room will only be part of the map after an user has joined the room, not before.

Solution 3:

I'm not sure what's up with that response. But I can confirm that when I run a basic socket.io example (codesandbox link) using socket.io@2.1.1 and console.log(io) I see the following in my terminal:

Server {
  nsps: {
    '/':Namespace {
      name:'/',
      server: [Circular],
      sockets: [Object],
      connected: [Object],
      fns: [],
      ids:0,
      rooms: [],
      flags: {},
      adapter: [Adapter],
      _events: [Object:nullprototype],
      _eventsCount:1
    }
  },
  parentNsps:Map {},
  _path:'/socket.io',
  _serveClient:true,
  parser: {
    protocol:4,
    types: [
      'CONNECT',
      'DISCONNECT',
      'EVENT',
      'ACK',
      'ERROR',
      'BINARY_EVENT',
      'BINARY_ACK'
    ],
    CONNECT:0,
    DISCONNECT:1,
    EVENT:2,
    ACK:3,
    ERROR:4,
    BINARY_EVENT:5,
    BINARY_ACK:6,
    Encoder: [Function:Encoder],
    Decoder: [Function:Decoder]
  },
  encoder:Encoder {},
  _adapter: [Function:Adapter],
  _origins:'*:*',
  sockets:Namespace {
    name:'/',
    server: [Circular],
    sockets: { WFrro9MpS4d1nSouAAAA: [Socket] },
    connected: { WFrro9MpS4d1nSouAAAA: [Socket] },
    fns: [],
    ids:0,
    rooms: [],
    flags: {},
    adapter:Adapter {
      nsp: [Circular],
      rooms: [Object],
      sids: [Object],
      encoder:Encoder {}
    },
    _events: [Object:nullprototype] { connection: [Array] },
    _eventsCount:1
  },
  eio:Server {
    clients: { WFrro9MpS4d1nSouAAAA: [Socket] },
    clientsCount:1,
    wsEngine:'ws',
    pingTimeout:5000,
    pingInterval:25000,
    upgradeTimeout:10000,
    maxHttpBufferSize:100000000,
    transports: [ 'polling', 'websocket' ],
    allowUpgrades:true,
    allowRequest: [Function:bound ],
    cookie:'io',
    cookiePath:'/',
    cookieHttpOnly:true,
    perMessageDeflate: { threshold:1024 },
    httpCompression: { threshold:1024 },
    initialPacket: [ '0' ],
    ws:WebSocketServer {
      _events: [Object:nullprototype] {},
      _eventsCount:0,
      _maxListeners:undefined,
      options: [Object],
      [Symbol(kCapture)]:false
    },
    _events: [Object:nullprototype] { connection: [Function:bound ] },
    _eventsCount:1
  },
  httpServer:Server {
    insecureHTTPParser:undefined,
    _events: [Object:nullprototype] {
      connection: [Function:connectionListener],
      close: [Function:bound ],
      listening: [Function:bound ],
      upgrade: [Function],
      request: [Function]
    },
    _eventsCount:5,
    _maxListeners:undefined,
    _connections:2,
    _handle:TCP {
      reading:false,
      onconnection: [Function:onconnection],
      [Symbol(owner_symbol)]: [Circular]
    },
    _usingWorkers:false,
    _workers: [],
    _unref:false,
    allowHalfOpen:true,
    pauseOnConnect:false,
    httpAllowHalfOpen:false,
    timeout:120000,
    keepAliveTimeout:5000,
    maxHeadersCount:null,
    headersTimeout:60000,
    _connectionKey:'6::::8080',
    [Symbol(IncomingMessage)]: [Function:IncomingMessage],
    [Symbol(ServerResponse)]: [Function:ServerResponse],
    [Symbol(kCapture)]:false,
    [Symbol(asyncId)]:6
  },
  engine:Server {
    clients: { WFrro9MpS4d1nSouAAAA: [Socket] },
    clientsCount:1,
    wsEngine:'ws',
    pingTimeout:5000,
    pingInterval:25000,
    upgradeTimeout:10000,
    maxHttpBufferSize:100000000,
    transports: [ 'polling', 'websocket' ],
    allowUpgrades:true,
    allowRequest: [Function:bound ],
    cookie:'io',
    cookiePath:'/',
    cookieHttpOnly:true,
    perMessageDeflate: { threshold:1024 },
    httpCompression: { threshold:1024 },
    initialPacket: [ '0' ],
    ws:WebSocketServer {
      _events: [Object:nullprototype] {},
      _eventsCount:0,
      _maxListeners:undefined,
      options: [Object],
      [Symbol(kCapture)]:false
    },
    _events: [Object:nullprototype] { connection: [Function:bound ] },
    _eventsCount:1
  }
} 

With the rooms in io.sockets.adapter.rooms.

Post a Comment for "Where Is Io.sockets.adapter.rooms In Io Of Nodejs?"