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

490
Vistas
How to use redis client in nestjs?

I have a microservice in NestJs, here is the main.ts file:

async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
    transport: Transport.REDIS,
    options: {
      url: 'redis://localhost:6379',
    }
  })
  await app.listen();
  console.log('Redis is running');
}
bootstrap();

I want to make calls to redis db from the service:

import { createClient } from '@redis/client';

async createWorkspaceShareCode(data: GetWorkspaceShareCodeMessage) {
    const client = createClient({
      url: 'redis://localhost:6379'
    });
    await client.connect();

    await client.set('key', 'value');
    const value = await client.get('key');
    return value;
  }

But that's not what I want, here are the issues:

  • every time I make request, it creates new connection
  • I need to write this code in each function to connect to redis (even though I'm already connected in main.ts file) Here is the question: how can I use some kind of RedisService in my NestJs microservice? I have already tried installing redis and nestjs-redis libraries, but the first one creates the issue described above and the second one just throws me an error: https://github.com/skunight/nestjs-redis/issues/97
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What I would do is keep your createMicroservice as you have it as it'll be encessary for a Redis based microservice server. Then you can create a module that exports a redis service like so:

@Module({
  providers: [
    {
      provide: 'REDIS_OPTIONS',
      useValue: {
        url: 'redis://localhost:6379'
      }
    },
    {
      inject: ['REDIS_OPTIONS']
      provide: 'REDIS_CLIENT',
      useFactory: async (options: { url: string }) => {
        const client = createClient(options);
        await client.connect();
        return client;
      }
    }
  ],
  exports: ['REDIS_CLIENT'],
})
export class RedisModule {}

Now in the module that contains the service you want to use add RedisModule to the imports array and in the service you can do @Inject('REDIS_CLIENT') private readonly redis: Redis to inject the redis instance and use it with this.redis.get('key') and this.redis.set('key', value)

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