The SocketHandler class manages a WebSocket connection, including connection, reconnection, and event subscription mechanisms.

Constructors

Properties

connected: boolean = false
id: null | string = null
socket: WebSocket = ...
subs: {
    [name: string]: {
        handler: SocketEventHandler<any>;
        id: string;
    }[];
} = {}

Methods

  • Establishes a WebSocket connection to the specified server and handles various connection events such as open, close, error, and message. This method also manages automatic reconnections with backoff in case of disconnections.

    Returns Promise<void>

    Resolves when the connection is successfully established or rejects with an error if the connection fails.

  • Disconnects an active socket connection if it exists.

    This method will close and terminate the current socket connection.

    Returns void

  • Emits an event through the socket connection with the specified event name and data.

    Type Parameters

    • Name extends
          | "template"
          | "refresh"
          | "socket_connection"
          | "api_key"
          | "entry"
          | "entry_status"
          | (keyof SocketEventNamesEntrySync)
          | "group"
          | "language"
          | "media"
          | "template_organizer"
          | "user"
          | "widget"
          | (keyof SocketEventNamesBackup)

    Parameters

    • eventName: Name

      The name of the event to be emitted.

    • data: SocketEventNamesAndTypes[Name]

      The data associated with the event.

    Returns void

  • Registers an internal event handler for either 'open' or 'close' events.

    Parameters

    • type: "open" | "close"
    • handler: (() => Promise<void>)
        • (): Promise<void>
        • Returns Promise<void>

    Returns (() => void)

    A function that can be called to unregister the handler.

      • (): void
      • Returns void

  • Registers an event handler for a specified socket event name.

    Type Parameters

    • Name extends
          | "template"
          | "refresh"
          | "socket_connection"
          | "api_key"
          | "entry"
          | "entry_status"
          | (keyof SocketEventNamesEntrySync)
          | "group"
          | "language"
          | "media"
          | "template_organizer"
          | "user"
          | "widget"
          | (keyof SocketEventNamesBackup)

    Parameters

    • eventName: Name

      The name of the socket event to register the handler for.

    • handler: SocketEventHandler<Name, undefined>

      The handler function to be executed when the event is triggered.

    Returns (() => void)

    • A function to unregister the event handler.
      • (): void
      • Returns void