Documentation / @eventkit/http / EventSource
Class: EventSource
A drop-in replacement for the standard EventSource class that provides an Observable interface for handling Server-Sent Events (SSE). This class extends the native EventSource class and adds the ability to consume events as an AsyncObservable using the asObservable method.
Example
import { EventSource } from "@eventkit/http";
const source = new EventSource("https://api.example.com/events");
// Get raw message data
source.asObservable<string>().subscribe(data => {
console.log("Received:", data);
// { id: undefined, type: "message", data: "hello" }
});
// Get full event objects including metadata
source.asObservable<string>({ dematerialize: true }).subscribe(event => {
switch (event.type) {
case "message":
console.log("Message:", event.data.data);
break;
case "error":
console.error("Error:", event.data);
break;
case "open":
console.log("Connection opened");
break;
}
});See
Extends
EventSource
Constructors
Constructor
new EventSource(url, init?): EventSourceObservableParameters
| Parameter | Type |
|---|---|
url | string | URL |
init? | EventSourceInit |
Returns
EventSourceObservable
Inherited from
EventSource.constructorProperties
CLOSED
readonly CLOSED: 2;Inherited from
EventSource.CLOSEDCONNECTING
readonly CONNECTING: 0;Inherited from
EventSource.CONNECTINGonerror()
onerror: (this, ev) => any;Parameters
| Parameter | Type |
|---|---|
this | EventSource |
ev | ErrorEvent |
Returns
any
Inherited from
EventSource.onerroronmessage()
onmessage: (this, ev) => any;Parameters
| Parameter | Type |
|---|---|
this | EventSource |
ev | MessageEvent |
Returns
any
Inherited from
EventSource.onmessageonopen()
onopen: (this, ev) => any;Parameters
| Parameter | Type |
|---|---|
this | EventSource |
ev | Event |
Returns
any
Inherited from
EventSource.onopenOPEN
readonly OPEN: 1;Inherited from
EventSource.OPENreadyState
readonly readyState: 0 | 1 | 2;Inherited from
EventSource.readyStateurl
readonly url: string;Inherited from
EventSource.urlwithCredentials
readonly withCredentials: boolean;Inherited from
EventSource.withCredentialsCLOSED
readonly static CLOSED: 2;Inherited from
EventSource.CLOSEDCONNECTING
readonly static CONNECTING: 0;Inherited from
EventSource.CONNECTINGOPEN
readonly static OPEN: 1;Inherited from
EventSource.OPENMethods
addEventListener()
Call Signature
addEventListener<K>(
type,
listener,
options?): voidAdds a new handler for the type event. Any given listener is added only once per type and per capture option value.
If the once option is true, the listener is removed after the next time a type event is dispatched.
The capture option is not used by Node.js in any functional way other than tracking registered event listeners per the EventTarget specification. Specifically, the capture option is used as part of the key when registering a listener. Any individual listener may be added once with capture = false, and once with capture = true.
Type Parameters
| Type Parameter |
|---|
K extends keyof EventSourceEventMap |
Parameters
| Parameter | Type |
|---|---|
type | K |
listener | (this, ev) => any |
options? | boolean | AddEventListenerOptions |
Returns
void
Inherited from
EventSource.addEventListenerCall Signature
addEventListener(
type,
listener,
options?): voidAdds a new handler for the type event. Any given listener is added only once per type and per capture option value.
If the once option is true, the listener is removed after the next time a type event is dispatched.
The capture option is not used by Node.js in any functional way other than tracking registered event listeners per the EventTarget specification. Specifically, the capture option is used as part of the key when registering a listener. Any individual listener may be added once with capture = false, and once with capture = true.
Parameters
| Parameter | Type |
|---|---|
type | string |
listener | EventListenerOrEventListenerObject |
options? | boolean | AddEventListenerOptions |
Returns
void
Inherited from
EventSource.addEventListenerasObservable()
Call Signature
asObservable<T>(opts): AsyncObservable<EventSourceEvent<T>>Converts the EventSource into an AsyncObservable that yields either raw message data or full event objects depending on the options provided.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of data contained in message events |
Parameters
| Parameter | Type | Description |
|---|---|---|
opts | { dematerialize: true; } | Configuration options for the observable |
opts.dematerialize | true | When true, yields full event objects including metadata. When false or omitted, yields only the message data. |
Returns
AsyncObservable<EventSourceEvent<T>>
An AsyncObservable that yields either raw message data or full event objects
Call Signature
asObservable<T>(opts?): AsyncObservable<EventSourceMessage<T>>Converts the EventSource into an AsyncObservable that yields either raw message data or full event objects depending on the options provided.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of data contained in message events |
Parameters
| Parameter | Type | Description |
|---|---|---|
opts? | { dematerialize: false; } | Configuration options for the observable |
opts.dematerialize? | false | When true, yields full event objects including metadata. When false or omitted, yields only the message data. |
Returns
AsyncObservable<EventSourceMessage<T>>
An AsyncObservable that yields either raw message data or full event objects
close()
close(): voidReturns
void
Inherited from
EventSource.closedispatchEvent()
dispatchEvent(event): booleanDispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
Parameters
| Parameter | Type |
|---|---|
event | Event |
Returns
boolean
Inherited from
EventSource.dispatchEventremoveEventListener()
Call Signature
removeEventListener<K>(
type,
listener,
options?): voidRemoves the event listener in target's event listener list with the same type, callback, and options.
Type Parameters
| Type Parameter |
|---|
K extends keyof EventSourceEventMap |
Parameters
| Parameter | Type |
|---|---|
type | K |
listener | (this, ev) => any |
options? | boolean | EventListenerOptions |
Returns
void
Inherited from
EventSource.removeEventListenerCall Signature
removeEventListener(
type,
listener,
options?): voidRemoves the event listener in target's event listener list with the same type, callback, and options.
Parameters
| Parameter | Type |
|---|---|
type | string |
listener | EventListenerOrEventListenerObject |
options? | boolean | EventListenerOptions |
Returns
void
Inherited from
EventSource.removeEventListener