Skip to main content

Class: EventStreamResponse<T>

@divine/web-service.EventStreamResponse

Server-Sent Events (SSE) WebResponse serializer/helper class.

Type parameters

NameTypeDescription
TunknownThe type of events to transmit.

Hierarchy

Constructors

constructor

new EventStreamResponse<T>(source, dataType?, headers?, keepaliveTimeout?)

Converts an AsyncIterable into a Server-Sent Event response stream.

Each object yielded by the source generator will be serialized and converted to an SSE event. Symbols from the EventAttributes interface may be added to transmit event metadata. null or undefined values will result in a comment event. If no value is emitted for keepaliveTimeout milliseconds, a comment line will be sent automatically, in order to signal to the client that the server is still alive and the connection is open.

Exceptions from the generator will be serialized and sent as events of type error.

Type parameters

NameTypeDescription
TunknownThe type of events to transmit.

Parameters

NameTypeDescription
sourceAsyncIterable<undefined | null | T | T & EventAttributes>The AsyncIterable which yields events to transmit.
dataType?string | ContentTypeThe format of the individual events. Default is JSON.
headers?WebResponseHeadersCustom response headers to send.
keepaliveTimeout?numberHow often, in milliseconds, to automatically send comments/keep-alive lines.

Overrides

WebResponse.constructor

Defined in

web-service/src/helpers.ts:230

Properties

body

body: null | Buffer | ReadableStream

The response body.

Inherited from

WebResponse.body

Defined in

web-service/src/response.ts:22


headers

headers: WebResponseHeaders = {}

The HTTP headers to return. If the length of the response body is known, content-length will be added automatically.

Inherited from

WebResponse.headers

Defined in

web-service/src/response.ts:35


status

status: WebStatus

The HTTP status code to return.

Inherited from

WebResponse.status

Defined in

web-service/src/response.ts:35

Methods

close

close(): Promise<void>

Closes the response by destroying the body, in case it is a readable stream.

Returns

Promise<void>

Inherited from

WebResponse.close

Defined in

web-service/src/response.ts:80


serialize

serialize(webreq, config): Promise<RawResponse>

Serializes the response.

For successful GET and HEAD responses, if an etag response header matches the if-none-match request header, NOT_MODIFIED will be returned instead.

If returnRequestID is configured, the request ID will also be automatically added to the response.

Parameters

NameTypeDescription
webreqWebRequestThe request this is a response to.
configRequired<WebServiceConfig>The WebService configuration.

Returns

Promise<RawResponse>

A serialized response.

Inherited from

WebResponse.serialize

Defined in

web-service/src/response.ts:99


setHeader

setHeader(name, value): EventStreamResponse<T>

Adds a custom header to the list of headers to return.

Parameters

NameTypeDescription
namestringThe name of the header to return (case-insensitive).
valueundefined | string | number | boolean | string[]The header value.

Returns

EventStreamResponse<T>

This WebResponse.

Inherited from

WebResponse.setHeader

Defined in

web-service/src/response.ts:73


toString

toString(): string

Returns a short description about this response, including status and content type.

Returns

string

Inherited from

WebResponse.toString

Defined in

web-service/src/response.ts:133