Skip to content

io.fetchSockets() fails with createSentinel #573

Open

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

  1. barisusakli commented on May 5, 2026

    @barisusakli
    ContributorAuthor

    I traced the code to see what was happening when fetchSockets was called.

    socket.io-redis adapter makes this call in dist/utils.js

      // redis@4 standalone
       return redisClient
           .sendCommand(["PUBSUB", arg, channel])
           .then(parseNumSubResponse);
    

    Which goes into @redis/client/dist/lib/sentinel/index.js sendCommand function. But that function has the below signature, so all the parameters are put into isReadonly.

     async sendCommand(isReadonly, args, options) {
           return this._execute(isReadonly, client => client.sendCommand(args, options));
    }
    

    This causes args and options to be undefined which causes the error and the stacktrace in the first post.

    Regular redis client has a sendCommand function as well, but as far as I can see it doesn't have this first isReadonly param.

    https://github.com/redis/node-redis/blob/master/packages/client/lib/client/index.ts#L1169-L1172

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