Skip to content

Commit

Permalink
feat: use cacheClient instead of create a new redis client (#109)
Browse files Browse the repository at this point in the history
* feat: use cacheClient instead of create a new redis client

* feat: add missing type
  • Loading branch information
romansharapov19 authored Aug 3, 2023
1 parent 2a6629d commit 5df8de8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion helm/intelli-mate-api/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions packages/api/src/chats/adapters/redis-io.adapter.ts
Original file line number Diff line number Diff line change
@@ -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<typeof createAdapter>;

async connectToRedis(): Promise<void> {
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);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/api/src/main.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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({
Expand All @@ -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);

Expand Down

0 comments on commit 5df8de8

Please sign in to comment.