Performance: WebSocket gateway creation
MEDIUM IMPACT
This affects the real-time communication speed and responsiveness between client and server.
import { WebSocketGateway, SubscribeMessage, MessageBody } from '@nestjs/websockets'; @WebSocketGateway() export class ChatGateway { @SubscribeMessage('message') async handleMessage(client: any, payload: any): Promise<void> { // offload heavy work asynchronously await someAsyncProcessing(payload); client.emit('message', payload); } } async function someAsyncProcessing(data: any) { return new Promise(resolve => setTimeout(resolve, 0)); }
import { WebSocketGateway, SubscribeMessage, MessageBody } from '@nestjs/websockets'; @WebSocketGateway() export class ChatGateway { @SubscribeMessage('message') handleMessage(client: any, payload: any): void { // heavy synchronous processing for (let i = 0; i < 1000000000; i++) {} client.emit('message', payload); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous heavy processing in gateway | 0 | 0 | 0 | [X] Bad |
| Asynchronous non-blocking message handling | 0 | 0 | 0 | [OK] Good |