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:
- Handshake: Initiates via standard HTTP GET with headers:
Server replies withhttp
Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==101 Switching Protocolsand a SHA-1 hash of the key, converting the TCP socket into a bidirectional binary stream. - 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.