Given that I'm importing these packages:
import {Socket, Server} from 'socket.io'; // @4.4.1
import {createClient} from 'redis'; // @4.0.4
import {createAdapter} from '@socket.io/redis-adapter'; // @7.1.0
import {Server as HttpServer} from 'http'; // npm@8.5.5, node@v17.7.0
I have a Server that I've created with:
const http = HttpServer // from 'http', creation not in this code
const io = new Server(http, {
path: '/socket/v1/tunnel',
serveClient: false,
cookie: false,
pingTimeout: 295000,
pingInterval: 300000,
});
and Redis pub/sub clients that I've created with:
const pubClient = createClient({
url: redisAddress,
});
const subClient = pubClient.duplicate();
After I have these I connect the Redis clients and add them to an adapter (in io) with the following:
Promise.all([pubClient.connect(), subClient.connect()]).then(() => {
io.adapter(createAdapter(pubClient, subClient));
...
}
With the above I have a working socket server. My problem is that every ~1 hour my connection to the Redis clients errors out. The pubClient reconnects immediately (as I expect it to do as per documentation), but the subClient errors and doesn't reconnect.
I can emit to rooms in Redis from other systems, and while this subClient is connected messages get passed along to the appropriate devices. After the subClient errors the first time (which it does fairly predictably on an interval of about 1 hour) it doesn't reconnect which means that emits to Redis rooms no longer get routed to their destinations.
I have a few questions:
subClient.connect() inside of the handler for subClient.on('error', () => {...}, or have I configured this incorrectly and should make a new distinct client with createClient({...})?This problem was being caused by the redis package. redis@4 does not reconnect on pub/sub channels properly (currently, v4.0.4). Downgrading redis to v3.1.2 solved the reconnect issue entirely