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?): EventSourceObservable
Parameters
Parameter | Type |
---|---|
url | string | URL |
init ? | EventSourceInit |
Returns
EventSourceObservable
Inherited from
EventSource.constructor
Properties
CLOSED
readonly CLOSED: 2;
Inherited from
EventSource.CLOSED
CONNECTING
readonly CONNECTING: 0;
Inherited from
EventSource.CONNECTING
onerror()
onerror: (this, ev) => any;
Parameters
Parameter | Type |
---|---|
this | EventSource |
ev | ErrorEvent |
Returns
any
Inherited from
EventSource.onerror
onmessage()
onmessage: (this, ev) => any;
Parameters
Parameter | Type |
---|---|
this | EventSource |
ev | MessageEvent |
Returns
any
Inherited from
EventSource.onmessage
onopen()
onopen: (this, ev) => any;
Parameters
Parameter | Type |
---|---|
this | EventSource |
ev | Event |
Returns
any
Inherited from
EventSource.onopen
OPEN
readonly OPEN: 1;
Inherited from
EventSource.OPEN
readyState
readonly readyState: 0 | 1 | 2;
Inherited from
EventSource.readyState
url
readonly url: string;
Inherited from
EventSource.url
withCredentials
readonly withCredentials: boolean;
Inherited from
EventSource.withCredentials
CLOSED
readonly static CLOSED: 2;
Inherited from
EventSource.CLOSED
CONNECTING
readonly static CONNECTING: 0;
Inherited from
EventSource.CONNECTING
OPEN
readonly static OPEN: 1;
Inherited from
EventSource.OPEN
Methods
addEventListener()
Call Signature
addEventListener<K>(
type,
listener,
options?): void
Adds 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.addEventListener
Call Signature
addEventListener(
type,
listener,
options?): void
Adds 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.addEventListener
asObservable()
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(): void
Returns
void
Inherited from
EventSource.close
dispatchEvent()
dispatchEvent(event): boolean
Dispatches 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.dispatchEvent
removeEventListener()
Call Signature
removeEventListener<K>(
type,
listener,
options?): void
Removes 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.removeEventListener
Call Signature
removeEventListener(
type,
listener,
options?): void
Removes 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