# EventSource keeps reconnecting (error event / server closes stream)

**Code:** `EventSource` · **Category:** HTTP · **Seen on:** HTTP, JavaScript, Node.js

The browser's EventSource reconnects again and again, hammering the server.

## Meaning

Any closed connection or non-200/wrong Content-Type response triggers an automatic reconnect (default 3 s). Return `Content-Type: text/event-stream` with status 200, keep the connection open with heartbeats, honour `retry:` and `id:` / `Last-Event-ID`, and send 204 to stop reconnecting.

## Common causes

- Wrong Content-Type or status code
- Server/proxy closes the stream after a timeout
- Auth failure returning 401/redirect
- App crashes between events
- Short `retry` value

## Quick fix

1. Send `Content-Type: text/event-stream` and keep it open
2. Heartbeat comments every 15–30 s
3. Return 204 to stop clients permanently

## Detailed fix by platform

### JavaScript

1. 

```javascript
const es = new EventSource("/events");
es.onerror = () => console.log("readyState", es.readyState);
```


## How to diagnose

1. **Status/type** — 200 text/event-stream?
2. **Stream** — Closed by timeout?
3. **Auth** — Valid?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/http/sse-reconnect-loop)
