Implementing WebSocket Support

1. Configuring WebSocket Endpoints

Example: NGINX WebSocket proxy

location /ws/ {
  proxy_pass http://ws_backend;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
  proxy_set_header Host $host;
  proxy_read_timeout 3600s;
  proxy_send_timeout 3600s;
}
SettingRequired
HTTP/1.1Yes (upgrade requires)
Upgrade header forwardedYes
Long timeoutsYes (idle connections)
No bufferingYes (streaming)

2. Setting Up WebSocket Routing

MatchExample
Path prefix/ws/chat
SubprotocolSec-WebSocket-Protocol: graphql-ws
Origin checkCORS for WS
Sticky LBHash by session

3. Implementing WebSocket Authentication

MethodDetail
Cookie + Origin checkBrowser auto
Query param token?token=jwt (logged risk)
First message authSend token after connect
Subprotocol tokenSec-WebSocket-Protocol: token, jwt-xxx

4. Configuring Connection Timeouts

TimeoutRecommended
Handshake10s
Idle (no traffic)5 min + ping/pong
Max session1-24h, then force reconnect
Ping interval30s

5. Setting Message Size Limits

LimitDefault
Per-message64 KB - 1 MB
Per-frame64 KB
Per-client send rate100 msg/sec
Connections per IP20-100

6. Implementing WebSocket Load Balancing

StrategyNotes
IP hashSticky, simple
Cookie hashSurvives IP change
Least connBest for long-lived
Consistent hashPub/sub topic routing

7. Using WebSocket Health Checks

MethodNotes
HTTP /healthzIndirect (parallel)
WS ping frameServer pings client
Custom heartbeatApp-level {type:"ping"}
Close on missDrop after 2 missed pongs

8. Configuring Compression

ExtensionDetail
permessage-deflateRFC 7692
Server max window15 (default)
Context takeoverReuses dictionary, more compress
CPU vs bandwidthDisable for small msgs

9. Setting Up WebSocket Logging

EventLog Field
Connectsession_id, user, origin, subprotocol
Messagecount, bytes (not body for PII)
Closeclose_code, reason, duration
Errorerror code, stack

10. Implementing Connection Pooling

SettingValue
Max conns per worker10k-50k (epoll)
Per-IP limit50-100
BackpressureDrop oldest msg in queue
Reconnect backoff (client)Exponential + jitter