Server to Server events in Socket.io

106 Views Asked by At

I'm using Socket.io with Redis Adapter in GKE where I have two replicas of socket server I want to create something scale able that's why I'm using redis pub/sub but I also need to listen when something happen in other replica, is it possible to send events from one replica to another replica.

Server1.js

import { Server } from "socket.io";
import { createAdapter } from "@socket.io/redis-adapter";
import { createClient } from "redis";

const io = new Server();

const pubClient = createClient({ url: "redis://localhost:6379" });
const subClient = pubClient.duplicate();

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

io.on("custom-event", () => {
console.log("Custom event occur");
});

From replica I want to fire event

import { Server } from "socket.io";
import { createAdapter } from "@socket.io/redis-adapter";
import { createClient } from "redis";

const io = new Server();

const pubClient = createClient({ url: "redis://localhost:6379" });
const subClient = pubClient.duplicate();

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

io.on("connection", socket => {
    socket.emit("custom-event"); // not working
    io.emit("custom event") // not working
})

How is it possible or is there any alternative.

0

There are 0 best solutions below