Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:AIScientists Dev WorldSeed useSSE

From Leeroopedia
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>;
}

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment