Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Socket

An abstraction over a WebSocket object to deal more easily with the WebSocket protocol.

This interface will usually abstract connection/disconnection as well as dealing with re-connection and disconnection error and handling all events of the WebSocket API (baseline is the W3C WebSocket API).

group

Interfaces

Hierarchy

  • Socket

Implemented by

Index

Properties

isConnected

isConnected: boolean

A property to determine if the current socket implementation is actually connected with the remote endpoint or not. If the Socket is actually connected, consumer can assume messages can be sent through the WebSocket.

Methods

connect

  • Perform an actual connection with the remote WebSocket endoint. This will usually construct a WebSocket instance and initiate the connection with the remote endpoint.

    The method receives a listener which will receive all messages sent through the WebSocket by the remote endpoint.

    Parameters

    • listener: SocketMessageListener

      The actual callback that will receive all the messages sent by the remote endpoint through the WebSocket.

    • Optional options: SocketConnectOptions

      The options that can be passed to the connect method for certain functionalities.

    Returns Promise<void>

disconnect

  • disconnect(): Promise<void>
  • Disconnects the actual socket. This closes the underlying socket by closing it and clean up all resources.

    Returns Promise<void>

send

  • send<T>(message: T): Promise<void>
  • Send a message through the WebSocket. The message is stringified to JSON before being sent to the remote endpoint

    Type parameters

    • T = unknown

    Parameters

    • message: T

      The actual message to send to the remote endpoint.

    Returns Promise<void>

setApiToken

  • setApiToken(apiToken: string): void
  • Assigns a new API token to the stream client instance meaning the previous one is not good anymore.

    This usualy indicates the previous token is not valid anymore. This does not re-trigger a re-connection automatically.

    Instead, it should change the internal state of the Socket instance, and once a re-connection is requested (by the client or by the server), the new API token should be used to re-construct the WebSocket url to contact.

    Parameters

    • apiToken: string

      The new API token that should be now set as the active token

    Returns void

Generated using TypeDoc