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.constructor
Properties
arrayBuffer()
readonly arrayBuffer: () => Promise<ArrayBuffer>;
Returns
Promise
<ArrayBuffer
>
Inherited from
Response.arrayBuffer
blob()
readonly blob: () => Promise<Blob>;
Returns
Promise
<Blob
>
Inherited from
Response.blob
body
readonly body: null | ReadableStream<any>;
Inherited from
Response.body
bodyUsed
readonly bodyUsed: boolean;
Inherited from
Response.bodyUsed
clone()
readonly clone: () => Response;
Returns
Response
Inherited from
Response.clone
formData()
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.formData
headers
readonly headers: Headers;
Inherited from
Response.headers
json()
readonly json: () => Promise<unknown>;
Returns
Promise
<unknown
>
Inherited from
Response.json
observable
protected readonly observable: AsyncObservable<T>;
The source observable that provides the events
ok
readonly ok: boolean;
Inherited from
Response.ok
redirected
readonly redirected: boolean;
Inherited from
Response.redirected
status
readonly status: number;
Inherited from
Response.status
statusText
readonly statusText: string;
Inherited from
Response.statusText
subscriber
protected readonly subscriber: Subscriber<T>;
The subscriber that handles the event stream
text()
readonly text: () => Promise<string>;
Returns
Promise
<string
>
Inherited from
Response.text
type
readonly type: ResponseType;
Inherited from
Response.type
url
readonly url: string;
Inherited from
Response.url
Methods
error()
static error(): Response
Returns
Response
Inherited from
Response.error
json()
static json(data, init?): Response
Parameters
Parameter | Type |
---|---|
data | any |
init ? | ResponseInit |
Returns
Response
Inherited from
Response.json
redirect()
static redirect(url, status): Response
Parameters
Parameter | Type |
---|---|
url | string | URL |
status | ResponseRedirectStatus |
Returns
Response
Inherited from
Response.redirect