From 5df8de87e0213cea952bc6d771dd9ff277c87f5f Mon Sep 17 00:00:00 2001 From: romansharapov19 <95644495+romansharapov19@users.noreply.github.com> Date: Thu, 3 Aug 2023 10:36:35 +0100 Subject: [PATCH] feat: use cacheClient instead of create a new redis client (#109) * feat: use cacheClient instead of create a new redis client * feat: add missing type --- helm/intelli-mate-api/values.yaml | 2 +- .../api/src/chats/adapters/redis-io.adapter.ts | 16 +++++++++------- packages/api/src/main.ts | 4 +++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/helm/intelli-mate-api/values.yaml b/helm/intelli-mate-api/values.yaml index e1db5f8..961fc30 100644 --- a/helm/intelli-mate-api/values.yaml +++ b/helm/intelli-mate-api/values.yaml @@ -2,7 +2,7 @@ # This is a YAML-formatted file. # Declare variables to be passed into your templates. -replicaCount: 1 +replicaCount: 2 image: repository: ghcr.io/xgeekshq/intelli-mate/api diff --git a/packages/api/src/chats/adapters/redis-io.adapter.ts b/packages/api/src/chats/adapters/redis-io.adapter.ts index 329a563..4c05d47 100644 --- a/packages/api/src/chats/adapters/redis-io.adapter.ts +++ b/packages/api/src/chats/adapters/redis-io.adapter.ts @@ -1,23 +1,25 @@ +import { INestApplication } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { IoAdapter } from '@nestjs/platform-socket.io'; import { createAdapter } from '@socket.io/redis-adapter'; -import { createClient } from 'redis'; +import { RedisClientType } from 'redis'; import { ServerOptions } from 'socket.io'; export class RedisIoAdapter extends IoAdapter { - constructor(app, private readonly configService: ConfigService) { + constructor( + app: INestApplication, + private readonly configService: ConfigService, + private readonly cacheClient: RedisClientType + ) { super(app); } private adapterConstructor: ReturnType; async connectToRedis(): Promise { - const pubClient = createClient({ - url: `redis://${this.configService.get('REDIS_CONNECTION_URL')}`, - password: this.configService.get('REDIS_HOST_PASSWORD'), - }); + const pubClient = this.cacheClient; const subClient = pubClient.duplicate(); - await Promise.all([pubClient.connect(), subClient.connect()]); + await subClient.connect(); this.adapterConstructor = createAdapter(pubClient, subClient); } diff --git a/packages/api/src/main.ts b/packages/api/src/main.ts index 1c65ac2..a78b871 100644 --- a/packages/api/src/main.ts +++ b/packages/api/src/main.ts @@ -1,3 +1,4 @@ +import { CACHE_CLIENT } from '@/common/constants/cache'; import { ZodValidationPipe } from '@anatine/zod-nestjs'; import { VersioningType } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; @@ -11,6 +12,7 @@ import { RedisIoAdapter } from './chats/adapters/redis-io.adapter'; async function bootstrap() { const app = await NestFactory.create(AppModule); const configService = app.get(ConfigService); + const cacheClient = app.get(CACHE_CLIENT); app.setGlobalPrefix('api'); app.enableVersioning({ @@ -32,7 +34,7 @@ async function bootstrap() { const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('docs', app, document); - const redisIoAdapter = new RedisIoAdapter(app, configService); + const redisIoAdapter = new RedisIoAdapter(app, configService, cacheClient); await redisIoAdapter.connectToRedis(); app.useWebSocketAdapter(redisIoAdapter);