Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions NestJS/Throttling/websockets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
### Rate Limiting a WebSocket connection
Create a guard like so:
```
@Injectable()
export class WsThrottlerGuard extends ThrottlerGuard {
async handleRequest({
context,
limit,
ttl,
throttler,
}: {
context: ExecutionContext;
limit: number; // limit of requests before connection should be rate limited
ttl: number; // how long requests are considered to be of the same session
throttler: ThrottlerOptions;
}): Promise<boolean> {
const client = context.switchToWs().getClient();
const ip = client.conn.remoteAddress;
const key = this.generateKey(context, ip, throttler.name);
// Increment number of requests made by this key (which is unique for each user and session)
const out = await this.storageService.increment(
key,
ttl,
limit,
60000, // blocked for a minute
'default', // name of the throttler
);
if (out.totalHits > limit) {
throw new ThrottlerException();
}
return true;
}
}
```
This guard can then be applied to a websocket controller or event using UseGuards or to the whole module. Docs: https://docs.nestjs.com/security/rate-limiting