0
0
Node.jsframework~10 mins

WebSocket protocol concept in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - WebSocket protocol concept
Client sends HTTP Upgrade request
Server receives Upgrade request
Server sends HTTP 101 Switching Protocols
WebSocket connection established
Bidirectional message exchange
Connection close initiated by client or server
Connection closed
This flow shows how a WebSocket connection starts with an HTTP upgrade, then allows two-way messages until closed.
Execution Sample
Node.js
const ws = new WebSocket('ws://localhost:8080');
ws.on('open', () => ws.send('Hello'));
ws.on('message', msg => console.log(msg));
This code connects to a WebSocket server, sends 'Hello' when open, and logs incoming messages.
Execution Table
StepActionMessage SentMessage ReceivedConnection State
1Client sends HTTP Upgrade requestUpgrade requestConnecting
2Server receives Upgrade requestConnecting
3Server sends HTTP 101 Switching Protocols101 Switching ProtocolsUpgrading
4Connection establishedOpen
5Client sends 'Hello'HelloOpen
6Server sends 'Welcome'WelcomeOpen
7Client sends 'How are you?'How are you?Open
8Server sends 'Good, thanks!'Good, thanks!Open
9Client initiates closeClose frameClosing
10Server acknowledges closeClose frameClosed
💡 Connection closes after client and server exchange close frames.
Variable Tracker
VariableStartAfter Step 4After Step 5After Step 6After Step 9Final
Connection StateConnectingOpenOpenOpenClosingClosed
Last Message SentNoneNoneHelloHelloClose frameClose frame
Last Message ReceivedNoneNoneNoneWelcomeGood, thanks!Close frame
Key Moments - 3 Insights
Why does the connection start as HTTP before switching to WebSocket?
Because WebSocket begins as a normal HTTP request with an Upgrade header (see Step 1 and 3 in execution_table), allowing servers and proxies to handle it like HTTP before switching protocols.
What does '101 Switching Protocols' mean in the process?
It is the server's response (Step 3) telling the client it agrees to switch from HTTP to WebSocket, enabling the full-duplex communication.
How do client and server know when to close the connection?
They send special 'Close frame' messages (Steps 9 and 10) signaling intent to close, ensuring both sides agree before closing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the connection state after Step 4?
AOpen
BConnecting
CClosing
DClosed
💡 Hint
Check the 'Connection State' column at Step 4 in execution_table.
At which step does the client send the first message after connection is open?
AStep 3
BStep 7
CStep 5
DStep 9
💡 Hint
Look for 'Client sends' actions in the 'Action' column in execution_table.
If the server never sends '101 Switching Protocols', what happens to the connection?
AConnection becomes Open anyway
BConnection stays in Connecting state
CConnection immediately closes
DClient sends Close frame
💡 Hint
Refer to Step 3 and the importance of the 101 response in execution_table.
Concept Snapshot
WebSocket starts as an HTTP request with an Upgrade header.
Server responds with 101 Switching Protocols to agree.
Connection becomes full-duplex for bidirectional messages.
Messages are sent as frames over the open connection.
Close frames signal connection termination by either side.
Full Transcript
The WebSocket protocol begins with the client sending an HTTP request asking to upgrade the connection. The server responds with a 101 Switching Protocols status to confirm the upgrade. Once established, the connection is open for two-way communication where client and server can send messages independently. When either side wants to end the connection, they send a close frame to signal termination. This process ensures a smooth transition from HTTP to a persistent, bidirectional WebSocket connection.