feat(iios): socket.io Redis adapter for cross-replica realtime (P9 scaling)
Adds an opt-in RedisIoAdapter (wired in main.ts when REDIS_URL is set) so socket.io room emits fan out across instances via Redis pub/sub — a client on replica B now receives messages emitted by replica A. Without REDIS_URL the in-memory adapter is kept (single-instance dev unchanged). Adds redis to docker-compose, REDIS_URL to the env contract, and smoke-realtime-cluster.mjs which proves cross-instance delivery fails without Redis and passes with it. Delivery is at-least-once at N>1; clients dedupe by message id (documented). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import { NestFactory } from '@nestjs/core';
|
||||
import { ValidationPipe } from '@nestjs/common';
|
||||
import { AppModule } from './app.module';
|
||||
import { traceMiddleware } from './observability/trace.middleware';
|
||||
import { RedisIoAdapter } from './realtime/redis-io.adapter';
|
||||
|
||||
async function bootstrap(): Promise<void> {
|
||||
const app = await NestFactory.create(AppModule, { rawBody: true });
|
||||
@@ -12,6 +13,15 @@ async function bootstrap(): Promise<void> {
|
||||
new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }),
|
||||
);
|
||||
app.enableCors({ origin: true, credentials: true });
|
||||
|
||||
// Multi-replica realtime: fan socket.io room emits across instances via Redis.
|
||||
// Opt-in — without REDIS_URL the default in-memory adapter is used (single instance).
|
||||
if (process.env.REDIS_URL) {
|
||||
const redisAdapter = new RedisIoAdapter(app, process.env.REDIS_URL);
|
||||
await redisAdapter.connect();
|
||||
app.useWebSocketAdapter(redisAdapter);
|
||||
}
|
||||
|
||||
const port = Number(process.env.PORT ?? 3200);
|
||||
await app.listen(port);
|
||||
// eslint-disable-next-line no-console
|
||||
|
||||
Reference in New Issue
Block a user