Skip to main content

error

§9.2.3

EventSource onerror

The error event fires when the EventSource connection fails: network error, wrong Content-Type, non-2xx HTTP response, or the server closes the connection. After an error, the browser automatically schedules a reconnect unless the error is permanent (HTTP 204 stops reconnection). The error event provides no detail about the cause – check readyState to distinguish reconnecting from closed.

Details

The error event is fired by the browser in several situations: 1. Network failure or connection reset 2. Server returns non-2xx status code (except 204 which closes permanently) 3. Server returns wrong Content-Type (not text/event-stream) 4. Server closes the connection gracefully (readyState goes back to CONNECTING, reconnect scheduled)

Critically: the SSE error event does not carry any detail about the error cause. The Event object has no error message. This is a known limitation of the EventSource API.

To distinguish between states: error + readyState CONNECTING → temporary disconnect, will reconnect error + readyState CLOSED → permanent close (204, or source.close() was called)

The browser will NOT reconnect after: HTTP 204 No Content response source.close() was called by application code HTTP redirect to a different origin than the original request

Reconnect timing: the browser uses the retry: value (default typically 1–3 seconds) with exponential backoff in some implementations.

Examples

Error handling
source.onerror = (e) => {
  if (source.readyState === EventSource.CONNECTING) {
    console.log('Disconnected, will reconnect...');
  } else if (source.readyState === EventSource.CLOSED) {
    console.log('Connection permanently closed');
    updateStatus('disconnected');
  }
};
Stop reconnection on 204
# Server response to permanently stop SSE:
HTTP/1.1 204 No Content

# Client error event fires, readyState → CLOSED
# Browser will NOT attempt to reconnect

Gotchas

!

The error Event object carries no error detail – you cannot get the HTTP status code or network error message from it

!

A graceful server close (connection end) fires error with readyState CONNECTING – this is normal reconnect behavior, not a bug

!

HTTP 204 is the only status code that stops automatic reconnection (aside from calling source.close())

!

Errors from wrong Content-Type fire error immediately and permanently close the connection

See Also