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
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
Extends
Response
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of events that will be streamed |
Constructors
Constructor
new EventSourceResponse<T>(observable, init?): EventSourceResponse<T>Creates a new EventSourceResponse that streams events from an AsyncObservable.
Parameters
| Parameter | Type | Description |
|---|---|---|
observable | AsyncObservable<T> | The source of events to stream |
init? | EventSourceResponseInit<T> | Configuration options for the response and SSE serialization |
Returns
EventSourceResponse<T>
Overrides
Response.constructorProperties
arrayBuffer()
readonly arrayBuffer: () => Promise<ArrayBuffer>;Returns
Promise<ArrayBuffer>
Inherited from
Response.arrayBufferblob()
readonly blob: () => Promise<Blob>;Returns
Promise<Blob>
Inherited from
Response.blobbody
readonly body: null | ReadableStream<any>;Inherited from
Response.bodybodyUsed
readonly bodyUsed: boolean;Inherited from
Response.bodyUsedclone()
readonly clone: () => Response;Returns
Response
Inherited from
Response.cloneformData()
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
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
Response.formDataheaders
readonly headers: Headers;Inherited from
Response.headersjson()
readonly json: () => Promise<unknown>;Returns
Promise<unknown>
Inherited from
Response.jsonobservable
protected readonly observable: AsyncObservable<T>;The source observable that provides the events
ok
readonly ok: boolean;Inherited from
Response.okredirected
readonly redirected: boolean;Inherited from
Response.redirectedstatus
readonly status: number;Inherited from
Response.statusstatusText
readonly statusText: string;Inherited from
Response.statusTextsubscriber
protected readonly subscriber: Subscriber<T>;The subscriber that handles the event stream
text()
readonly text: () => Promise<string>;Returns
Promise<string>
Inherited from
Response.texttype
readonly type: ResponseType;Inherited from
Response.typeurl
readonly url: string;Inherited from
Response.urlMethods
error()
static error(): ResponseReturns
Response
Inherited from
Response.errorjson()
static json(data, init?): ResponseParameters
| Parameter | Type |
|---|---|
data | any |
init? | ResponseInit |
Returns
Response
Inherited from
Response.jsonredirect()
static redirect(url, status): ResponseParameters
| Parameter | Type |
|---|---|
url | string | URL |
status | ResponseRedirectStatus |
Returns
Response
Inherited from
Response.redirect