You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Setup with existing Redis connection (Recommended)
45
-
46
41
```typescript
47
42
// app.module.ts
48
43
import { Module } from'@nestjs/common';
@@ -107,62 +102,6 @@ async function bootstrap() {
107
102
}
108
103
```
109
104
110
-
## The Problem
111
-
112
-
When scaling your NestJS application horizontally with multiple instances, WebSocket connections become a challenge. By default, Socket.IO connections are tied to a single server instance, which means:
113
-
114
-
- Events sent from one server instance won't reach clients connected to other instances
115
-
- Real-time features break when users connect to different servers
116
-
- Load balancing becomes complex as you need sticky sessions
117
-
118
-
## The Solution
119
-
120
-
This package provides a Redis-backed Socket.IO adapter that uses Redis pub/sub to synchronize events across all server instances. When a server emits an event, it's published to Redis and distributed to all other server instances, ensuring all clients receive the event regardless of which server they're connected to.
121
-
122
-
## How It Works
123
-
124
-
1.**Redis Pub/Sub**: The adapter creates two Redis connections - one for publishing and one for subscribing
125
-
2.**Event Distribution**: When a server emits an event, it's published to a Redis channel
126
-
3.**Cross-Instance Delivery**: All server instances subscribe to the same channels and forward events to their connected clients
127
-
4.**Automatic Management**: Connection lifecycle is handled automatically by the adapter
128
-
129
-
## API
130
-
131
-
### `setupRedisAdapter(app, redisToken?)`
132
-
133
-
Sets up the Redis adapter for the NestJS application.
134
-
135
-
-`app`: NestJS application instance
136
-
-`redisToken` (optional): Redis connection name (defaults to the default connection)
137
-
138
-
### `RedisIoAdapter`
139
-
140
-
The underlying Socket.IO adapter class that handles Redis connections.
0 commit comments