Skip to content

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 ​

ts
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 ​

MDN Reference

Extends ​

  • EventSource

Constructors ​

Constructor ​

ts
new EventSource(url, init?): EventSourceObservable

Parameters ​

ParameterType
urlstring | URL
init?EventSourceInit

Returns ​

EventSourceObservable

Inherited from ​

ts
EventSource.constructor

Properties ​

CLOSED ​

ts
readonly CLOSED: 2;

Inherited from ​

ts
EventSource.CLOSED

CONNECTING ​

ts
readonly CONNECTING: 0;

Inherited from ​

ts
EventSource.CONNECTING

onerror() ​

ts
onerror: (this, ev) => any;

Parameters ​

ParameterType
thisEventSource
evErrorEvent

Returns ​

any

Inherited from ​

ts
EventSource.onerror

onmessage() ​

ts
onmessage: (this, ev) => any;

Parameters ​

ParameterType
thisEventSource
evMessageEvent

Returns ​

any

Inherited from ​

ts
EventSource.onmessage

onopen() ​

ts
onopen: (this, ev) => any;

Parameters ​

ParameterType
thisEventSource
evEvent

Returns ​

any

Inherited from ​

ts
EventSource.onopen

OPEN ​

ts
readonly OPEN: 1;

Inherited from ​

ts
EventSource.OPEN

readyState ​

ts
readonly readyState: 0 | 1 | 2;

Inherited from ​

ts
EventSource.readyState

url ​

ts
readonly url: string;

Inherited from ​

ts
EventSource.url

withCredentials ​

ts
readonly withCredentials: boolean;

Inherited from ​

ts
EventSource.withCredentials

CLOSED ​

ts
readonly static CLOSED: 2;

Inherited from ​

ts
EventSource.CLOSED

CONNECTING ​

ts
readonly static CONNECTING: 0;

Inherited from ​

ts
EventSource.CONNECTING

OPEN ​

ts
readonly static OPEN: 1;

Inherited from ​

ts
EventSource.OPEN

Methods ​

addEventListener() ​

Call Signature ​

ts
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 ​
ParameterType
typeK
listener(this, ev) => any
options?boolean | AddEventListenerOptions
Returns ​

void

Inherited from ​
ts
EventSource.addEventListener

Call Signature ​

ts
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 ​
ParameterType
typestring
listenerEventListenerOrEventListenerObject
options?boolean | AddEventListenerOptions
Returns ​

void

Inherited from ​
ts
EventSource.addEventListener

asObservable() ​

Call Signature ​

ts
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 ParameterDescription
TThe type of data contained in message events
Parameters ​
ParameterTypeDescription
opts{ dematerialize: true; }Configuration options for the observable
opts.dematerializetrueWhen 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 ​

ts
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 ParameterDescription
TThe type of data contained in message events
Parameters ​
ParameterTypeDescription
opts?{ dematerialize: false; }Configuration options for the observable
opts.dematerialize?falseWhen 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() ​

ts
close(): void

Returns ​

void

Inherited from ​

ts
EventSource.close

dispatchEvent() ​

ts
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 ​

ParameterType
eventEvent

Returns ​

boolean

Inherited from ​

ts
EventSource.dispatchEvent

removeEventListener() ​

Call Signature ​

ts
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 ​
ParameterType
typeK
listener(this, ev) => any
options?boolean | EventListenerOptions
Returns ​

void

Inherited from ​
ts
EventSource.removeEventListener

Call Signature ​

ts
removeEventListener(
   type, 
   listener, 
   options?): void

Removes the event listener in target's event listener list with the same type, callback, and options.

Parameters ​
ParameterType
typestring
listenerEventListenerOrEventListenerObject
options?boolean | EventListenerOptions
Returns ​

void

Inherited from ​
ts
EventSource.removeEventListener

Released under the MIT License.