WebSockets and Server-Sent Events
Run long-lived bidirectional and streaming HTTP connections through Envoy without timeouts cutting them off.
Long-lived HTTP through Envoy
WebSockets and Server-Sent Events both ride on HTTP/1.1, but they break all the comfortable assumptions of "request → response → done":
- WebSockets start with an HTTP
Upgrade: websockethandshake; the server returns101 Switching Protocolsand the connection becomes a bidirectional binary stream. - SSE is just a long HTTP/1.1 response with
Content-Type: text/event-streamand chunked encoding — the server pushes events whenever it has something to say.
Both are long-lived. Both hit the same idle-timeout traps in Envoy.
WebSockets need an opt-in
The HCM won't proxy upgrades unless you enable them:
filters:
- name: envoy.filters.network.http_connection_manager
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
stat_prefix: edge
codec_type: AUTO
upgrade_configs:
- upgrade_type: websocket # <-- the magic
common_http_protocol_options:
idle_timeout: 1h # connection-level
stream_idle_timeout: 0s # per-stream — disable for WS/SSE
request_timeout: 0s
route_config:
virtual_hosts:
- name: chat
domains: ["*"]
routes:
- match: { prefix: "/ws" }
route:
cluster: chat_backend
timeout: 0s # disable overall route timeout
upgrade_configs:
- upgrade_type: websocket
- match: { prefix: "/events" } # SSE
route: { cluster: notif_backend, timeout: 0s }
http_filters:
- name: envoy.filters.http.router
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
Three timeouts that kill long-lived streams
This applies to BOTH WebSockets and SSE. SSE has no application-level
traffic between server-pushed events, so any silent gap longer than the
timeout closes the stream. Set stream_idle_timeout: 0s for
WS / SSE / long-poll routes — or have the server emit
: keepalive comment lines more often than the timeout.
The route-level timeout is the overall response
timeout. For WebSockets and SSE, set timeout: 0s on the
matching route or every connection dies after 15 seconds with a clean
504.
Even with global upgrade_configs, repeat
upgrade_configs on the route entry for the matched routes.
And the upstream cluster must accept Upgrade headers —
some HTTP/2 backends reject them outright.
Hands-on: prove the timeout
- Run a tiny SSE server:
docker run --rm -d --name sse -p 9000:9000 \
python:3.12-alpine sh -c '
python -c "
import http.server, time
class H(http.server.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header(\"content-type\",\"text/event-stream\")
self.end_headers()
while True:
self.wfile.write(b\"data: hi\\n\\n\"); self.wfile.flush()
time.sleep(30)
http.server.HTTPServer((\"0.0.0.0\",9000),H).serve_forever()
"'
-
Set
stream_idle_timeout: 30sin your Envoy config and make the SSE server's loop sleep60seconds between events. -
Open the SSE stream:
curl -N localhost:10000/events
After ~30 seconds with no events, Envoy resets it. Logs show
UC or DC response flag.
- Set
stream_idle_timeout: 0s, restart Envoy. Now the stream survives indefinitely.
stream_idle_timeout of 5 minutes — clock drift between the client's 4-minute ping interval and the server-side timer caused some pings to land just after the timeout fired.stream_idle_timeout: 0s and rely on TCP keepalive plus app-level pings. The reconnect avalanche disappears overnight. Bonus: per-connection memory drops because Envoy isn't tracking idle timers per stream.SSE-specific tip
For SSE, app-level keepalive comments are the friend you didn't know you needed:
data: real event
\n
: keepalive
\n
: keepalive
\n
data: another real event
\n
Browsers ignore comment lines (:-prefixed); Envoy sees bytes flowing,
so the idle timer doesn't fire. Send one every 15-20 seconds and you
can keep stream_idle_timeout at a sane non-zero value.
What this enables
Real-time features through Envoy with no application-level changes:
- Chat / notifications (WebSocket)
- Live dashboards (SSE)
- Long-poll fallback for browsers that hate WebSockets
- Streaming AI responses (SSE — what ChatGPT uses)
The same auth, rate limiting, and observability filters from earlier modules work on long-lived connections too.
Your SSE stream gets dropped after 5 minutes through Envoy. Which knob is most likely the cause?