Chapter 6: Modern Protocols: WebSocket & gRPC
Metadata Card
| Attribute | Value |
|---|---|
| Difficulty | |
| Prerequisites | HTTP/1.1 (Chapter 4), TLS basics (Chapter 5) |
| Core Concepts | WebSocket upgrade handshake, full-duplex communication, gRPC + Protocol Buffers, HTTP/2 multiplexing, Streaming |
| Practical Skills | Write a WebSocket client from scratch, build a gRPC service, understand streaming RPC calls |
Your Progress
"HTTP defaults to 'one question, one answer' — you send a spell request, the beacon tower responds, then it's done. But what if the beacon tower wants to proactively push spell messages? Or you need cross-tower remote service calls? WebSocket and gRPC rewrite the conversation rules."
WebSocket Upgrade Handshake
WebSocket involves a protocol gear shift:
Client (HTTP request with Upgrade)
─────────────────────────────→
GET /chat HTTP/1.1
Host: server.example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Server (101 Switching Protocols)
←─────────────────────────────
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=Three steps:
- Client sends a standard HTTP GET with
Upgrade: websocket Sec-WebSocket-Keyis a 16-byte base64 random number — prevents cache proxy confusion- Server SHA-1(Key + magic GUID) → base64 →
Sec-WebSocket-Accept
After handshake: protocol switches from HTTP text headers to WebSocket binary frame protocol.
gRPC & Protocol Buffers
gRPC is Google's high-performance RPC framework. Call remote methods like local functions.
┌───────────┐ ┌───────────┐
│ gRPC │ HTTP/2 │ gRPC │
│ Client │──────────│ Server │
│ │ Frame │ │
│ Stub <─── Proto ───> Service │
└───────────┘ └───────────┘Core contract: .proto file
syntax = "proto3";
package weather;
message GetWeatherRequest {
string city_code = 1;
bool extended = 2;
}
message WeatherResponse {
double temperature = 1;
double humidity = 2;
string description = 3;
int64 timestamp = 4;
}
service WeatherService {
rpc GetWeather(GetWeatherRequest) returns (WeatherResponse);
rpc SubscribeWeather(GetWeatherRequest) returns (stream WeatherResponse);
rpc ReportBatch(stream WeatherResponse) returns (WeatherResponse);
rpc WeatherChat(stream GetWeatherRequest) returns (stream WeatherResponse);
}Four gRPC Call Modes
- Unary RPC — One request, one response (like traditional HTTP POST + JSON)
- Server Streaming RPC — Server sends N messages
- Client Streaming RPC — Client sends N, server returns 1 summary
- Bidirectional Streaming RPC — Both sides stream independently
Comparison: WebSocket vs gRPC vs HTTP/2 SSE
| WebSocket | gRPC | HTTP/2 SSE | |
|---|---|---|---|
| Transport | Custom frame protocol | HTTP/2 frames | HTTP text stream |
| Direction | Full duplex | Full duplex | Server→Client simplex |
| Message format | Arbitrary (often JSON) | Protocol Buffers | Plain text |
| Browser support | Native | Needs grpc-web | Native EventSource |
| Connections | 1 connection | 1 connection (multiplexed) | 1 connection |
Traveler's Notes
WebSocket and gRPC — two completely different answers to the same problem: HTTP/1.1's request-response model doesn't work for push, streaming, or real-time communication. WebSocket abandons HTTP entirely after the handshake; gRPC rebuilds RPC on top of HTTP/2's multiplexing. Choose based on your needs: browser-native full-duplex or high-performance cross-service RPC.