Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
add redis adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Metauriel committed Mar 27, 2024
1 parent 9d385e3 commit 60103fa
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ spec:
envFrom:
- configMapRef:
name: poc-board-collaboration-server-configmap
- secretRef:
name: api-secret
resources:
limits:
cpu: {{ POC_BOARD_COLLABORATION_SERVER__CPU_LIMITS|default("1000m", true) }}
Expand Down
118 changes: 118 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"@nestjs/platform-express": "^10.0.0",
"@nestjs/platform-socket.io": "^10.3.3",
"@nestjs/websockets": "^10.3.3",
"@socket.io/redis-adapter": "^8.3.0",
"redis": "^4.6.13",
"reflect-metadata": "^0.2.0",
"rxjs": "^7.8.1",
"socket.io": "^4.7.4",
Expand Down
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { RedisIoAdapter } from './socket/socket/redis-adapter';

async function bootstrap() {
const app = await NestFactory.create(AppModule, {
cors: true,
});
app.setGlobalPrefix('bc-6683-poc-board-collaboration');

const redisIoAdapter = new RedisIoAdapter(app);
await redisIoAdapter.connectToRedis();

app.useWebSocketAdapter(redisIoAdapter);
await app.listen(3000); // TODO: use env variable
}
bootstrap();
28 changes: 28 additions & 0 deletions src/socket/socket/redis-adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { IoAdapter } from '@nestjs/platform-socket.io';
import { ServerOptions } from 'socket.io';
import { createAdapter } from '@socket.io/redis-adapter';
import { createClient } from 'redis';

export class RedisIoAdapter extends IoAdapter {
private adapterConstructor: ReturnType<typeof createAdapter>;

async connectToRedis(): Promise<void> {
if (process.env.REDIS_URI) {
console.log('found a secret redis uri')
} else {
console.log('did not find a secret redis uri')
}
const pubClient = createClient({ url: process.env.REDIS_URI || `redis://localhost:6379` });
const subClient = pubClient.duplicate();

await Promise.all([pubClient.connect(), subClient.connect()]);

this.adapterConstructor = createAdapter(pubClient, subClient);
}

createIOServer(port: number, options?: ServerOptions): any {
const server = super.createIOServer(port, options);
server.adapter(this.adapterConstructor);
return server;
}
}

0 comments on commit 60103fa

Please sign in to comment.