Implementation:AIScientists Dev WorldSeed useSSE
| Knowledge Sources | |
|---|---|
| Domains | Frontend, Streaming |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
Concrete React hook managing an EventSource connection to the `/api/stream/live` SSE endpoint with exponential-backoff reconnection, provided by the WorldSeed frontend.
Description
The useSSE hook returns `connect` and `disconnect` functions. `connect` opens an `EventSource` to `/api/stream/live` when in dashboard mode, not viewing history, and world is not in lobby. On message, it pushes records to the stream store, caps the buffer at 2000 (trims to 1500), accumulates token counts, tracks per-agent last-action ticks, and enqueues subtitle cues for live records (skipping catch-up). On error, it closes and schedules reconnection with exponential backoff (base 3s, max 30s, max 8 attempts). Module-level state prevents duplicate connections for the same run.
Usage
Import this hook in the dashboard or polling hook. `pollWorld` calls `sse.connect()` when in dashboard mode. The hook is also used by `usePolling`.
Code Reference
Source Location
- Repository: WorldSeed
- File: frontend/src/hooks/useSSE.ts
Signature
export function useSSE(): {
connect: () => void;
disconnect: () => void;
};
Import
import { useSSE } from '@/hooks/useSSE';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (none) | — | — │ Reads from Zustand stores (appStore, worldStore, etc.) |
Outputs
| Name | Type | Description |
|---|---|---|
| connect | () => void │ Opens EventSource with reconnection logic | |
| disconnect | () => void │ Closes EventSource and resets state |
Usage Examples
import { useEffect } from 'react';
import { useSSE } from '@/hooks/useSSE';
function Dashboard() {
const { connect, disconnect } = useSSE();
useEffect(() => {
connect();
return disconnect;
}, [connect, disconnect]);
return <div>{/* live stream */}</div>;
}