Skip to content

io.fetchSockets() fails with createSentinel #573

Description

@barisusakli

I created a minimal example below.

const { Server } = require("socket.io");
const { createClient, createSentinel } = require("redis");
const { createAdapter } = require("@socket.io/redis-adapter");

const port = process.argv[2] || 3000;

async function setupWithCreateSentinel() {
  const sentinelOptions = {
    name: 'mymaster',
    sentinelRootNodes: [
      { host: '127.0.0.1', port: 26379 },
      { host: '127.0.0.1', port: 26380 }
    ],
  };

  const pubClient = await createSentinel(sentinelOptions);
  const subClient = await createSentinel(sentinelOptions);

  pubClient.on('error', (err) => console.error('Pub Redis Error', err));
  subClient.on('error', (err) => console.error('Sub Redis Error', err));

  try {
    await Promise.all([pubClient.connect(), subClient.connect()]);
    console.log(`Connected via createSentinel to group: ${sentinelOptions.name}`);
  } catch (err) {
    console.error('Sentinel connection failed:', err);
    process.exit(1);
  }

  const io = new Server(port, {
    cors: { origin: "*" }
  });

  io.adapter(createAdapter(pubClient, subClient));

  console.log(`Socket.io server running on port ${port}`);

  setInterval(async () => {
    try {
      const sockets = await io.fetchSockets();
    } catch (err) {
      console.error('Fetch sockets error:', err);
    }
  }, 3000);
}

setupWithCreateSentinel();

Running this with node index.js 4567 throws the below error when calling io.fetchSockets()

Fetch sockets error: TypeError: Cannot read properties of undefined (reading 'length')
    at encodeCommand (socket.io-sentinel\node_modules\@redis\client\dist\lib\RESP\encoder.js:6:30)
    at RedisCommandsQueue.commandsToWrite (socket.io-sentinel\node_modules\@redis\client\dist\lib\client\commands-queue.js:379:49)
    at commandsToWrite.next (<anonymous>)
    at RedisSocket.write (socket.io-sentinel\node_modules\@redis\client\dist\lib\client\socket.js:274:20)
    at #write (socket.io-sentinel\node_modules\@redis\client\dist\lib\client\index.js:844:22)
    at Immediate.<anonymous> (socket.io-sentinel\node_modules\@redis\client\dist\lib\client\index.js:851:24)
    at process.processImmediate (node:internal/timers:504:21)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions