How can I extract info from a Map Object?

Viewed 344

I am using socket.io 4.x on my NodeJs server and I need to access a few things which are stored in io.sockets.sockets object. So here is what I am looking for how can i get values out of this map, or get a count of how many there are in the map. I tried the below with no luck..

var srvSockets = io.sockets.sockets;
Object.keys(srvSockets).length;

if I do a

let sockets = io.sockets.sockets;
console.log(sockets) 

The output looks like this

Map {
  'TTRWuTdEjhyGwKQvAAAB' => Socket {
    _events: [Object: null prototype] {
      authenticate: [AsyncFunction],
      messageToServer: [Function],
      demo: [Function],
      clientDisco: [AsyncFunction],
      disconnect: [AsyncFunction]
    },
    _eventsCount: 5,
    _maxListeners: undefined,
    nsp: Namespace {
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      sockets: [Circular],
      _fns: [],
      _ids: 0,
      server: [Server],
      name: '/',
      adapter: [Adapter]
    },
    client: Client {
      sockets: [Map],
      nsps: [Map],
      server: [Server],
      conn: [Socket],
      encoder: Encoder {},
      decoder: [Decoder],
      id: 'dnQiSj9Rxt6rnMSMAAAA',
      onclose: [Function: bound onclose],
      ondata: [Function: bound ondata],
      onerror: [Function: bound onerror],
      ondecoded: [Function: bound ondecoded],
      connectTimeout: undefined
    },
    data: {},
    acks: Map {},
    fns: [],
    flags: {},
    server: Server {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      _nsps: [Map],
      parentNsps: Map {},
      _path: '/socket.io',
      clientPathRegex: /^\/socket\.io\/socket\.io(\.min|\.msgpack\.min)?\.js(\.map)?$/,
      _connectTimeout: 45000,
      _serveClient: true,
      _parser: [Object],
      encoder: Encoder {},
      _adapter: [Function: Adapter],
      sockets: [Namespace],
      opts: [Object],
      eio: [Server],
      httpServer: [Server],
      engine: [Server]
    },
    adapter: Adapter {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      nsp: [Namespace],
      rooms: [Map],
      sids: [Map],
      encoder: Encoder {}
    },
    id: 'TTRWuTdEjhyGwKQvAAAB',
    connected: true,
    disconnected: false,
    handshake: {
      headers: [Object],
      time: 'Mon Apr 05 2021 16:10:07 GMT-0700 (Pacific Daylight Time)',
      address: '::ffff:192.168.1.33',
      xdomain: true,
      secure: false,
      issued: 1617664207799,
      url: '/socket.io/?EIO=4&transport=polling&t=NYaJEjx',
      query: [Object: null prototype],
      auth: {}
    },
    auth: true
  },
  'rfjC2P4XCy84Q3YMAAAD' => Socket {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    nsp: Namespace {
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      sockets: [Circular],
      _fns: [],
      _ids: 0,
      server: [Server],
      name: '/',
      adapter: [Adapter]
    },
    client: Client {
      sockets: [Map],
      nsps: [Map],
      server: [Server],
      conn: [Socket],
      encoder: Encoder {},
      decoder: [Decoder],
      id: 'Pz7vOszEQoqMvGq7AAAC',
      onclose: [Function: bound onclose],
      ondata: [Function: bound ondata],
      onerror: [Function: bound onerror],
      ondecoded: [Function: bound ondecoded],
      connectTimeout: undefined
    },
    data: {},
    acks: Map {},
    fns: [],
    flags: {},
    server: Server {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      _nsps: [Map],
      parentNsps: Map {},
      _path: '/socket.io',
      clientPathRegex: /^\/socket\.io\/socket\.io(\.min|\.msgpack\.min)?\.js(\.map)?$/,
      _connectTimeout: 45000,
      _serveClient: true,
      _parser: [Object],
      encoder: Encoder {},
      _adapter: [Function: Adapter],
      sockets: [Namespace],
      opts: [Object],
      eio: [Server],
      httpServer: [Server],
      engine: [Server]
    },
    adapter: Adapter {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      nsp: [Namespace],
      rooms: [Map],
      sids: [Map],
      encoder: Encoder {}
    },
    id: 'rfjC2P4XCy84Q3YMAAAD',
    connected: true,
    disconnected: false,
    handshake: {
      headers: [Object],
      time: 'Mon Apr 05 2021 16:10:08 GMT-0700 (Pacific Daylight Time)',
      address: '::ffff:192.168.1.33',
      xdomain: true,
      secure: false,
      issued: 1617664208757,
      url: '/socket.io/?EIO=4&transport=polling&t=NYaJEzW',
      query: [Object: null prototype],
      auth: {}
    }
  }
}
2 Answers

Maps can be iterate over with .entries (and a few other methods):

for (const [key, value] of io.sockets.sockets.entries()) {
  // key will be something like TTRWuTdEjhyGwKQvAAAB
  // value will be a Socket
}

If you want to get the participants of the server, you can use fetchSockets function as below:

// return all Socket instances
const sockets = await io.fetchSockets();

// return all Socket instances in the "room1" room of the main namespace
const sockets = await io.in("room1").fetchSockets();

// return all Socket instances in the "room1" room of the "admin" namespace
const sockets = await io.of("/admin").in("room1").fetchSockets();

// this also works with a single socket ID
const sockets = await io.in(theSocketId).fetchSockets();

Source: https://socket.io/docs/v4/server-instance/#fetchSockets

Related