# reconnectStream

An event emitter that reports reconnection state changes.

## Methods

### `on(event, callback)`

Listen for state change events.

| Parameter  | Type       | Description                   |
| ---------- | ---------- | ----------------------------- |
| `event`    | `string`   | Event name (always `'state'`) |
| `callback` | `function` | Called with state data        |

{% code lineNumbers="true" %}

```javascript
reconnectStream.on('state', (data) => {
  console.log(data.status, data.code, data.reason);
});
```

{% endcode %}

## Callback Data

| Property | Type     | Description                                                        |
| -------- | -------- | ------------------------------------------------------------------ |
| `status` | `string` | Current state (see table below)                                    |
| `code`   | `number` | Disconnect/error code (only on `disconnected` and `failed` states) |
| `reason` | `string` | Disconnect reason (only on `disconnected` state)                   |

## State Values

| Status           | Description                                                         |
| ---------------- | ------------------------------------------------------------------- |
| `"connecting"`   | Initial connection or reconnection started                          |
| `"reconnecting"` | WebSocket closed unexpectedly, reconnection process begun           |
| `"retrying"`     | Actively attempting to reconnect (after 5-second delay)             |
| `"connected"`    | Successfully reconnected and streaming                              |
| `"disconnected"` | Connection lost. Check `code` and `reason` for details              |
| `"failed"`       | Reconnection gave up. `code` = `4007` when 60-second window expires |

## Disconnect Reasons

When `status` is `"disconnected"`, the `reason` field may contain:

| Reason                      | Description                   |
| --------------------------- | ----------------------------- |
| `"Project Inactive"`        | Project disabled in dashboard |
| `"Worker Node Unavailable"` | No compute nodes available    |
| `"Application Not Found"`   | Invalid appId                 |
| `"Application Error"`       | UE application error          |
| `"Maximum Runtime Reached"` | Session time limit exceeded   |
| `"No streamer connected"`   | No UE instance running        |

## Reconnection Behavior

{% hint style="warning" %}
Auto-reconnection triggers only on WebSocket close codes **1005** and **1006**. All other close codes display the disconnect overlay without attempting reconnection.
{% endhint %}

* Reconnection window: **60 seconds** from first disconnect
* Retry delay: **5 seconds** between attempts
* If the window expires, state transitions to `"failed"` with code `4007`
