Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Connection

The layer that abstracts across transports and handles the connection-level protocol. The externally exposed events from a connection are connect, disconnect and error. A connection only cares about whether it is connected or not.

Higher level semantics such as reconnect or session close, are determined by the consumer of this connection's events, i.e the internal session.

The following events are emitted by the connection are

Event Argument
connect the handshake response
data Message
disconnect CloseReason
close CloseReason
error Error

TODO FB19940 update documentation to reflect the type of the handshake response

Hierarchy

Implements

Index

Constructors

constructor

  • new Connection(transports: Transports, reconnectTimeout: number, recoveryBufferSize: number): Connection
  • Create a new connection

    Parameters

    • transports: Transports

      the transports that can be used for the connecttion

    • reconnectTimeout: number

      a reconnection timeout

    • recoveryBufferSize: number

      size of the recovery buffer

    Returns Connection

Properties

Private closeReason

closeReason: CloseReason

The reason why the connection was closed

Private emitter

emitter: Emitter

The emitter on which to emit stream events

Private fsm

fsm: FSM

A finite state machine that keeps track of the internal state

lastReceivedSequence

lastReceivedSequence: number

The sequence number of the last message that was received

Private lastSentSequence

lastSentSequence: number

The sequence number of the last message that was sent

Private reconnectTimeout

reconnectTimeout: number

The timeout for reconnection attempts

Private recoveryBuffer

recoveryBuffer: RecoveryBuffer

The recovery buffer that stores the last messages that were dispatched

Private Optional scheduledClose

scheduledClose: undefined | number

The timer handle for closing the connection due to timeout

Private Optional scheduledRecoveryBufferTrim

scheduledRecoveryBufferTrim: undefined | number

The timer handle for trimming the recovery buffer

Private transport

transport: CascadeDriver

The active transport

Private transports

transports: Transports

The transports

Methods

close

  • Close the connection. When the connection has been closed, it will emit a disconnected event.

    Parameters

    Returns StreamImpl

closeIdleConnection

  • closeIdleConnection(): void
  • Close the connection when it is idling

    Returns void

connect

  • Establish a connection with a provided connection request and connection options.

    throws

    an IllegalArgumentError if no valid transports are requested

    Parameters

    • request: ConnectionRequest

      the connection request

    • opts: OptionsImpl

      options passed to the transports

    • timeout: number

      a timeout after which the connection will be closed

    Returns void

Private dispatch

  • Internal dispatch method for converting a message into a buffer and dispatching it through the active transport.

    The message is added to the recovery buffer

    Parameters

    Returns void

error

  • Close the stream. This will emit a 'close' event to any assigned listeners. No further events will be emitted.

    Parameters

    Returns Stream

getAvailableSequence

  • getAvailableSequence(): number
  • Get the sequence number of the oldest message in the recovery buffer

    Returns number

    the oldest sequence number

getState

  • getState(): string
  • Get the current state of the connection.

    Returns string

off

  • Remove a listener from a specified event.

    Example:

    // Bind a single listener to the 'foo' event and then deregister it
    var listener = function() {};
    stream.on('foo', listener);
    stream.off('foo', listener);
    

    Example:

    // Bind a listener to the 'foo' event and deregister all listeners
    var listener = function() {};
    stream.on('foo', listener);
    stream.off('foo');
    

    Parameters

    • events: string | CallbackMap
    • Optional listener: StreamCallback

      the listener to remove. All listeners for the event are removed if this is not specified. This argument is ignored if the first argument is a CallbackMap.

    Returns Stream

    this stream.

on

  • Register listeners against events.

    A single listener may be bound to an event by passing the event name and listener function.

    Multiple listeners may be bound by passing in a CallbackMap, mapping event names to listener functions.

    Example:

    // Bind a single listener to the 'foo' event
    stream.on('foo', function(arg1, arg2) {
        console.log("Called for 'foo' event", arg1, arg2);
    });
    

    Example:

    // Bind multiple listeners
    stream.on({
        foo : function() { ... },
        bar : function() { ... },
        baz : function() { ... }
    });
    

    It is possible to bind multiple listeners to the same event. In this case, the listeners will be called in the order they were bound.

    Parameters

    • events: string | CallbackMap

      the event name or CallbackMap mapping event names to listeners

    • Optional listener: StreamCallback

      the listener to bind to the event, if passed as string. This argument is ignored if the first argument is a CallbackMap.

    Returns Stream

    this stream.

Private onClose

  • Close the connection and emit a close event. If the connection was in a disconnected state, then a disconnect event is emitted instead.

    Parameters

    • Optional reason: CloseReason

      the reason the transport closed, if any

    Returns void

Private onData

  • onData(data: Uint8Array): void
  • Parse Message from data and emit the data event.

    If the message could not be parsed, the connection will be cloed with CloseReasonEnum.CONNECTION_ERROR

    Parameters

    • data: Uint8Array

      the raw message data

    Returns void

Private onError

  • onError(err: any): void
  • Handle an error.

    The transport's emitter raises close immediately after error, so onClose runs next and performs the state transition: from an active state it reaches the recoverable disconnected state and emits a disconnect event, which engages the session's reconnection logic.

    TRANSPORT_ERROR is a reconnectable close reason, and the session's reconnect logic still gates on reason.canReconnect and the configured reconnect timeout, so this honours that intent rather than forcing a terminal close.

    Parameters

    • err: any

      the error that occured

    Returns void

Private onHandshakeError

  • onHandshakeError(error: any): void
  • Handle connection response parsing errors.

    Parameters

    • error: any

      error thrown parsing the response

    Returns void

Private onHandshakeSuccess

  • Handle connection responses.

    Parameters

    Returns ConnectionResponse

resetSequences

  • resetSequences(): void
  • Reset the recovery buffer and message sequence numbers

    Returns void

send

  • Send a message.

    Sending messages is only valid in a 'connected' state. This method will return false otherwise.

    Parameters

    Returns boolean

    true if the message was succesfully enqueued on the transport.

Copyright © 2025 DiffusionData Ltd. All Rights Reserved.