SSE: the seventh tab never loads

An SSE stream holds an HTTP/1.1 connection for as long as the tab is open, and a browser opens at most six of them per origin. The seventh tab just waits.

What it means

Seven tabs are open, each with an SSE stream. Six get data, while the seventh's request stays pending and never reaches the server. Browsers open at most six HTTP/1.1 connections per origin, and each EventSource holds one for the tab's lifetime.

The limit counts open connections, not requests. An ordinary request holds a connection for a fraction of a second and frees it for the next one, so the queue goes unnoticed. The trouble starts when long-lived requests hold all six. Most often that is SSE, but long polling, a streaming fetch and a large download behave the same way. Then everything going to that origin over HTTP/1.1 waits, including the page itself. WebSocket, HTTP/2 and HTTP/3 are outside this limit.

How to fix it

First, how the limit works.

  • It is the same in Chrome, Edge, Firefox and Safari.
  • It counts per origin, meaning scheme, host and port (RFC 6454 § 3.2), so https://example.com and https://api.example.com get six each.
  • The pool is shared by all tabs of the profile. Once it is full, requests to that origin wait in every tab.
  • The number six is not from an RFC. RFC 9112 § 9.4 only says a client "ought to limit" connections, and the cap of two in RFC 2616 § 8.1.4 is gone.

Turn on HTTP/2 (or HTTP/3). It uses one connection and carries requests as streams inside it. The cap is SETTINGS_MAX_CONCURRENT_STREAMS (RFC 9113 § 6.5.2 recommends ≥ 100), 128 in nginx. No code changes.

curl -sI --http2 https://example.com/ | head -1
# HTTP/2 200

This is how nginx 1.25.1 and later turns HTTP/2 on.

listen 443 ssl;
http2 on;

Older nginx uses listen 443 ssl http2; instead.

Browsers speak HTTP/2 only over TLS, so a plain-http:// dev server is HTTP/1.1, hence "only locally". Past the stream cap, requests queue again.

One stream for all tabs. A SharedWorker holds the connection and fans events out.

// sse-worker.js
const source = new EventSource('/events');
const ports = [];

self.onconnect = (e) => {
  const port = e.ports[0];
  ports.push(port);
  port.start();
};

source.onmessage = (event) => {
  for (const port of ports) port.postMessage(event.data);
};
// in every tab
const worker = new SharedWorker('/sse-worker.js');
worker.port.start();
worker.port.onmessage = (e) => handle(e.data);

Safari 7 to 15.x has no SharedWorker; there use BroadcastChannel plus a leader tab via navigator.locks (15.4+).

WebSocket. It has a separate pool, capped at 255 connections per host in Chrome and 200 browser-wide in Firefox.

Close the stream in hidden tabs.

let source = new EventSource('/events');

document.addEventListener('visibilitychange', () => {
  if (document.hidden) source.close();
  else source = new EventSource('/events');
});

A new EventSource sends no Last-Event-ID, so events fired while the tab was hidden are lost unless you pass the id yourself (/events?from=<id>).

What does not fix it

  • Subdomain sharding. On HTTP/1.1, six per shard, paid for with a DNS lookup and TLS handshake each and split cookies and cache. On HTTP/2 it gains nothing, because browsers may coalesce shards sharing an IP and certificate (RFC 9113 § 9.1.1).
  • Retries. The request is queued, not refused.
  • Server timeouts. The server never sees it.

See it on your own traffic

Devtools shows only its own tab, with a request stuck in "Stalled". Solpuga shows all tabs at once, with six requests to one host and stream in the duration column. The seventh never reaches the proxy. For hosts whose HTTPS it decrypts, Solpuga talks to the browser over HTTP/1.1, so under capture the limit applies even to an HTTP/2 site. See Capture & Inspect Traffic for how Solpuga records a stream.

View in Solpuga

Related