Examples

Example 1

import { SsePlugin } from '@setu-ts/sse-plugin';
import { CAPABILITIES, ISseService } from '@setu-ts/common';

const app = createApplication();
app.register(SsePlugin({ heartbeatMs: 15000, retryMs: 3000 }));
await app.start({ port: 3000 });

app.router.get('/events', async (ctx) => {
  const sse = ctx.services.get<ISseService>(CAPABILITIES.SSE);
  const conn = sse.open(ctx);
  conn.send({ id: '1', data: 'hello world' });
  return conn.result;
});

Classes

c
SseConnection(
ctx: IRequestContext,
runtime: IRuntimeServices,
heartbeatMs: number | undefined,
retryMs: number | undefined,
onClosed: () => void
)

Implements IConn.

c
SseService(
options: SsePluginOptions | undefined,
runtime: IRuntimeServices,
backplane?: IRealtimeBackplane,
logger?: ILogger
)

Implements IService.

Functions

Interfaces

I
ISseConnection

A live SSE connection backed by a ReadableStream.

I
ISseService

Service contract for the SSE hub — registered by the SsePlugin under CAPABILITIES.SSE.

I
SseChannel

A named broadcast channel within the SSE hub.

I
SseMessage

A single SSE event payload.

  • data: JsonValue

    Event data. A string is written literally (split on \n into multiple data: lines); any non-string is JSON.stringify-ed. undefined is forbidden — use {} or omit the message instead.

  • event: string

    Event type name — sent as event: field.

  • id: string

    Unique event identifier — sent as id: field; enables Last-Event-ID resume.

  • retry: number

    Reconnection time in milliseconds — sent as retry: field.

I
SsePluginOptions

Options for the SsePlugin.

  • heartbeatMs: number

    Heartbeat interval in milliseconds. When set, the plugin schedules a repeating : heartbeat\n\n comment frame. Omit to disable (no timer created).

  • retryMs: number

    Reconnection time in milliseconds. When set, the first bytes on every new stream are retry: <ms> advertising the reconnect delay. Omit to send no retry: field.

  • scalingNotice: boolean

    Whether to log one info line at registration when no realtime backplane is registered, stating that channels broadcast in-process only. Defaults to true.

Type Aliases

T
ChannelPublisher = (
name: string,
msg: SseMessage
) => void

Forwards a local publish to peers on other replicas.

Variables

v
CAPABILITIES: { RUNTIME: string; LOGGER: string; CONFIG: string; VALIDATION: string; DATABASE: string; CACHE: string; EVENTS: string; MESSAGING: string; AUTH: string; AUTHORIZATION: string; JWT: string; SCHEDULER: string; METRICS: string; HEALTH: string; OPENAPI: string; TELEMETRY: string; SECRETS: string; AUDIT: string; RESILIENCE: string; STORAGE: string; MAIL: string; NOTIFICATION: string; FEATURE_FLAGS: string; QUEUE: string; CQRS: string; COMMAND_BUS: string; QUERY_BUS: string; MULTI_TENANCY: string; WORKER_POOL: string; DI_CONTAINER: string; HTTP_ADAPTER: string; SSE: string; WEBSOCKET: string; REALTIME_BACKPLANE: string; SSR: string; SESSION: string; SERVICE_DISCOVERY: string; HEALTH_INDICATOR: string; METRIC_REGISTRATION: string; OPENAPI_SCHEMA: string; CLI_COMMAND: string; DECORATOR_HANDLER: string; METADATA_STORE: string; GRPC: string; CLOUDFLARE: string; GRAPHQL: string; STATIC_FILES: string; VIEW: string; }

Standard capability tokens provided by the first-party plugins.

Usage

import * as Server_Sent_Events__SSE__plugin_for_real_time__one_way_server_to_client_messaging_over__text_event_stream___Built_on_the_Milestone_42_streaming_primitive___IResponse_stream_______IRequestContext_signal____ from "sse-plugin/src/index.ts";