interface ISseService
Since 0.1.0

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

Examples

Example 1

import { CAPABILITIES } from '@setu-ts/common';

const sse = ctx.services.get<ISseService>(CAPABILITIES.SSE);
const conn = sse.open(ctx);
conn.send({ data: 'hello' });

Properties

readonly
connectionCount: number

Current number of open connections.

Since 0.4.0
readonly
channelCount: number

Number of channels the registry currently holds.

The counterpart to IWebSocketService.roomCount, and worth watching rather than merely reporting: a channel is created by ISseService.channel and nothing reclaims it, so for the life of a running application this number only rises. A steadily climbing count means channel names are being derived from unbounded input — use ISseService.peek on any path that reads a caller-supplied name.

The one thing that lowers it is shutdown: the plugin's onClose discards every channel, so a probe racing teardown reads 0 rather than the last live value.

Methods

open(ctx: IRequestContext): ISseConnection

Opens a new SSE connection for the given request context.

Constructs a ReadableStream, captures its controller, sets the SSE response headers on the context's response builder, calls ctx.response.stream(rs) to obtain the HandlerResult, and returns a connection the handler can use to send messages after the handler returns.

channel(name: string): SseChannel

Returns or creates a named channel.

Creating is the point: the first call for a name registers a channel that lives until the process stops. Use ISseService.peek to read a caller-supplied name without registering anything.

Since 0.4.0
peek(name: string): SseChannel | undefined

Returns the named channel if one already exists, without creating it.

The non-allocating counterpart to ISseService.channel. A presence or dashboard endpoint that reports size for a name taken from a request must use this: channel(name) would register one channel per distinct name polled, and an SSE channel is never reclaimed before shutdown.

Usage

import { type ISseService } from "sse-plugin/src/index.ts";