@nestjs-redis/socket.io-adapter 0.11.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -71
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
# @nestjs-redis/socket.io-adapter
|
|
6
6
|
|
|
7
|
-
Redis-powered Socket.IO adapter for NestJS
|
|
7
|
+
Redis-powered Socket.IO adapter for NestJS
|
|
8
8
|
|
|
9
9
|
[](https://www.npmjs.com/package/@nestjs-redis/socket.io-adapter)
|
|
10
10
|
[](https://www.npmjs.com/package/@nestjs-redis/socket.io-adapter)
|
|
@@ -18,31 +18,32 @@ Redis-powered Socket.IO adapter for NestJS enabling scalable WebSocket connectio
|
|
|
18
18
|
|
|
19
19
|
## Features
|
|
20
20
|
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
- **Works with existing connections**: Integrates seamlessly with `@nestjs-redis/client`
|
|
25
|
-
- **Type-safe**: Full TypeScript support
|
|
26
|
-
- **Production-ready**: Built on the official Socket.IO Redis adapter
|
|
21
|
+
- Horizontal scaling with Redis pub/sub
|
|
22
|
+
- Works with existing `@nestjs-redis/client` connections
|
|
23
|
+
- Type-safe, production-ready
|
|
27
24
|
|
|
28
25
|
## Installation
|
|
29
26
|
|
|
27
|
+
### Recommended: Install the complete toolkit
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
npm install @nestjs-redis/kit redis
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Alternative: Install socket.io-adapter package only
|
|
34
|
+
|
|
30
35
|
```bash
|
|
31
|
-
npm install @nestjs-redis/socket.io-adapter
|
|
32
|
-
# or
|
|
33
|
-
yarn add @nestjs-redis/socket.io-adapter
|
|
34
|
-
# or
|
|
35
|
-
pnpm add @nestjs-redis/socket.io-adapter
|
|
36
|
+
npm install @nestjs-redis/socket.io-adapter redis
|
|
36
37
|
```
|
|
37
38
|
|
|
38
39
|
## Quick Start
|
|
39
40
|
|
|
40
|
-
|
|
41
|
+
> **Note**: Examples use `@nestjs-redis/kit` imports (recommended). If you installed only this package, import from `@nestjs-redis/socket.io-adapter` and `@nestjs-redis/client` instead.
|
|
41
42
|
|
|
42
43
|
```typescript
|
|
43
44
|
// app.module.ts
|
|
44
45
|
import { Module } from '@nestjs/common';
|
|
45
|
-
import { RedisModule } from '@nestjs-redis/
|
|
46
|
+
import { RedisModule } from '@nestjs-redis/kit';
|
|
46
47
|
|
|
47
48
|
@Module({
|
|
48
49
|
imports: [
|
|
@@ -57,7 +58,7 @@ export class AppModule {}
|
|
|
57
58
|
```typescript
|
|
58
59
|
// main.ts
|
|
59
60
|
import { NestFactory } from '@nestjs/core';
|
|
60
|
-
import { setupRedisAdapter } from '@nestjs-redis/
|
|
61
|
+
import { setupRedisAdapter } from '@nestjs-redis/kit';
|
|
61
62
|
import { AppModule } from './app.module';
|
|
62
63
|
|
|
63
64
|
async function bootstrap() {
|
|
@@ -103,62 +104,6 @@ async function bootstrap() {
|
|
|
103
104
|
}
|
|
104
105
|
```
|
|
105
106
|
|
|
106
|
-
## The Problem
|
|
107
|
-
|
|
108
|
-
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:
|
|
109
|
-
|
|
110
|
-
- Events sent from one server instance won't reach clients connected to other instances
|
|
111
|
-
- Real-time features break when users connect to different servers
|
|
112
|
-
- Load balancing becomes complex as you need sticky sessions
|
|
113
|
-
|
|
114
|
-
## The Solution
|
|
115
|
-
|
|
116
|
-
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.
|
|
117
|
-
|
|
118
|
-
## How It Works
|
|
119
|
-
|
|
120
|
-
1. **Redis Pub/Sub**: The adapter creates two Redis connections - one for publishing and one for subscribing
|
|
121
|
-
2. **Event Distribution**: When a server emits an event, it's published to a Redis channel
|
|
122
|
-
3. **Cross-Instance Delivery**: All server instances subscribe to the same channels and forward events to their connected clients
|
|
123
|
-
4. **Automatic Management**: Connection lifecycle is handled automatically by the adapter
|
|
124
|
-
|
|
125
|
-
## API
|
|
126
|
-
|
|
127
|
-
### `setupRedisAdapter(app, redisToken?)`
|
|
128
|
-
|
|
129
|
-
Sets up the Redis adapter for the NestJS application.
|
|
130
|
-
|
|
131
|
-
- `app`: NestJS application instance
|
|
132
|
-
- `redisToken` (optional): Redis connection name (defaults to the default connection)
|
|
133
|
-
|
|
134
|
-
### `RedisIoAdapter`
|
|
135
|
-
|
|
136
|
-
The underlying Socket.IO adapter class that handles Redis connections.
|
|
137
|
-
|
|
138
|
-
## Architecture
|
|
139
|
-
|
|
140
|
-
```
|
|
141
|
-
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
|
|
142
|
-
│ Server 1 │ │ Server 2 │ │ Server 3 │
|
|
143
|
-
│ ┌───────┐ │ │ ┌───────┐ │ │ ┌───────┐ │
|
|
144
|
-
│ │Client │ │ │ │Client │ │ │ │Client │ │
|
|
145
|
-
│ └───────┘ │ │ └───────┘ │ │ └───────┘ │
|
|
146
|
-
└──────┬──────┘ └──────┬──────┘ └──────┬──────┘
|
|
147
|
-
│ │ │
|
|
148
|
-
└──────────────────┼──────────────────┘
|
|
149
|
-
│
|
|
150
|
-
┌─────▼─────┐
|
|
151
|
-
│ Redis │
|
|
152
|
-
│ Pub/Sub │
|
|
153
|
-
└───────────┘
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
## Learn More
|
|
157
|
-
|
|
158
|
-
- [NestJS WebSocket Adapter Documentation](https://docs.nestjs.com/websockets/adapter)
|
|
159
|
-
- [Socket.IO Redis Adapter](https://socket.io/docs/v4/redis-adapter/)
|
|
160
|
-
- [Redis Pub/Sub](https://redis.io/docs/manual/pubsub/)
|
|
161
|
-
|
|
162
107
|
## Links
|
|
163
108
|
|
|
164
109
|
- Root repo: [CSenshi/nestjs-redis](https://github.com/CSenshi/nestjs-redis)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestjs-redis/socket.io-adapter",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Saba Pochkhua <saba.pochkhua@gmail.com> (https://github.com/CSenshi)",
|
|
6
6
|
"description": "Redis-powered Socket.IO adapter for NestJS enabling scalable WebSocket connections",
|