Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

329
Visualizações
@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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda