WebSockets
Full-duplex persistent connection over a single TCP socket. Starts as HTTP, then upgrades.
Handshake:
GET /chat HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
Response:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
After upgrade, data flows as frames (2-6 bytes overhead per frame vs 500-800 bytes for HTTP headers).
Use cases: chat, live notifications, collaborative editing, gaming, financial tickers.
Server-Sent Events (SSE)
One-way (server → client) push over a single HTTP connection. Lighter than WebSockets for server-to-client scenarios.
Content-Type: text/event-stream
data: {"message": "hello"}
data: {"message": "world"}
event: update
data: {"id": 42}
| Feature | WebSocket | SSE | gRPC |
|---|---|---|---|
| Direction | Bidirectional | Server → Client | Bidirectional |
| Protocol | HTTP upgrade → frames | HTTP (standard) | HTTP/2 |
| Binary | Yes | Text only | Yes (Protocol Buffers) |
| Reconnection | Manual | Built-in (EventSource API) | Manual |
| Browser support | Excellent | Good (no IE) | Limited (gRPC-web) |
gRPC Streaming
gRPC supports four communication patterns:
| Pattern | Client | Server | Example |
|---|---|---|---|
| Unary | Single message | Single response | Standard RPC |
| Server streaming | Single message | Stream of responses | News feed, log tailing |
| Client streaming | Stream of messages | Single response | Upload file, batch processing |
| Bidirectional | Stream | Stream | Chat, real-time game |
Choice Guide
| Need | Choice |
|---|---|
| Low-latency bidirectional messages | WebSocket |
| Server pushing events to browser | SSE (simpler, native EventSource) |
| Service-to-service RPC | gRPC (fast, typed, streaming) |
| Real-time dashboards | SSE (updates: server → browser) |
| Chat application | WebSocket (bidirectional) |
Q: What’s the difference between WebSocket and SSE?
A: WebSocket is bidirectional (client and server can send at any time). SSE is server-to-client only (the client receives pushes via a standard HTTP connection). SSE is simpler for one-way updates (notifications, dashboards); WebSocket is needed for interactive apps.
Q: When would you use gRPC instead of WebSockets?
A: For service-to-service communication within a backend. gRPC provides strong typing via .proto files, auto-generated clients in many languages, Protocol Buffer efficiency, and native streaming patterns. WebSockets are better for browser-to-server real-time communication.
Q: What problem does gRPC streaming solve?
A: Polling. Without streaming, a client must repeatedly poll the server for new data. With gRPC streams, the server pushes data as it arrives, reducing latency and eliminating polling overhead.
Q: How does the WebSocket upgrade work?
A: The client sends a standard HTTP GET with Upgrade: websocket and a Sec-WebSocket-Key. The server responds with 101 Switching Protocols and a Sec-WebSocket-Accept hash. After that, the connection switches from HTTP to WebSocket frame protocol.
Premium Content
Unlock Real-Time Communication Primitives and all premium lessons with a subscription.
From ₹199.99/year — See plans