Skip to main content
InterviewHubPRO
Section Overview
Q41

Question 41 of 50

Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)

82%
Networking & Protocols (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)

WebSocket Handshake and Framing: HTTP 101 Switching Protocols and Masking.

senior LevelCode ExampleHTTP & REST APIs

In Simple Words:

  1. Handshake: Initiates via standard HTTP GET with headers:
    http
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
    
    Server replies with 101 Switching Protocols and a SHA-1 hash of the key, converting the TCP socket into a bidirectional binary stream.
  2. Framing & Masking: Client-to-server frames must be masked with a 4-byte random XOR key. This prevents poisoned proxy caches from misidentifying WebSocket payloads as cached HTTP responses.