Skip to content

WebSockets and SSE through Cloudflare: timeouts, dropped connections, and the 100-second rule

Cloudflare proxies WebSockets on every plan and passes Server-Sent Events through fine — until a quiet connection hits the ~100-second window or an edge reload drops it. Heartbeats, reconnect logic, and knowing which timeout is whose.

Long-lived connections through Cloudflare fail for three reasons, and the fix is different for each: the ~100-second quiet window (if the origin sends nothing for that long, the edge gives up — a 524 error page before response headers, a silent connection close mid-stream), routine edge churn (colo reloads and reassignments drop long-lived connections without malice), and origin-side proxy timeouts that get blamed on Cloudflare (Nginx’s default proxy_read_timeout is 60 seconds — shorter than Cloudflare’s window). The durable answer is the same one real-time systems always need: heartbeats more frequent than the shortest timeout in the chain, and reconnect logic that treats drops as weather, not disaster.

What’s happening

WebSockets are supported on every Cloudflare plan: the edge honours the Upgrade: websocket handshake and relays frames in both directions. Server-Sent Events are plainer still — a long-running HTTP response with Content-Type: text/event-stream — and pass through as uncached, streamed responses (cf-cache-status: DYNAMIC).

The constraint both share is the proxy’s patience. Cloudflare waits about 100 seconds for an origin to produce response headers; miss that and clients receive a Cloudflare 524 page. That number is fixed on Free, Pro, and Business — raising it is an Enterprise feature. After headers, the same order of quiet applies between bytes: a stream or socket that stays silent long enough gets closed. A WebSocket exchanging pings never notices; an SSE feed with rare events and no heartbeat dies between events.

Separately, edge processes restart as Cloudflare deploys — routine, global, indifferent to your dashboards. Connections held for hours will eventually be dropped even with perfect heartbeats. Client code that auto-reconnects makes this invisible; client code that treats a socket as permanent turns it into a support ticket.

What to check first

  • Timing pattern: drops consistently near 100 seconds point at the edge quiet window; near 60 seconds, at Nginx defaults on your origin; random with hours-long uptimes, at ordinary edge churn.
  • Whether the failure is a 524 error page (origin too slow to start answering) or a mid-stream close (silence after headers).
  • Whether the WebSocket toggle is on: Network » WebSockets in the zone dashboard. On by default on new zones, but worth ten seconds.
  • Whether the same endpoint holds direct-to-origin — the isolation test below.

How to verify

# SSE: watch the stream and time the drop precisely
time curl -N -sS "https://example.com/events" \
  -H "Accept: text/event-stream" | ts '%H:%M:%S'

# Confirm the endpoint is not being cached or buffered by the edge
curl -sI "https://example.com/events" | grep -iE "cf-cache-status|cf-ray|content-type"

# WebSocket handshake through the proxy - expect HTTP/1.1 101
curl -si "https://example.com/ws" \
  -H "Connection: Upgrade" -H "Upgrade: websocket" \
  -H "Sec-WebSocket-Version: 13" -H "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" \
  | head -5

# Isolate: same tests direct to origin - does the 60s vs 100s signature change?
time curl -N -sS --resolve example.com:443:<ORIGIN_IP> "https://example.com/events" \
  -H "Accept: text/event-stream" >/dev/null

# Who closed it? Check origin proxy logs for upstream timeouts
grep -iE "upstream timed out|client prematurely closed" /var/log/nginx/error.log | tail

If the direct test drops at 60 seconds too, your origin proxy owns the bug. If direct runs forever but the proxied stream dies at ~100 seconds of silence, you are looking at the edge window, and heartbeats are the fix.

The fix

  1. Send heartbeats under the window — from the server. For SSE, emit a comment line (: ping) every 20-30 seconds; it keeps every timeout in the chain fed and costs a dozen bytes. For WebSockets, run protocol-level ping/pong on a 30-60 second interval from the server side. Client-only keepalives do not help an SSE stream — SSE is one-directional, so the silence that kills it is the server’s.
  2. Make reconnection a first-class feature. EventSource reconnects natively — implement Last-Event-ID on the server so clients resume without gaps. For WebSockets, exponential backoff with jitter, resubscription of state, and idempotent message handling. This single item converts both the 100-second window and edge churn from outages into noise.
  3. Fix the origin proxy’s shorter fuse. Nginx in front of your app needs proxy_read_timeout, proxy_send_timeout raised beyond your heartbeat interval on the WebSocket/SSE locations, plus proxy_buffering off and X-Accel-Buffering: no for SSE so events are not held back in a buffer. The Nginx WebSocket proxying guide has the full location block.
  4. Keep the endpoint out of cache and challenges. A Cache Rule set to Bypass on /events and /ws paths, and a WAF skip so long-lived clients are not handed an interactive challenge mid-protocol. A challenged WebSocket handshake fails with a 403 your client library reports as a generic connection error.
  5. If the first byte legitimately takes over 100 seconds, restructure. Report generation and bulk exports should return 202 Accepted plus a poll URL or push completion over the existing socket. The 524 timeout is only adjustable on Enterprise, and even there a two-minute synchronous HTTP call is a design smell.
  6. For non-HTTP or genuinely unproxyable protocols, use the right product. Raw TCP services (game servers, MQTT without WebSocket framing, custom protocols) belong on Cloudflare Spectrum or a grey-cloud hostname — accepting, in the grey-cloud case, that the origin IP becomes public and unshielded.

When this fix is wrong

If drops cluster at 60 seconds through proxy and direct, stop tuning Cloudflare — it is your own reverse proxy’s defaults. If connections fail immediately rather than timing out, look at the handshake: the WebSockets toggle, a WAF rule on the path, or an origin that does not speak the upgrade on that port. And if you need tens of thousands of concurrent sockets with strict delivery guarantees, a proxied WebSocket fan-out may be the wrong architecture entirely versus a managed pub/sub layer.

Common mistakes

  • Heartbeating in the wrong direction. Browser-side pings keep a WebSocket alive but do nothing for SSE. The server must speak regularly on any stream it wants kept open.
  • Treating every drop as an incident. Long-lived connections through any proxy get recycled. The metric that matters is reconnect success rate, not connection lifetime.
  • Compressing the event stream into a buffer. Compression middleware that waits for a full buffer before flushing delays SSE events by minutes. Disable compression or flush per event on the stream route.
  • Testing only with short sessions. A five-minute test never meets the failure your users hit at hour three. Soak-test with a client that logs disconnect timestamps.

Prevention

Bake the three numbers into your runbook — your heartbeat interval, your origin proxy timeout, and Cloudflare’s ~100 seconds — and alert on reconnect storms rather than individual drops; the Cloudflare fundamentals docs carry the current connection limits when you need to recheck them. If “is it Cloudflare or is it us” keeps eating your evenings, that triage is literally the job of 24/7 Monitoring & Response — book a monitoring and response review.