socket.io-redis-adapter icon indicating copy to clipboard operation
socket.io-redis-adapter copied to clipboard

issue with fetchSocket()

Open SK-CSE opened this issue 3 years ago • 1 comments

i am using pm2 cluster mode for my node app and when i try to fetch socket via

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

sometimes i got actual socket where i have append some custom data eventMetaData and sometimes i got remote socket which doesn’t have my custom data, please find below the actual socket and remote socket that i get

1|test-app  | sockets--- [
    1|test-app  |   Socket {
    1|test-app  |     _events: [Object: null prototype] {
    1|test-app  |       error: [AsyncFunction (anonymous)],
    1|test-app  |       req: [Function: bound requestHandler] AsyncFunction,
    1|test-app  |       disconnect: [Function (anonymous)]
    1|test-app  |     },
    1|test-app  |     _eventsCount: 3,
    1|test-app  |     _maxListeners: undefined,
    1|test-app  |     nsp: Namespace {
    1|test-app  |       _events: [Object: null prototype],
    1|test-app  |       _eventsCount: 1,
    1|test-app  |       _maxListeners: undefined,
    1|test-app  |       sockets: [Map],
    1|test-app  |       _fns: [],
    1|test-app  |       _ids: 0,
    1|test-app  |       server: [Server],
    1|test-app  |       name: '/',
    1|test-app  |       adapter: [RedisAdapter],
    1|test-app  |       [Symbol(kCapture)]: false
    1|test-app  |     },
    1|test-app  |     client: Client {
    1|test-app  |       sockets: [Map],
    1|test-app  |       nsps: [Map],
    1|test-app  |       server: [Server],
    1|test-app  |       conn: [Socket],
    1|test-app  |       encoder: Encoder {},
    1|test-app  |       decoder: [Decoder],
    1|test-app  |       id: 'pIQW8CCYn82_GSpoAAAP',
    1|test-app  |       onclose: [Function: bound onclose],
    1|test-app  |       ondata: [Function: bound ondata],
    1|test-app  |       onerror: [Function: bound onerror],
    1|test-app  |       ondecoded: [Function: bound ondecoded],
    1|test-app  |       connectTimeout: undefined
    1|test-app  |     },
    1|test-app  |     data: {},
    1|test-app  |     acks: Map(0) {},
    1|test-app  |     fns: [ [AsyncFunction (anonymous)] ],
    1|test-app  |     flags: {},
    1|test-app  |     server: Server {
    1|test-app  |       _events: [Object: null prototype] {},
    1|test-app  |       _eventsCount: 0,
    1|test-app  |       _maxListeners: undefined,
    1|test-app  |       _nsps: [Map],
    1|test-app  |       parentNsps: Map(0) {},
    1|test-app  |       _path: '/socket.io',
    1|test-app  |       clientPathRegex: /^\/socket\.io\/socket\.io(\.min|\.msgpack\.min)?\.js(\.map)?$/,
    1|test-app  |       _connectTimeout: 45000,
    1|test-app  |       _serveClient: true,
    1|test-app  |       _parser: [Object],
    1|test-app  |       encoder: Encoder {},
    1|test-app  |       _adapter: [Function (anonymous)],
    1|test-app  |       sockets: [Namespace],
    1|test-app  |       opts: [Object],
    1|test-app  |       eio: [Server],
    1|test-app  |       httpServer: [Server],
    1|test-app  |       engine: [Server],
    1|test-app  |       [Symbol(kCapture)]: false
    1|test-app  |     },
    1|test-app  |     adapter: RedisAdapter {
    1|test-app  |       _events: [Object: null prototype] {},
    1|test-app  |       _eventsCount: 0,
    1|test-app  |       _maxListeners: undefined,
    1|test-app  |       nsp: [Namespace],
    1|test-app  |       rooms: [Map],
    1|test-app  |       sids: [Map],
    1|test-app  |       encoder: Encoder {},
    1|test-app  |       pubClient: [RedisClient],
    1|test-app  |       subClient: [RedisClient],
    1|test-app  |       requests: Map(0) {},
    1|test-app  |       uid: 'dh0K01',
    1|test-app  |       requestsTimeout: 5000,
    1|test-app  |       publishOnSpecificResponseChannel: false,
    1|test-app  |       channel: 'socket.io#/#',
    1|test-app  |       requestChannel: 'socket.io-request#/#',
    1|test-app  |       responseChannel: 'socket.io-response#/#',
    1|test-app  |       [Symbol(kCapture)]: false
    1|test-app  |     },
    1|test-app  |     id: 'pIQW8CCYn82_GSpoAAAP',
    1|test-app  |     connected: true,
    1|test-app  |     disconnected: false,
    1|test-app  |     handshake: {
    1|test-app  |       headers: [Object],
    1|test-app  |       time: 'Fri Jul 08 2022 05:48:10 GMT+0000 (Coordinated Universal Time)',
    1|test-app  |       address: '::ffff:10.30.11.223',
    1|test-app  |       xdomain: false,
    1|test-app  |       secure: false,
    1|test-app  |       issued: 1657259290376,
    1|test-app  |       url: '/socket.io/?EIO=3&transport=websocket',
    1|test-app  |       query: [Object: null prototype],
    1|test-app  |       auth: {}
    1|test-app  |     },
    1|test-app  |     eventMetaData: {
    1|test-app  |       userObjectId: '12345',
    1|test-app  |     },
    1|test-app  |     [Symbol(kCapture)]: false
    1|test-app  |   }
    1|test-app  | ]

and remote socket sometimes

2|test-app  | sockets--- [
        2|test-app  |   RemoteSocket {
        2|test-app  |     id: 'XCqoMchtLYdsB9ldAAAJ',
        2|test-app  |     handshake: {
        2|test-app  |       headers: [Object],
        2|test-app  |       time: 'Fri Jul 08 2022 06:10:13 GMT+0000 (Coordinated Universal Time)',
        2|test-app  |       address: '::ffff:10.30.1.185',
        2|test-app  |       xdomain: false,
        2|test-app  |       secure: false,
        2|test-app  |       issued: 1657260613462,
        2|test-app  |       url: '/socket.io/?EIO=3&transport=websocket',
        2|test-app  |       query: [Object],
        2|test-app  |       auth: {}
        2|test-app  |     },
        2|test-app  |     rooms: Set(2) {
        2|test-app  |       'XCqoMchtLYdsB9ldAAAJ',
        2|test-app  |       'ef88ce1b91524264bbb020bcafe6b61c'
        2|test-app  |     },
        2|test-app  |     data: {},
        2|test-app  |     operator: BroadcastOperator {
        2|test-app  |       adapter: [RedisAdapter],
        2|test-app  |       rooms: [Set],
        2|test-app  |       exceptRooms: Set(0) {},
        2|test-app  |       flags: {}
        2|test-app  |     }
        2|test-app  |   }
        2|test-app  | ]

with

io.sockets.sockets.get(socketId)

i'm getting null incase of RemoteSocket, when actual socket is not returned how can i get my actual socket every time in pm2 cluster..??? and what is the difference between the term Socket and Remote Socket (see 1st element of sockets array)

SK-CSE avatar Aug 17 '22 13:08 SK-CSE

Hi! A RemoteSocket is a socket that exists on another node, while a Socket exists on the node that called fetchSockets().

Regarding the eventMetaData, you can use the data property of the socket, it will be included in the fetchSockets() results:

// server A
io.on("connection", (socket) => {
  socket.data.username = "alice";
});

// server B
const sockets = await io.fetchSockets();
console.log(sockets[0].data.username); // "alice"

Reference: https://socket.io/docs/v4/server-instance/#fetchsockets

darrachequesne avatar Sep 20 '22 05:09 darrachequesne

Closed due to inactivity, please reopen if needed.

darrachequesne avatar Feb 08 '23 07:02 darrachequesne