Skip to content

Chapter 6: Modern Protocols: WebSocket & gRPC


Metadata Card

AttributeValue
Difficulty
PrerequisitesHTTP/1.1 (Chapter 4), TLS basics (Chapter 5)
Core ConceptsWebSocket upgrade handshake, full-duplex communication, gRPC + Protocol Buffers, HTTP/2 multiplexing, Streaming
Practical SkillsWrite 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:

  1. Client sends a standard HTTP GET with Upgrade: websocket
  2. Sec-WebSocket-Key is a 16-byte base64 random number — prevents cache proxy confusion
  3. 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

protobuf
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

  1. Unary RPC — One request, one response (like traditional HTTP POST + JSON)
  2. Server Streaming RPC — Server sends N messages
  3. Client Streaming RPC — Client sends N, server returns 1 summary
  4. Bidirectional Streaming RPC — Both sides stream independently

Comparison: WebSocket vs gRPC vs HTTP/2 SSE

WebSocketgRPCHTTP/2 SSE
TransportCustom frame protocolHTTP/2 framesHTTP text stream
DirectionFull duplexFull duplexServer→Client simplex
Message formatArbitrary (often JSON)Protocol BuffersPlain text
Browser supportNativeNeeds grpc-webNative EventSource
Connections1 connection1 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.

Built with VitePress | Software Systems Atlas