Combine @socket.io/cluster-adapter and @socket.io/redis-adapter #4659
-
Hi, on our project we have 2 machines with 4 cores each behind the LB. Currently we are running a single node process on each machine and using We want to use all 4 cores on every machine using node's
and only clients from the cluster can be reached, but not from the 2nd instance and it's cluster.
And all 8 workers are communicating through redis. While that seems to work fine it generates a lot of redis pub/sub traffic. What we want is to communicate between instances through redis, and between workers through node's cluster API. Click meconst cluster = require("cluster");
const http = require("http");
const { Server } = require("socket.io");
const {createClient} = require("redis");
const { setupMaster, setupWorker } = require("@socket.io/sticky");
const { setupPrimary, createAdapter: createClusterAdapter } = require("@socket.io/cluster-adapter");
const {createAdapter: createRedisAdapter} = require('@socket.io/redis-adapter');
const PORT = process.env.PORT;
const PID = process.pid;
const getRedisClients = async () => {
const client = createClient({
url: 'redis://localhost:6379',
socket: {
tls: false,
},
});
const clientDuplicate = client.duplicate();
await client.connect();
await clientDuplicate.connect();
return [client, clientDuplicate];
}
(async () => {
if (cluster.isMaster) {
///////////// MASTER
console.log(`Master ${PID} is running`);
const httpServer = http.createServer();
// setup sticky sessions
setupMaster(httpServer, {
loadBalancingMethod: "least-connection",
});
// setup connections between the workers
setupPrimary();
httpServer.listen(PORT);
for (let i = 0; i < 4; i++) {
cluster.fork();
}
} else {
///////////// WORKER
console.log(`Worker ${PID} started`);
const httpServer = http.createServer();
const io = new Server(httpServer);
const [pubClient, subClient] = await getRedisClients();
io.adapter(createClusterAdapter());
io.adapter(createRedisAdapter(pubClient, subClient));
// setup connection with the primary process
setupWorker(io);
io.on("connection", (socket) => {
console.log(`connected to [${PID}]`);
});
setInterval(async () => {
try {
console.log((await io.fetchSockets()).length);
} catch (e) {
console.log(e);
}
}, 3000);
}
})(); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi! This is not currently possible, but that's a great idea 👍 Related: socketio/socket.io-cluster-adapter#1 |
Beta Was this translation helpful? Give feedback.
Hi! This is not currently possible, but that's a great idea 👍
Related: socketio/socket.io-cluster-adapter#1