# Proxies, Buffering and Connection Limits — WebSockets & Server-Sent Events

Source: https://www.skillbyai.com/en/realtime-web/rt-sse-proxies

> Configure reverse proxies for streaming and avoid the HTTP/1.1 per-origin connection limit.

## What sits between the server and the browser

Most SSE problems in production are not in your code but in the path. **Reverse proxies** such as nginx buffer upstream responses by default, so events can arrive in bursts or only when the stream ends; turn buffering off for streaming routes. **Idle timeouts** on proxies and cloud load balancers close connections that send nothing for a while (often 60 seconds); a comment line every 15-30 seconds keeps them open. **Compression** middleware may hold small writes back. Then there is the browser: over **HTTP/1.1**, browsers allow only about six concurrent connections per origin, and each open EventSource uses one. Open the app in seven tabs and the seventh tab's requests hang. **HTTP/2** multiplexes many streams over one connection, so serving SSE over HTTP/2 (which, in browsers, means HTTPS) removes this limit in practice. Alternatively, share one stream between tabs with a `SharedWorker` or `BroadcastChannel`.

## nginx for both SSE and WebSockets

`proxy_buffering off` lets SSE events through immediately. The WebSocket block forwards the `Upgrade` and `Connection` headers, which nginx does not pass on by default, and raises the read timeout so idle sockets are not cut after 60 seconds.

```nginx
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

upstream realtime_app {
    server 10.0.0.11:3000;
    server 10.0.0.12:3000;
}

server {
    listen 443 ssl;
    server_name app.example.in;

    location /ws/ {
        proxy_pass http://realtime_app;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_read_timeout 1h;
    }

    location /events/ {
        proxy_pass http://realtime_app;
        proxy_http_version 1.1;
        proxy_set_header Connection '';
        proxy_buffering off;
        proxy_read_timeout 1h;
    }
}
```

## Couriers who wait for a full bag

A buffering proxy is a courier who will not leave the hub until the bag is full. For parcels that is efficient; for a live score it means you hear about a six only after the over ends. You must tell this courier to run every item out the moment it arrives.

## Test through the real path

An SSE endpoint that streams perfectly on `localhost` can arrive in one lump behind a CDN or ingress. Always test with `curl -N` against the public URL, and watch whether events trickle in one by one or all at once.

**Quiz:** A user opens the dashboard in eight tabs over HTTP/1.1 and later tabs stop loading. What is the likely cause?

- [x] The per-origin connection limit is used up by open EventSource streams
- [ ] SSE only allows one listener per user
- [ ] The server ran out of event ids
- [ ] HTTP/1.1 does not support text/event-stream

*Answer:* The per-origin connection limit is used up by open EventSource streams. Browsers cap HTTP/1.1 connections per origin at around six. Each EventSource holds one, so extra tabs queue. HTTP/2 or one shared stream fixes it.
