ConnectionManager

flitter-socket/ConnectionManager~ ConnectionManager

Transactional websocket connection manager.


Constructor

new ConnectionManager(ws, req, controller, do_bootstrapopt)

Initialize the connection manager.

Parameters:
Name Type Attributes Default Description
ws WebSocket

the open connection

req express/Request

the connection request

controller object

the connection's controller

do_bootstrap boolean <optional>
true

if false, the socket connection will NOT be automatically bootstrapped to use transactional communication

Methods

(private) _bootstrap(socket)

Bootstrap a websocket connection to use transactional processing. When new messages come in, validate them and handle them as requests or responses based on their data.

Parameters:
Name Type Description
socket WebSocket

(private) _is_json(string) → {boolean}

Checks if a string is valid JSON

Parameters:
Name Type Description
string
Returns:
Type
boolean

(private) _request(endpoint, data, handler) → {*|boolean|void}

Send a request to the client managed by this class, and wait for a valid response.

Parameters:
Name Type Description
endpoint string

client endpoint to be called

data object

body data of the request

handler function

callback function for a valid response

Returns:
Type
* | boolean | void

on_close(callbackopt) → {Promise.<void>}

Register a callback or promise to execute on close of the connection.

Parameters:
Name Type Attributes Default Description
callback function <optional>
false
Returns:
Type
Promise.<void>

on_open(callbackopt) → {Promise.<void>}

Register a callback or promise to execute on open of the connection.

Parameters:
Name Type Attributes Default Description
callback function <optional>
false
Returns:
Type
Promise.<void>

process(t) → {module:flitter-socket/Transaction~Transaction}

Process a transaction by calling its endpoint method. If the transaction is resolved, delete it.

Parameters:
Name Type Description
t module:flitter-socket/Transaction~Transaction
Returns:

the processed transaction

Type
module:flitter-socket/Transaction~Transaction

transaction(topt) → {null|module:flitter-socket/Transaction~Transaction}

Kind of a catch-all for registering transactions. If no transaction is provided, gracefully return. If a transaction object is provided, register it in this.active_transactions by its ID. Otherwise if it is a string, return the active transaction with that ID.

Parameters:
Name Type Attributes Default Description
t module:flitter-socket/Transaction~Transaction | string <optional>
false

validate_incoming_message(msg) → {object|null}

Validates that a transaction is a valid flitter-sockets spec transaction. If it is, return the transaction's data. Otherwise, send a module:flitter-socket/ClientErrorTransaction~ClientErrorTransaction.

Parameters:
Name Type Description
msg string

the incoming client message

Returns:
Type
object | null