envoy-guide
Module 8 · Beyond HTTP·10 min·intermediate

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: websocket handshake; the server returns 101 Switching Protocols and the connection becomes a bidirectional binary stream.
  • SSE is just a long HTTP/1.1 response with Content-Type: text/event-stream and 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:

YAML
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

stream_idle_timeout defaults to 5 minutes

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.

Route timeout defaults to 15 seconds

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.

HTTP/2 upstreams need per-route opt-in

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

Hands-on · Watch SSE die at 5 minutes (then save it)
  1. Run a tiny SSE server:
Shell
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()
"'
  1. Set stream_idle_timeout: 30s in your Envoy config and make the SSE server's loop sleep 60 seconds between events.

  2. Open the SSE stream:

Shell
curl -N localhost:10000/events

After ~30 seconds with no events, Envoy resets it. Logs show UC or DC response flag.

  1. Set stream_idle_timeout: 0s, restart Envoy. Now the stream survives indefinitely.
Real-world example · A notification fanout service (Slack-shaped)
Problem
500k concurrent WebSocket connections per pod. Engineers see periodic disconnect storms and trace them to the edge Envoy's default 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.
Envoy solution
Set 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:

Text
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.

Quick check

Your SSE stream gets dropped after 5 minutes through Envoy. Which knob is most likely the cause?

Practical task
Finish the quick check and tick off the task to unlock.
Quick checkTasks 0/3