0
0
NestJSframework~10 mins

Why WebSockets enable real-time features in NestJS - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why WebSockets enable real-time features
Client sends HTTP request
Server upgrades connection
WebSocket connection established
Bidirectional communication
Real-time data exchange
Client and Server stay connected
The client starts with an HTTP request, then the server upgrades it to a WebSocket connection, enabling continuous two-way communication for real-time updates.
Execution Sample
NestJS
import { WebSocketGateway, SubscribeMessage, MessageBody } from '@nestjs/websockets';

@WebSocketGateway()
export class EventsGateway {
  @SubscribeMessage('message')
  handleMessage(@MessageBody() data: string): string {
    return `Received: ${data}`;
  }
}
This NestJS WebSocket gateway listens for 'message' events and responds immediately, enabling real-time interaction.
Execution Table
StepActionData SentServer ResponseConnection State
1Client sends HTTP request to serverN/AHTTP 101 Switching ProtocolsUpgrading
2Server upgrades to WebSocketN/AConnection establishedOpen
3Client sends 'message' event with 'Hello'HelloN/AOpen
4Server receives 'message' eventHelloProcesses dataOpen
5Server sends response 'Received: Hello'N/AReceived: HelloOpen
6Client receives responseN/AReceived: HelloOpen
7Connection remains open for more messagesN/AN/AOpen
8Client or server closes connectionN/AConnection closedClosed
💡 Connection closes when client or server decides to end communication.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
connectionStateClosedOpenOpenClosed
dataSentN/AHelloN/AN/A
serverResponseN/AN/AReceived: HelloN/A
Key Moments - 3 Insights
Why does the connection upgrade from HTTP to WebSocket?
Because WebSockets require a persistent connection, the server responds with HTTP 101 to switch protocols, allowing continuous two-way communication as shown in step 2 of the execution_table.
How does the server send data back to the client instantly?
Once the WebSocket connection is open (step 2), the server can push messages anytime without waiting for client requests, demonstrated in step 5 where the server sends 'Received: Hello' immediately.
Why does the connection stay open after messages are exchanged?
WebSockets keep the connection open (step 7) to allow real-time data flow both ways, unlike HTTP which closes after each request.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the connectionState after step 3?
AClosed
BOpen
CUpgrading
DUnknown
💡 Hint
Check the 'connectionState' column in the variable_tracker after step 3.
At which step does the server send the response 'Received: Hello'?
AStep 3
BStep 2
CStep 5
DStep 6
💡 Hint
Look at the 'Server Response' column in the execution_table.
If the client never closes the connection, what happens to connectionState in the final step?
AIt stays Open
BIt changes to Closed
CIt becomes Upgrading
DIt resets to N/A
💡 Hint
Refer to the 'connectionState' variable in variable_tracker and the exit_note.
Concept Snapshot
WebSockets start with an HTTP request that upgrades to a persistent connection.
This allows two-way, real-time communication between client and server.
Unlike HTTP, the connection stays open for instant data exchange.
NestJS uses decorators like @WebSocketGateway and @SubscribeMessage to handle events.
This enables features like chat apps, live notifications, and real-time updates.
Full Transcript
WebSockets enable real-time features by upgrading a normal HTTP connection to a persistent, two-way communication channel. Initially, the client sends an HTTP request. The server responds with a status code 101 to switch protocols, establishing a WebSocket connection. This connection remains open, allowing both client and server to send messages anytime without waiting for a response. In NestJS, this is handled by WebSocket gateways and message handlers. The example code shows a server listening for 'message' events and responding immediately. The execution table traces each step: connection upgrade, message sent by client, server processing, and response sent back. Variables like connection state and data sent change accordingly. This persistent connection is why WebSockets are ideal for real-time apps like chats or live feeds. The connection only closes when either side decides to end it, keeping communication fast and continuous.