Thursday, 28 November 2019

socket.io with socket.io-redis: get all socket object in a room

I want to retrieve server side all socket connected in a room.

I have found the method clients that if chained after the method in return all the sockets connected in a room:

import * as express from 'express';
import * as SocketIO from 'socket.io';
import * as redisSocket from 'socket.io-redis';
import * as sharedsession from 'express-socket.io-session';

const app = express();

const redisOption = {port: 6379, host: 'XXX'};

const session = session({
  name: 'SESSIONID',
  store: new RedisStore(redisOption),
  secret: 'Ashg8hV77bxFeyLVec',
  resave: false,
  saveUninitialized: false
});

app.use(session);

const httpServer = app.listen(80);

const socketServer = SocketIO(httpServer, { path: '/api/socket.io', origins: '*:*' });

socketServer.adapter(redisSocket(redisOption ));

socketServer.use(sharedsession(session, {autoSave: true}));

socketServer.in(room_id).clients((err, clients) => {
    for (let i = 0, e = clients.length; i < e; i++) {
        const client = clients[i];
        console.log(client); // print the socket id
    }
});

But i need to retrive all sockets session/handshake.

There is a way for retrive all sockets session/handshake?

SIDE NOTE: the socket server is multi instance/nodes with socket.io-redis

  • socket.io: 2.3.0
  • socket.io-redis: 5.2.0


from socket.io with socket.io-redis: get all socket object in a room

No comments:

Post a Comment