Skip to content

Documentation / @eventkit/http / EventSourceResponse

Class: EventSourceResponse<T>

A Response subclass that subscribes to an AsyncObservable and streams the values yielded by the observable to the client as Server-Sent Events (SSE). Optional property selectors can be provided to customize the serialization of the events.

Example

ts
const observable = new AsyncObservable<Message>(...);
const response = new EventSourceResponse(observable, {
  sse: {
    getId: (msg) => msg.id,
    getType: (msg) => msg.type,
    getEvent: (msg) => JSON.stringify(msg.data)
  }
});

See

MDN Reference

Extends

  • Response

Type Parameters

Type ParameterDescription
TThe type of events that will be streamed

Constructors

Constructor

ts
new EventSourceResponse<T>(observable, init?): EventSourceResponse<T>

Creates a new EventSourceResponse that streams events from an AsyncObservable.

Parameters

ParameterTypeDescription
observableAsyncObservable<T>The source of events to stream
init?EventSourceResponseInit<T>Configuration options for the response and SSE serialization

Returns

EventSourceResponse<T>

Overrides

ts
Response.constructor

Properties

arrayBuffer()

ts
readonly arrayBuffer: () => Promise<ArrayBuffer>;

Returns

Promise<ArrayBuffer>

Inherited from

ts
Response.arrayBuffer

blob()

ts
readonly blob: () => Promise<Blob>;

Returns

Promise<Blob>

Inherited from

ts
Response.blob

body

ts
readonly body: null | ReadableStream<any>;

Inherited from

ts
Response.body

bodyUsed

ts
readonly bodyUsed: boolean;

Inherited from

ts
Response.bodyUsed

clone()

ts
readonly clone: () => Response;

Returns

Response

Inherited from

ts
Response.clone

formData()

ts
readonly formData: () => Promise<FormData>;

Returns

Promise<FormData>

Deprecated

This method is not recommended for parsing multipart/form-data bodies in server environments. It is recommended to use a library such as @fastify/busboy as follows:

Example

js
import { Busboy } from '@fastify/busboy'
import { Readable } from 'node:stream'

const response = await fetch('...')
const busboy = new Busboy({ headers: { 'content-type': response.headers.get('content-type') } })

// handle events emitted from `busboy`

Readable.fromWeb(response.body).pipe(busboy)

Inherited from

ts
Response.formData

headers

ts
readonly headers: Headers;

Inherited from

ts
Response.headers

json()

ts
readonly json: () => Promise<unknown>;

Returns

Promise<unknown>

Inherited from

ts
Response.json

observable

ts
protected readonly observable: AsyncObservable<T>;

The source observable that provides the events


ok

ts
readonly ok: boolean;

Inherited from

ts
Response.ok

redirected

ts
readonly redirected: boolean;

Inherited from

ts
Response.redirected

status

ts
readonly status: number;

Inherited from

ts
Response.status

statusText

ts
readonly statusText: string;

Inherited from

ts
Response.statusText

subscriber

ts
protected readonly subscriber: Subscriber<T>;

The subscriber that handles the event stream


text()

ts
readonly text: () => Promise<string>;

Returns

Promise<string>

Inherited from

ts
Response.text

type

ts
readonly type: ResponseType;

Inherited from

ts
Response.type

url

ts
readonly url: string;

Inherited from

ts
Response.url

Methods

error()

ts
static error(): Response

Returns

Response

Inherited from

ts
Response.error

json()

ts
static json(data, init?): Response

Parameters

ParameterType
dataany
init?ResponseInit

Returns

Response

Inherited from

ts
Response.json

redirect()

ts
static redirect(url, status): Response

Parameters

ParameterType
urlstring | URL
statusResponseRedirectStatus

Returns

Response

Inherited from

ts
Response.redirect

Released under the MIT License.