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();
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)
I created a minimal example below.
Running this with
node index.js 4567throws the below error when callingio.fetchSockets()