Options
All
  • Public
  • Public/Protected
  • All
Menu

Class RcSocket<Message>

Type parameters

Hierarchy

  • object & object
    • RcSocket

Index

Constructors

constructor

  • constructor
    name

    RcSocket

    desc

    This behaves like a WebSocket in every way, except if it fails to connect, or it gets disconnected, it will use an exponential backoff until it succesfully connects again.

    It is API compatible with the standard WebSocket API.

    example

    const ws = new RcSocket(wss://host)

    Parameters

    • url: string

      Url to connect to.

    • Optional protocols: string | string[]

      Optional subprotocols.

    • Optional settings: Partial<RcSocketSettings>

    Returns RcSocket

Properties

Protected _attempts

_attempts: number = 1

Protected _closeType

_closeType: RcSocketCloseType | null = null

Protected _connectTimer

_connectTimer: ReturnType<setTimeout> | null = null

Protected _listeners

_listeners: Map<string, Set<RCSocketListener>> = new Map()

Protected _shouldReopen

_shouldReopen: boolean = false

Protected _ws

_ws: RcWebSocket | null = null

onclose

onclose: WebSocket["onclose"] = null

onclosing

onclosing: RcSocketEventHandler = null

onconnecting

onconnecting: RcSocketEventHandler = null

onerror

onerror: WebSocket["onerror"] = null

onmessage

onmessage: WebSocket["onmessage"] = null

onopen

onopen: WebSocket["onopen"] = null

ontimeout

ontimeout: RcSocketEventHandler = null

Optional protocols

protocols: string | string[]

queue

queue: Message[] = []

readyState

readyState: RcSocketReadyState = RcSocketReadyState.CONNECTING

url

url: string

Methods

Protected _close

  • desc

    Helper around ws.close to ensure ws exists. If it does not exist we fail silently. This seemed logical as closing the socket would have the same effect as if the socket never existed. In other words no matter what happens in this method the net effect will always be the same.

    Parameters

    Returns void

Protected _connect

  • _connect(): void
  • desc

    Wrapper around WebSocket creation. By wrapping the raw WebSocket we have the opportunity to manipulate events, change behavior (like adding reconnection logic), and then finally proxy the events as if we were a the actual socket.

    Returns void

Protected _getListenerKey

  • _getListenerKey(type: string, options?: boolean | AddEventListenerOptions): string

Protected _onclose

  • _onclose(evt: CloseEvent): void
  • desc

    Responsible for interpretting the various possible close types (force, retry, etc...) and reconnecting/proxying events accordingly.

    Parameters

    • evt: CloseEvent

      WebSocket onclose evt.

    Returns void

Protected _onerror

  • _onerror(evt: Event): void
  • desc

    Simple proxy for onerror event.

    Parameters

    • evt: Event

      WebSocket onerror evt.

    Returns void

Protected _onmessage

  • _onmessage(evt: MessageEvent): void
  • desc

    Simple proxy for onmessage event.

    Parameters

    • evt: MessageEvent

      WebSocket onmessage evt.

    Returns void

Protected _onopen

  • _onopen(evt: Event): void
  • desc

    Timeout cleanup, state management, and queue handling.

    Parameters

    • evt: Event

      WebSocket onopen evt.

    Returns void

Protected _reconnect

  • _reconnect(): void
  • desc

    Call connect after a delayed timeout. The timeout is calculated using expotential backoff. As connect attempts increase, the time between connect attempts will grow (up to a specified connectionMaxRetryInterval).

    Returns void

Protected _send

  • _send(data: Message): void
  • desc

    Wrapper around ws send to make sure all data is sent in correct format.

    Parameters

    • data: Message

    Returns void

Protected _sendPayload

  • _sendPayload(payload: string): void
  • desc

    Proxy to underlying websocket send method. This is pulled into its own method for debugging/testing purposes.

    Parameters

    • payload: string

    Returns void

Protected _sendQueued

  • _sendQueued(): void

Protected _stateChanged

  • _stateChanged<EventName>(readyState: RcSocketReadyState, evtName: EventName, evt?: GetRcSocketEventType<EventName>): void

Protected _stop

  • _stop(): void
  • desc

    Stop all async code from executing. Used internally anytime a socket is either manually closed, or interpretted as closed

    Returns void

Protected _trigger

  • _trigger<EventName>(evtHandlerName: EventName, evt?: GetRcSocketEventType<EventName>): void
  • desc

    Convenience method for semantically calling handlers.

    Type parameters

    Parameters

    • evtHandlerName: EventName
    • Optional evt: GetRcSocketEventType<EventName>

      Raw WebSocket evt we are proxying.

    Returns void

addEventListener

  • addEventListener(type: string, listener: EventListener | EventListenerObject | null, options?: boolean | AddEventListenerOptions): (Anonymous function)
  • desc

    Registers an event handler of a specific event type on the EventTarget. Unlike the default WebSocket addEventListener method, RcSocket will return the removeEventListener method.

    example

    const listener = evt => console.log(evt) socket.addEventListener('message', listener)

    Parameters

    • type: string

      A case-sensitive string representing the event type to listen for.

    • listener: EventListener | EventListenerObject | null

      The object which receives a notification (an object that implements the Event interface) when an event of the specified type occur.

    • Optional options: boolean | AddEventListenerOptions

      An options object that specifies characteristics about the event listener.

    Returns (Anonymous function)

close

  • close(): void
  • desc

    Explicitly close socket. Overrides default RcSocket reconnection logic.

    example

    socket.close()

    Returns void

kill

  • kill(): void
  • desc

    Hard kill by cleaning up all handlers.

    example

    socket.kill()

    Returns void

open

  • open(): void
  • desc

    Sets initial websocket state and connects. Useful for situations where you want to close the socket and reopen it at a later time.

    example

    socket.close() socket.open()

    Returns void

reboot

  • reboot(): void
  • desc

    Refresh the connection if open (close, re-open). If the app suspects the socket is stale (occurs when changing from wifi -> carrier or vice versa), this method will close the existing socket and reconnect.

    example

    socket.reboot()

    Returns void

removeAllEventListeners

  • removeAllEventListeners(): void
  • desc

    Removes all event listeners from the EventTarget.

    example

    socket.removeAllEventListeners()

    Returns void

removeEventListener

  • removeEventListener(type: string, listener: EventListener | EventListenerObject | null, options?: EventListenerOptions | boolean): void
  • desc

    Removes an event listener from the EventTarget.

    example

    socket.removeEventListener('message', listener)

    Parameters

    • type: string

      A case-sensitive string representing the event type to listen for.

    • listener: EventListener | EventListenerObject | null

      The object which receives a notification (an object that implements the Event interface) when an event of the specified type occur.

    • Optional options: EventListenerOptions | boolean

      An options object that specifies characteristics about the event listener.

    Returns void

reset

  • reset(): void
  • desc

    Reset socket to initial state.

    example

    socket.reset()

    Returns void

send

  • send(data: Message): number | void
  • desc

    Wrapper around ws.send that adds queue functionality when socket is not in a connected readyState.

    example

    socket.send({ prop: 'val' })

    Parameters

    • data: Message

      data to send via web socket.

    Returns number | void

Static addEventListener

  • addEventListener<TEventType>(type: TEventType, listener: EventTarget.Listener<EventTarget.PickEvent<TEvents, TEventType>> | null, options?: boolean | AddOptions): void
  • Add a given listener to this event target.

    Type parameters

    • TEventType: EventTarget.EventType<TEvents, TMode>

    Parameters

    • type: TEventType
    • listener: EventTarget.Listener<EventTarget.PickEvent<TEvents, TEventType>> | null

      The listener to add.

    • Optional options: boolean | AddOptions

      The options for this listener.

    Returns void

Static dispatchEvent

  • dispatchEvent<TEventType>(event: EventTarget.EventData<TEvents, TEventType, TMode>): boolean
  • Dispatch a given event.

    Type parameters

    • TEventType: EventTarget.EventType<TEvents, TMode>

    Parameters

    • event: EventTarget.EventData<TEvents, TEventType, TMode>

      The event to dispatch.

    Returns boolean

    false if canceled.

Static removeEventListener

  • removeEventListener<TEventType>(type: TEventType, listener: EventTarget.Listener<EventTarget.PickEvent<TEvents, TEventType>> | null, options?: boolean | RemoveOptions): void
  • Remove a given listener from this event target.

    Type parameters

    • TEventType: EventTarget.EventType<TEvents, TMode>

    Parameters

    • type: TEventType
    • listener: EventTarget.Listener<EventTarget.PickEvent<TEvents, TEventType>> | null

      The listener to remove.

    • Optional options: boolean | RemoveOptions

      The options for this listener.

    Returns void

Object literals

settings

settings: object

connectionMaxRetryInterval

connectionMaxRetryInterval: number = RcSocket.defaultSettings.connectionMaxRetryInterval

connectionTimeout

connectionTimeout: number = RcSocket.defaultSettings.connectionTimeout

debug

debug: boolean = RcSocket.defaultSettings.debug

logger

logger: RcSocketLogger = RcSocket.defaultSettings.logger

Static defaultSettings

defaultSettings: object

connectionMaxRetryInterval

connectionMaxRetryInterval: number = 1000

connectionTimeout

connectionTimeout: number = 2500

debug

debug: false = false

logger

logger: Console = console

Generated using TypeDoc