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.

Declarations
#

6 declarations

view source

create_sse_response
#

realtime/sse.ts view source

<T = unknown>(c: Context<any, any, {}>, log: Logger): { response: Response; stream: SseStream<T>; }

Create an SSE response for a Hono context.

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

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

c

Hono context

type Context<any, any, {}>

log

type Logger

returns

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

object with response and stream controller

create_validated_broadcaster
#

realtime/sse.ts view source

<T extends SseNotification>(broadcaster: { broadcast: (channel: string, data: T) => void; }, event_specs: SseEventSpec[], log: Logger): { broadcast: (channel: string, data: T) => void; }

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. SubscriberRegistry)

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

event_specs

event specs to validate against

type SseEventSpec[]

log

type Logger

returns

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

validated broadcaster wrapper (passthrough in production)

SSE_CONNECTED_COMMENT
#

realtime/sse.ts view source

": connected\n\n"

SSE comment sent on connect to flush headers through proxies. Exported for test assertions.

SseEventSpec
#

realtime/sse.ts view source

SseEventSpec

Spec for an SSE event — declares params schema, description, and channel.

method

type string

params

type z.ZodType

description

type string

channel

type string

SseNotification
#

realtime/sse.ts view source

SseNotification

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

SseStream
#

realtime/sse.ts view source

SseStream<T>

Generic SSE stream controller interface.

Transport-agnostic — works with any serializable type.

generics

T

default unknown

send

Send data to the client as a JSON SSE event.

type (data: T) => void

comment

Send a comment (for keep-alive pings).

type (text: string) => void

close

Close the stream.

type () => void

on_close

Register a listener called when the stream closes (client disconnect or explicit close).

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

Imported by
#