fix(server): use ClsInterceptor on websocket (#9859)
https://papooch.github.io/nestjs-cls/considerations/compatibility#websockets
This commit is contained in:
@@ -52,17 +52,28 @@ import { ENABLED_PLUGINS } from './plugins/registry';
|
|||||||
export const FunctionalityModules = [
|
export const FunctionalityModules = [
|
||||||
ClsModule.forRoot({
|
ClsModule.forRoot({
|
||||||
global: true,
|
global: true,
|
||||||
|
// for http / graphql request
|
||||||
middleware: {
|
middleware: {
|
||||||
mount: true,
|
mount: true,
|
||||||
generateId: true,
|
generateId: true,
|
||||||
idGenerator() {
|
idGenerator() {
|
||||||
// make every request has a unique id to tracing
|
// make every request has a unique id to tracing
|
||||||
return randomUUID();
|
return `req-${randomUUID()}`;
|
||||||
},
|
},
|
||||||
setup(cls, _req, res: Response) {
|
setup(cls, _req, res: Response) {
|
||||||
res.setHeader('X-Request-Id', cls.getId());
|
res.setHeader('X-Request-Id', cls.getId());
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
// for websocket connection
|
||||||
|
// https://papooch.github.io/nestjs-cls/considerations/compatibility#websockets
|
||||||
|
interceptor: {
|
||||||
|
mount: true,
|
||||||
|
generateId: true,
|
||||||
|
idGenerator() {
|
||||||
|
// make every request has a unique id to tracing
|
||||||
|
return `ws-${randomUUID()}`;
|
||||||
|
},
|
||||||
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
// https://papooch.github.io/nestjs-cls/plugins/available-plugins/transactional/prisma-adapter
|
// https://papooch.github.io/nestjs-cls/plugins/available-plugins/transactional/prisma-adapter
|
||||||
new ClsPluginTransactional({
|
new ClsPluginTransactional({
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { applyDecorators, Logger } from '@nestjs/common';
|
import { applyDecorators, Logger, UseInterceptors } from '@nestjs/common';
|
||||||
import {
|
import {
|
||||||
ConnectedSocket,
|
ConnectedSocket,
|
||||||
MessageBody,
|
MessageBody,
|
||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
SubscribeMessage as RawSubscribeMessage,
|
SubscribeMessage as RawSubscribeMessage,
|
||||||
WebSocketGateway,
|
WebSocketGateway,
|
||||||
} from '@nestjs/websockets';
|
} from '@nestjs/websockets';
|
||||||
|
import { ClsInterceptor } from 'nestjs-cls';
|
||||||
import { Socket } from 'socket.io';
|
import { Socket } from 'socket.io';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -131,6 +132,7 @@ interface UpdateAwarenessMessage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@WebSocketGateway()
|
@WebSocketGateway()
|
||||||
|
@UseInterceptors(ClsInterceptor)
|
||||||
export class SpaceSyncGateway
|
export class SpaceSyncGateway
|
||||||
implements OnGatewayConnection, OnGatewayDisconnect
|
implements OnGatewayConnection, OnGatewayDisconnect
|
||||||
{
|
{
|
||||||
@@ -147,11 +149,13 @@ export class SpaceSyncGateway
|
|||||||
|
|
||||||
handleConnection() {
|
handleConnection() {
|
||||||
this.connectionCount++;
|
this.connectionCount++;
|
||||||
|
this.logger.log(`New connection, total: ${this.connectionCount}`);
|
||||||
metrics.socketio.gauge('realtime_connections').record(this.connectionCount);
|
metrics.socketio.gauge('realtime_connections').record(this.connectionCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleDisconnect() {
|
handleDisconnect() {
|
||||||
this.connectionCount--;
|
this.connectionCount--;
|
||||||
|
this.logger.log(`Connection disconnected, total: ${this.connectionCount}`);
|
||||||
metrics.socketio.gauge('realtime_connections').record(this.connectionCount);
|
metrics.socketio.gauge('realtime_connections').record(this.connectionCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user