realtime/sse.ts

SSE (Server-Sent Events) streaming utilities for Hono.

Provides generic helpers for creating SSE response streams and a notification type aligned with JSON-RPC 2.0.

Outbound bound

SseStream.send never waits on its client: each frame is written to the response stream, which holds it until the client reads. What a stream holds unread is kept within SSE_OUTBOUND_QUEUE_BYTES — each frame charged its bytes as written (data: prefix and terminator included) plus OUTBOUND_FRAME_OVERHEAD_BYTES, and a frame always accepted when nothing is unread, so a single frame larger than the budget is still deliverable. A frame that does not fit ends the stream: it is not written, what was unread is dropped, the body ends with no final frame, and the stream's on_close listeners run. One warn line names the stream's account (when the caller passed it) and what was unread; no audit row is written, since each would itself be fanned to every stream. A client that is reading reconnects and misses the frames in between. The twin of the Rust spine's fuz_realtime SSE bound — the same budget and charge.

Keep-alive

Every SSE_KEEP_ALIVE_INTERVAL_MS an open stream writes SSE_KEEP_ALIVE_COMMENT, so a proxy's idle read timeout never cuts a quiet stream. The comment is a frame like any other — held to the same budget, so a client that stopped reading ends its stream on a keep-alive as on data — and the timer stops when the stream closes. keep_alive_ms: null turns it off.

view source

Declarations
#

6 declarations

create_sse_response
#

realtime/sse.ts view source

<T = unknown>(c: Context<any, any, {}>, log: Logger, options?: SseResponseOptions | undefined): { response: Response; stream: SseStream<T>; } import {create_sse_response} from '@fuzdev/fuz_app/realtime/sse.js';

Create an SSE response for a Hono context.

Wraps Hono's streamSSE to provide a {response, stream} API compatible with SseRegistry push-based broadcasting. The callback suspends via a promise that resolves on client disconnect or explicit close(), keeping the stream alive for external sends.

The stream closes when its client disconnects, whenever that happens — also before the handler returns the response, which no runtime reports by cancelling the body. A client already gone when this is called yields a stream that is already closed, so register cleanup with on_close (which runs a late listener at once) before handing the stream anywhere that would count it — SseRegistry.admit after on_close(pending.unsubscribe) refuses it.

Uses hono_stream.write() directly (not writeSSE) to avoid Hono's HTML callback resolution — keeps the same data: JSON\n\n format.

What the stream holds unread is bounded by SSE_OUTBOUND_QUEUE_BYTES; a frame past it ends the stream (module doc, "Outbound bound").

c

type Context<any, any, {}>

log

logger for serialization and on_close listener errors, and for the warning when the stream ends on its outbound budget

type Logger

options?

what the overflow warning names, and the keep-alive interval

optional

returns

{ response: Response; stream: SseStream<T>; }

object with the streaming Response and an SseStream controller

generics

create_sse_response<T = unknown>
T
default unknown

throws

  • Error - when `options.keep_alive_ms` is out of range (`validate_sse_keep_alive_ms`)

create_validated_broadcaster
#

realtime/sse.ts view source

<T extends SseNotification>(broadcaster: { broadcast: (channel: string, data: T) => void; }, event_specs: EventSpec[], log: Logger): { broadcast: (channel: string, data: T) => void; } import {create_validated_broadcaster} from '@fuzdev/fuz_app/realtime/sse.js';

Create a broadcaster that validates events in DEV mode.

In DEV: warns on unknown methods and invalid params. In production: passes through with zero overhead.

broadcaster

duck-typed broadcaster (e.g. SseRegistry)

type { broadcast: (channel: string, data: T) => void; }

event_specs

event specs to validate against

type EventSpec[]

log

logger used to emit DEV warnings on unknown methods or param mismatches

type Logger

returns

{ broadcast: (channel: string, data: T) => void; }

validated broadcaster wrapper (passthrough in production)

generics

create_validated_broadcaster<T extends SseNotification>
T
constraint SseNotification

EventSpec
#

realtime/sse.ts view source

EventSpec import type {EventSpec} from '@fuzdev/fuz_app/realtime/sse.js';

Spec for a push event — declares params schema, description, and channel.

method

Event method name, used as the JSON-RPC notification method.

type string

params

Schema for the notification params payload.

type AnySchema

description

Human-readable description for surface output and docs.

type string

channel?

Channel this event broadcasts on. Omit for cross-channel events.

type string

SseNotification
#

realtime/sse.ts view source

SseNotification import type {SseNotification} from '@fuzdev/fuz_app/realtime/sse.js';

Notification shape aligned with JSON-RPC 2.0.

Uses {method, params} to match the JSON-RPC notification format.

method

Notification method name (e.g. 'run_created', 'host_updated').

type string

params

Method-specific payload.

type unknown

SseResponseOptions
#

realtime/sse.ts view source

SseResponseOptions import type {SseResponseOptions} from '@fuzdev/fuz_app/realtime/sse.js';

Options for create_sse_response.

account_id?

The account the stream belongs to, named in the warning logged when the stream ends on its outbound budget.

type string | null

keep_alive_ms?

How often the stream writes SSE_KEEP_ALIVE_COMMENT, in ms; null writes none. Checked by validate_sse_keep_alive_ms.

type number | null

default SSE_KEEP_ALIVE_INTERVAL_MS

SseStream
#

realtime/sse.ts view source

SseStream<T> import type {SseStream} from '@fuzdev/fuz_app/realtime/sse.js';

Generic SSE stream controller interface.

Transport-agnostic — works with any serializable type.

generics

SseStream<T = unknown>
T
default unknown

send

Send data to the client as a JSON SSE event. Never waits: a frame the stream's outbound budget does not fit ends the stream instead (module doc, "Outbound bound").

type (data: T): void

data

type T
returns void

close

Close the stream.

type (): void

returns void

on_close

Register a listener called when the stream closes (client disconnect or explicit close). Called at once when the stream has already closed — a client can leave before the caller registers — so a cleanup registered through it always runs.

type (fn: () => void): void

fn

type () => void
returns void

Depends on
#

Imported by
#