eventsource

  • Version 3.0.6
  • Published
  • 146 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

class ErrorEvent

class ErrorEvent extends Event {}
  • An extended version of the Event emitted by the EventSource 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

constructor

constructor(
type: string,
errorEventInitDict?: {
message?: string | undefined;
code?: number | undefined;
}
);
  • Constructs a new ErrorEvent instance. This is typically not called directly, but rather emitted by the EventSource object when an error occurs.

    Parameter type

    The type of the event (should be "error")

    Parameter errorEventInitDict

    Optional properties to include in the error event

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 in text/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 of 0 | 1 | 2 for compatibility with the EventSource interface, defined in the TypeScript dom 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 AddEventListenerOptions

        interface AddEventListenerOptions extends EventListenerOptions {}
        • Mirrors the official DOM typings (sorta).

          Modifiers

          • @public

        property once

        once?: boolean;
        • When true, the listener is automatically removed when it is first invoked. Default: false.

        property passive

        passive?: boolean;
        • When true, serves as a hint that the listener will not call the Event object's preventDefault() method. Default: false.

        property signal

        signal?: AbortSignal;
        • The listener will be removed when the given AbortSignal object's abort() method is called.

        interface EventListener

        interface EventListener {}
        • Mirrors the official DOM typings.

          Modifiers

          • @public

        call signature

        (evt: Event | MessageEvent): void;

          interface EventListenerObject

          interface EventListenerObject {}
          • Mirrors the official DOM typings.

            Modifiers

            • @public

          method handleEvent

          handleEvent: (object: Event) => void;

            interface EventListenerOptions

            interface EventListenerOptions {}
            • Mirrors the official DOM typings (sorta).

              Modifiers

              • @public

            property capture

            capture?: boolean;
            • Not directly used by Node.js. Added for API completeness. Default: false.

            interface EventSourceEventMap

            interface EventSourceEventMap {}
            • Mirrors the official DOM typings, with the exception of the extended ErrorEvent.

              Modifiers

              • @public

            property error

            error: ErrorEvent;

              property message

              message: MessageEvent;

                property open

                open: Event;

                  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 to include 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 by fetch().

                    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

                            method cancel

                            cancel: () => Promise<void>;

                              method read

                              read: () => Promise<
                              { done: false; value: unknown } | { done: true; value?: undefined }
                              >;

                                Type Aliases

                                type EventListenerOrEventListenerObject

                                type EventListenerOrEventListenerObject = EventListener | EventListenerObject;
                                • Mirrors the official DOM typings.

                                  Modifiers

                                  • @public

                                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 onejsDocs.io badgeto 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>