feat(server): use postgres-adapter for websockets (#5569)
* feat(server): use postgres-adapter for websockets * refactor: create attachment table via migration
This commit is contained in:
@@ -2,4 +2,4 @@ export * from './database-locks';
|
||||
export * from './database.config';
|
||||
export * from './infra.config';
|
||||
export * from './infra.module';
|
||||
export * from './redis-io.adapter';
|
||||
export * from './websocket.adapter';
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class AddWebSocketAttachmentTable1702084989965 implements MigrationInterface {
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(
|
||||
'CREATE TABLE IF NOT EXISTS "socket_io_attachments" (id bigserial UNIQUE, created_at timestamptz DEFAULT NOW(), payload bytea);',
|
||||
);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`DROP TABLE "socket_io_attachments"`);
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { IoAdapter } from '@nestjs/platform-socket.io';
|
||||
import { createAdapter } from '@socket.io/redis-adapter';
|
||||
import Redis from 'ioredis';
|
||||
import { ServerOptions } from 'socket.io';
|
||||
import { redisConfig } from './infra.config';
|
||||
|
||||
export class RedisIoAdapter extends IoAdapter {
|
||||
private readonly logger = new Logger(RedisIoAdapter.name);
|
||||
createIOServer(port: number, options?: ServerOptions): any {
|
||||
const pubClient = new Redis(redisConfig);
|
||||
pubClient.on('error', (error) => {
|
||||
this.logger.error(`Redis pubClient: ${error}`);
|
||||
});
|
||||
const subClient = pubClient.duplicate();
|
||||
subClient.on('error', (error) => {
|
||||
this.logger.error(`Redis subClient: ${error}`);
|
||||
});
|
||||
const server = super.createIOServer(port, options);
|
||||
server.adapter(createAdapter(pubClient, subClient));
|
||||
return server;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { INestApplicationContext } from '@nestjs/common';
|
||||
import { IoAdapter } from '@nestjs/platform-socket.io';
|
||||
import { createAdapter } from '@socket.io/postgres-adapter';
|
||||
import { ServerOptions } from 'socket.io';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { PostgresDriver } from 'typeorm/driver/postgres/PostgresDriver.js';
|
||||
|
||||
export class WebSocketAdapter extends IoAdapter {
|
||||
constructor(private app: INestApplicationContext) {
|
||||
super(app);
|
||||
}
|
||||
|
||||
createIOServer(port: number, options?: ServerOptions): any {
|
||||
const server = super.createIOServer(port, options);
|
||||
const pool = (this.app.get(DataSource).driver as PostgresDriver).master;
|
||||
server.adapter(createAdapter(pool));
|
||||
return server;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user