0
0
Computer Networksknowledge~10 mins

WebSockets for real-time communication in Computer Networks - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - WebSockets for real-time communication
Client sends HTTP request to server
Server responds with HTTP 101 Switching Protocols
WebSocket connection established
Client and Server exchange messages anytime
Connection stays open until closed by either side
Connection closed
The client starts by requesting to upgrade to WebSocket. The server agrees, and then both keep the connection open to send messages instantly until one closes it.
Execution Sample
Computer Networks
Client: Send HTTP Upgrade request
Server: Respond with 101 Switching Protocols
Client & Server: Exchange messages freely
Client or Server: Close connection
Shows the steps to establish and use a WebSocket connection for real-time message exchange.
Analysis Table
StepActionMessage TypeResultConnection State
1Client sends HTTP Upgrade requestHTTP RequestRequest to switch to WebSocketConnecting
2Server responds with 101 Switching ProtocolsHTTP ResponseAgrees to upgradeConnecting
3Connection upgraded to WebSocketProtocol SwitchConnection establishedOpen
4Client sends messageWebSocket FrameMessage received by serverOpen
5Server sends messageWebSocket FrameMessage received by clientOpen
6Client or Server sends close frameWebSocket Close FrameConnection closing initiatedClosing
7Connection closedNoneConnection fully closedClosed
💡 Connection closes when either client or server sends a close frame, ending the session.
State Tracker
VariableStartAfter Step 3After Step 4After Step 6Final
Connection StateDisconnectedOpenOpenClosingClosed
Messages Sent001 (client)1 (client)1 (client)
Messages Received0001 (client)1 (client)
Key Insights - 3 Insights
Why does the connection stay open after the initial HTTP handshake?
Because after the server sends the 101 Switching Protocols response (see execution_table step 2), the protocol changes from HTTP to WebSocket, which keeps the connection open for continuous message exchange.
What happens if the client sends a message before the server agrees to upgrade?
Messages can only be sent after the connection is upgraded (step 3). Sending messages before that would fail because the connection is still HTTP and not ready for WebSocket frames.
How does the connection close properly?
Either side sends a WebSocket Close Frame (step 6), signaling the intent to close. Both sides then close the connection cleanly (step 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the connection state after the server sends the 101 Switching Protocols response?
AConnecting
BOpen
CClosed
DClosing
💡 Hint
Check the 'Connection State' column at step 2 in the execution_table.
At which step does the client first send a WebSocket message?
AStep 3
BStep 2
CStep 4
DStep 6
💡 Hint
Look for the first 'WebSocket Frame' sent by the client in the execution_table.
If the client closes the connection at step 6, what is the connection state immediately after?
AClosed
BClosing
COpen
DConnecting
💡 Hint
Refer to the 'Connection State' column at step 6 in the execution_table.
Concept Snapshot
WebSockets start with an HTTP Upgrade request.
Server responds with 101 Switching Protocols to switch protocols.
Connection stays open for two-way real-time messages.
Messages are sent as WebSocket frames.
Connection closes when either side sends a close frame.
Full Transcript
WebSockets begin when a client sends an HTTP request asking to upgrade the connection. The server replies with a 101 Switching Protocols response to agree. After this, the connection changes from HTTP to WebSocket and stays open. Both client and server can send messages anytime without waiting. This allows real-time communication. The connection remains open until one side sends a close frame, which starts the closing process. Finally, the connection closes fully. This process enables fast, continuous data exchange between client and server.