0

I'm trying to get a connection count from the server socket, this server is set up away from the client application, I've referenced several SO articles that reference different ways to get the socket connection counts, but none of them have gotten me there.

the most recent referenced article is here: Socket.IO Connected User Count

I've tried the most recent answers here and none of them seem to want to return the correct information. I also have, from what I can see, always an extra connection, I console.log the io object to see what was inside of it, and I found the id of the connection I made on the socket, but I always see an extra socket connection there, not sure if I'm creating multiple connections or if the server is counting itself?

here is a console log of my io.sockets.sockets (as I was trying the example from the linked article):

<ref *1> Map(2) {
[0]   'tX7QE_wHuTtv_nSWAAAG' => Socket {
[0]     _events: [Object: null prototype] {
[0]       setup: [Function (anonymous)],
[0]       getConnectedUsers: [Function (anonymous)],
[0]       disconnect: [Function (anonymous)]
[0]     },
[0]     _eventsCount: 3,
[0]     _maxListeners: undefined,
[0]     nsp: Namespace {
[0]       _events: [Object: null prototype],
[0]       _eventsCount: 1,
[0]       _maxListeners: undefined,
[0]       sockets: [Circular *1],
[0]       _fns: [],
[0]       _ids: 0,
[0]       server: [Server],
[0]       name: '/',
[0]       adapter: [Adapter],
[0]       [Symbol(kCapture)]: false
[0]     },
[0]     client: Client {
[0]       sockets: [Map],
[0]       nsps: [Map],
[0]       server: [Server],
[0]       conn: [Socket],
[0]       encoder: [Encoder],
[0]       decoder: [Decoder],
[0]       id: 'sxkZ2zf2364ptRpkAAAE',
[0]       onclose: [Function: bound onclose],
[0]       ondata: [Function: bound ondata],
[0]       onerror: [Function: bound onerror],
[0]       ondecoded: [Function: bound ondecoded],
[0]       connectTimeout: undefined
[0]     },
[0]     data: {},
[0]     connected: true,
[0]     acks: Map(0) {},
[0]     fns: [],
[0]     flags: {},
[0]     server: Server {
[0]       _events: [Object: null prototype] {},
[0]       _eventsCount: 0,
[0]       _maxListeners: undefined,
[0]       _nsps: [Map],
[0]       parentNsps: Map(0) {},
[0]       _path: '/socket.io',
[0]       clientPathRegex: /^\/socket\.io\/socket\.io(\.msgpack|\.esm)?(\.min)?\.js(\.map)?(?:\?|$)/,
[0]       _connectTimeout: 45000,
[0]       _serveClient: true,
[0]       _parser: [Object],
[0]       encoder: [Encoder],
[0]       _adapter: [class Adapter extends EventEmitter],
[0]       sockets: [Namespace],
[0]       opts: [Object],
[0]       eio: [Server],
[0]       httpServer: [Server],
[0]       engine: [Server],
[0]       [Symbol(kCapture)]: false
[0]     },
[0]     adapter: Adapter {
[0]       _events: [Object: null prototype] {},
[0]       _eventsCount: 0,
[0]       _maxListeners: undefined,
[0]       nsp: [Namespace],
[0]       rooms: [Map],
[0]       sids: [Map],
[0]       encoder: [Encoder],
[0]       [Symbol(kCapture)]: false
[0]     },
[0]     id: 'tX7QE_wHuTtv_nSWAAAG',
[0]     handshake: {
[0]       headers: [Object],
[0]       time: 'Sat Dec 24 2022 17:37:59 GMT-0500 (Eastern Standard Time)',
[0]       address: '::1',
[0]       xdomain: true,
[0]       secure: false,
[0]       issued: 1671921479323,
[0]       url: '/socket.io/?EIO=4&transport=polling&t=OL6IIvl',
[0]       query: [Object: null prototype],
[0]       auth: {}
[0]     },
[0]     [Symbol(kCapture)]: false
[0]   },
[0]   'Fd0eZg47FOQ7kqKJAAAH' => Socket {
[0]     _events: [Object: null prototype] {
[0]       setup: [Function (anonymous)],
[0]       getConnectedUsers: [Function (anonymous)],
[0]       disconnect: [Function (anonymous)]
[0]     },
[0]     _eventsCount: 3,
[0]     _maxListeners: undefined,
[0]     nsp: Namespace {
[0]       _events: [Object: null prototype],
[0]       _eventsCount: 1,
[0]       _maxListeners: undefined,
[0]       sockets: [Circular *1],
[0]       _fns: [],
[0]       _ids: 0,
[0]       server: [Server],
[0]       name: '/',
[0]       adapter: [Adapter],
[0]       [Symbol(kCapture)]: false
[0]     },
[0]     client: Client {
[0]       sockets: [Map],
[0]       nsps: [Map],
[0]       server: [Server],
[0]       conn: [Socket],
[0]       encoder: [Encoder],
[0]       decoder: [Decoder],
[0]       id: 'WmPBukQtun1HRsXlAAAF',
[0]       onclose: [Function: bound onclose],
[0]       ondata: [Function: bound ondata],
[0]       onerror: [Function: bound onerror],
[0]       ondecoded: [Function: bound ondecoded],
[0]       connectTimeout: undefined
[0]     },
[0]     data: {},
[0]     connected: true,
[0]     acks: Map(0) {},
[0]     fns: [],
[0]     flags: {},
[0]     server: Server {
[0]       _events: [Object: null prototype] {},
[0]       _eventsCount: 0,
[0]       _maxListeners: undefined,
[0]       _nsps: [Map],
[0]       parentNsps: Map(0) {},
[0]       _path: '/socket.io',
[0]       clientPathRegex: /^\/socket\.io\/socket\.io(\.msgpack|\.esm)?(\.min)?\.js(\.map)?(?:\?|$)/,
[0]       _connectTimeout: 45000,
[0]       _serveClient: true,
[0]       _parser: [Object],
[0]       encoder: [Encoder],
[0]       _adapter: [class Adapter extends EventEmitter],
[0]       sockets: [Namespace],
[0]       opts: [Object],
[0]       eio: [Server],
[0]       httpServer: [Server],
[0]       engine: [Server],
[0]       [Symbol(kCapture)]: false
[0]     },
[0]     adapter: Adapter {
[0]       _events: [Object: null prototype] {},
[0]       _eventsCount: 0,
[0]       _maxListeners: undefined,
[0]       nsp: [Namespace],
[0]       rooms: [Map],
[0]       sids: [Map],
[0]       encoder: [Encoder],
[0]       [Symbol(kCapture)]: false
[0]     },
[0]     id: 'Fd0eZg47FOQ7kqKJAAAH',
[0]     handshake: {
[0]       headers: [Object],
[0]       time: 'Sat Dec 24 2022 17:37:59 GMT-0500 (Eastern Standard Time)',
[0]       address: '::1',
[0]       xdomain: true,
[0]       secure: false,
[0]       issued: 1671921479330,
[0]       url: '/socket.io/?EIO=4&transport=polling&t=OL6IIvr',
[0]       query: [Object: null prototype],
[0]       auth: {}
[0]     },
[0]     [Symbol(kCapture)]: false
[0]   }
[0] }

im trying to turn this into a simple array that i can use to send back a number to the client. This particular connection i was logged as connection Fd0eZg47FOQ7kqKJAAAH im not sure what the other connection is, and it cant be another user, the socket isnt deployed to production yet.

How can i get this to an array? i've tried: Object.keys(io.sockets.sockets).length but it just returns an empty array.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
Austin Howard
  • 780
  • 4
  • 10
  • 24

1 Answers1

0

So i fixed this issue by simly adding the socket connection to a room based on the data passed in. So for example, i have several admins in this application and i wanted to know how many admins where connected to the panel at one time. so instead of looking at the number of connections to the server, which for some reason adds 2 connections per 1 connect, i simply passed in the userdata back to the server, and added that user to a room, then i used Array.from(await io.in('adminRoom').allSockets()) || [] which looks into the defined room that i want, and returns all of the sockets back into an array, if its empty or null, it just defaults to an empty array i.e, 0 connections.

Austin Howard
  • 780
  • 4
  • 10
  • 24