Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

332
Vistas
@socket.io/redis-adapter and redis; sub client doesn't reconnect after error

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:

  1. Why doesn't the Redis Client reconnect? The pubClient reconnects after errors consistently, but the subClient does not. According to Redis documentation it should reconnect automatically, why doesn't it?
  2. How can I configure the subClient to reconnect after errors? should I add a 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({...})?
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda