Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Real-Time Communication Primitives
CN

Real-Time Communication Primitives

Deep dive into WebSockets, Server-Sent Events (SSE), and gRPC over HTTP/2 for bidirectional communication.

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}
FeatureWebSocketSSEgRPC
DirectionBidirectionalServer → ClientBidirectional
ProtocolHTTP upgrade → framesHTTP (standard)HTTP/2
BinaryYesText onlyYes (Protocol Buffers)
ReconnectionManualBuilt-in (EventSource API)Manual
Browser supportExcellentGood (no IE)Limited (gRPC-web)

gRPC Streaming

gRPC supports four communication patterns:

PatternClientServerExample
UnarySingle messageSingle responseStandard RPC
Server streamingSingle messageStream of responsesNews feed, log tailing
Client streamingStream of messagesSingle responseUpload file, batch processing
BidirectionalStreamStreamChat, real-time game

Choice Guide

NeedChoice
Low-latency bidirectional messagesWebSocket
Server pushing events to browserSSE (simpler, native EventSource)
Service-to-service RPCgRPC (fast, typed, streaming)
Real-time dashboardsSSE (updates: server → browser)
Chat applicationWebSocket (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.

My Private Notes

Notes are auto-saved locally to this device.