Defjs
Core

defineEventStream

Declare a typed Server-Sent Events endpoint.

@defjs/core · Function

Signature

function defineEventStream<
  const P extends string,
  const M extends string = "GET",
  TInput extends AnyStruct | undefined = undefined,
  TEvents extends EventStructs = EventStructs,
  TError extends AnyStruct | undefined = undefined,
>(
  definition: Omit<EventStreamDefinitionBase<TEvents>, "method" | "path"> &
    EventStreamErrorField<TError> & {
      input?: TInput;
      method?: M;
      path: P;
    } & BuilderField<
      NoInfer<TInput>,
      EventStreamRequestShape<NoInfer<P>, NoInfer<M>>
    >,
): EventStreamEndpoint<EndpointArgs<TInput>, EventStreamData<TEvents>, TError>;

Pass path, event structs, and buffer/queue limits. Call the returned operation with a client and, when declared, the input.

  • definition — path, an events map, and the required maxBufferSize and maxQueueSize limits (positive safe integers). Optional method (default 'GET'), operation, input, and error. A declared input always requires an explicit builder. input only defines the parameters; the builder alone decides where each value goes. Only definitions without input may omit builder.

EventStreamDefinitionBase, EventStreamErrorField, and BuilderField are internal helper types: error is required only when TError is a struct, and builder is (input: BuilderInput<TInput>) => EventStreamRequestShape when input is declared.

Parameters

Prop

Type

definition Omit<EventStreamDefinitionBase<TEvents>, "method" | "path"> & EventStreamErrorField<TError> & { input?: TInput; method?: M; path: P; } & BuilderField< NoInfer<TInput>, EventStreamRequestShape<NoInfer<P>, NoInfer<M>> >

SSE contract (path, events, buffer limits, optional input).

Returns

An operation (client, ...input) => Promise<error-first tuple>.

Examples

import { defineEventStream, struct } from "@defjs/core";

const ticks = defineEventStream({
  maxBufferSize: 64 * 1024,
  maxQueueSize: 100,
  path: "/ticks",
  events: { message: struct.object({ text: struct.string() }) },
});
Edit this page on GitHub
Released under the MIT License. · Copyright © 2026 MunMunMiao