eventsource
- Version 3.0.2
- Published
- 134 kB
- 1 dependency
- MIT license
Install
npm i eventsource
yarn add eventsource
pnpm add eventsource
Overview
WhatWG/W3C compliant EventSource client for Node.js and browsers
Index
Classes
Interfaces
Type Aliases
Classes
class ErrorEvent
class ErrorEvent extends Event {}
An extended version of the
Event
emitted by theEventSource
object when an error occurs. While the spec does not include any additional properties, we intentionally go beyond the spec and provide some (minimal) additional information to aid in debugging.Modifiers
@public
property code
code?: number;
HTTP status code, if this was triggered by an HTTP error Note: this is not part of the spec, but is included for better error handling.
Modifiers
@public
property message
message?: string;
Optional message attached to the error. Note: this is not part of the spec, but is included for better error handling.
Modifiers
@public
class EventSource
class EventSource_2 extends EventTarget {}
An
EventSource
instance opens a persistent connection to an HTTP server, which sends events intext/event-stream
format. The connection remains open until closed by calling.close()
.Example 1
const eventSource = new EventSource('https://example.com/stream')eventSource.addEventListener('error', (error) => {console.error(error)})eventSource.addEventListener('message', (event) => {console.log('Received message:', event.data)})Modifiers
@public
constructor
constructor(url: string | URL, eventSourceInitDict?: EventSourceInit);
property CLOSED
static CLOSED: number;
ReadyState representing an EventSource connection that is closed (eg disconnected)
Modifiers
@public
property CLOSED
readonly CLOSED: number;
ReadyState representing an EventSource connection that is closed (eg disconnected)
Modifiers
@public
property CONNECTING
static CONNECTING: number;
ReadyState representing an EventSource currently trying to connect
Modifiers
@public
property CONNECTING
readonly CONNECTING: number;
ReadyState representing an EventSource currently trying to connect
Modifiers
@public
property onerror
onerror: (ev: ErrorEvent) => unknown;
[MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/error_event)
property onmessage
onmessage: (ev: MessageEvent) => unknown;
[MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/message_event)
property onopen
onopen: (ev: Event) => unknown;
[MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/open_event)
property OPEN
static OPEN: number;
ReadyState representing an EventSource connection that is open (eg connected)
Modifiers
@public
property OPEN
readonly OPEN: number;
ReadyState representing an EventSource connection that is open (eg connected)
Modifiers
@public
property readyState
readonly readyState: number;
Returns the state of this EventSource object's connection. It can have the values described below.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/readyState)
Note: typed as
number
instead of0 | 1 | 2
for compatibility with theEventSource
interface, defined in the TypeScriptdom
library.Modifiers
@public
property url
readonly url: string;
Returns the URL providing the event stream.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/url)
Modifiers
@public
property withCredentials
readonly withCredentials: boolean;
Returns true if the credentials mode for connection requests to the URL providing the event stream is set to "include", and false otherwise.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/withCredentials)
method addEventListener
addEventListener: { <K extends keyof EventSourceEventMap>( type: K, listener: (this: EventSource_2, ev: EventSourceEventMap[K]) => unknown, options?: boolean | AddEventListenerOptions ): void; ( type: string, listener: (this: EventSource_2, event: MessageEvent<any>) => unknown, options?: boolean | AddEventListenerOptions ): void; ( type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions ): void;};
method close
close: () => void;
Aborts any instances of the fetch algorithm started for this EventSource object, and sets the readyState attribute to CLOSED.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/close)
Modifiers
@public
method removeEventListener
removeEventListener: { <K extends keyof EventSourceEventMap>( type: K, listener: (this: EventSource_2, ev: EventSourceEventMap[K]) => unknown, options?: boolean | EventListenerOptions ): void; ( type: string, listener: (this: EventSource_2, event: MessageEvent<any>) => unknown, options?: boolean | EventListenerOptions ): void; ( type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions ): void;};
Interfaces
interface EventSourceEventMap
interface EventSourceEventMap {}
Mirrors the official DOM typings, with the exception of the extended ErrorEvent.
Modifiers
@public
interface EventSourceInit
interface EventSourceInit {}
Mirrors the official DOM typings (for the most part)
Modifiers
@public
property fetch
fetch?: FetchLike;
Optional fetch implementation to use. Defaults to
globalThis.fetch
. Can also be used for advanced use cases like mocking, proxying, custom certs etc.
property withCredentials
withCredentials?: boolean;
A boolean value, defaulting to
false
, indicating if CORS should be set toinclude
credentials.
interface FetchLikeInit
interface FetchLikeInit {}
Stripped down version of
RequestInit
, only defining the parts we care about.Modifiers
@public
property cache
cache?: 'no-store';
Controls how the request is cached.
property credentials
credentials?: 'include' | 'omit' | 'same-origin';
A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials.
property headers
headers?: Record<string, string>;
A Headers object, an object literal, or an array of two-item arrays to set request's headers.
property mode
mode?: 'cors' | 'no-cors' | 'same-origin';
A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode.
property redirect
redirect?: 'error' | 'follow' | 'manual';
A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect.
property signal
signal?: | { aborted: boolean; } | any;
An AbortSignal to set request's signal. Typed as
any
because of polyfill inconsistencies.
interface FetchLikeResponse
interface FetchLikeResponse {}
Minimal version of the
Response
type returned byfetch()
.Modifiers
@public
property body
readonly body: | { getReader(): ReaderLike; } | Response['body'] | null;
property headers
readonly headers: { get(name: string): string | null;};
property redirected
readonly redirected: boolean;
property status
readonly status: number;
property url
readonly url: string;
interface ReaderLike
interface ReaderLike {}
Stripped down version of
ReadableStreamDefaultReader
, only defining the parts we care about.Modifiers
@public
Type Aliases
type FetchLike
type FetchLike = ( url: string | URL, init?: FetchLikeInit) => Promise<FetchLikeResponse>;
Stripped down version of
fetch()
, only defining the parts we care about. This ensures it should work with "most" fetch implementations.Modifiers
@public
Package Files (1)
Dependencies (1)
Dev Dependencies (20)
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/eventsource
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/eventsource)
- HTML<a href="https://www.jsdocs.io/package/eventsource"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 3727 ms. - Missing or incorrect documentation? Open an issue for this package.