2023-12-08 20:38:25 -05:00
|
|
|
import { INestApplicationContext } from '@nestjs/common';
|
|
|
|
|
import { IoAdapter } from '@nestjs/platform-socket.io';
|
2024-07-03 21:27:29 +01:00
|
|
|
import { createAdapter } from '@socket.io/redis-adapter';
|
|
|
|
|
import { Redis } from 'ioredis';
|
2023-12-08 20:38:25 -05:00
|
|
|
import { ServerOptions } from 'socket.io';
|
2024-07-03 21:27:29 +01:00
|
|
|
import { parseRedisConfig } from 'src/config';
|
2023-12-08 20:38:25 -05:00
|
|
|
|
|
|
|
|
export class WebSocketAdapter extends IoAdapter {
|
|
|
|
|
constructor(private app: INestApplicationContext) {
|
|
|
|
|
super(app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
createIOServer(port: number, options?: ServerOptions): any {
|
|
|
|
|
const server = super.createIOServer(port, options);
|
2024-07-03 21:27:29 +01:00
|
|
|
const pubClient = new Redis(parseRedisConfig());
|
|
|
|
|
const subClient = pubClient.duplicate();
|
|
|
|
|
server.adapter(createAdapter(pubClient, subClient));
|
2023-12-08 20:38:25 -05:00
|
|
|
return server;
|
|
|
|
|
}
|
|
|
|
|
}
|