# Class: MoneroConnectionManager

## MoneroConnectionManager(proxyToWorker)

Manages a collection of prioritized connections to daemon or wallet RPC endpoints.

Example usage:

`// imports
const monerojs = require("monero-javascript");
const MoneroRpcConnection = monerojs.MoneroRpcConnection;
const MoneroConnectionManager = monerojs.MoneroConnectionManager;
const MoneroConnectionManagerListener = monerojs.MoneroConnectionManagerListener;
// create connection manager
let connectionManager = new MoneroConnectionManager();
// add managed connections with priorities
connectionManager.addConnection(new MoneroRpcConnection("http://localhost:38081").setPriority(1)); // use localhost as first priority
connectionManager.addConnection(new MoneroRpcConnection("http://example.com")); // default priority is prioritized last
// set current connection
connectionManager.setConnection(new MoneroRpcConnection("http://foo.bar", "admin", "password")); // connection is added if new
// check connection status
await connectionManager.checkConnection();
console.log("Connection manager is connected: " + connectionManager.isConnected());
console.log("Connection is online: " + connectionManager.getConnection().isOnline());
console.log("Connection is authenticated: " + connectionManager.getConnection().isAuthenticated());
// receive notifications of any changes to current connection
connectionManager.addListener(new class extends MoneroConnectionManagerListener {
onConnectionChanged(connection) {
console.log("Connection changed to: " + connection);
}
});
// start polling for best connection every 10 seconds and automatically switch
connectionManager.startPolling(10000);
// automatically switch to best available connection if disconnected
connectionManager.setAutoSwitch(true);
// get best available connection in order of priority then response time
let bestConnection = await connectionManager.getBestAvailableConnection();
// check status of all connections
await connectionManager.checkConnections();
// get connections in order of current connection, online status from last check, priority, and name
let connections = connectionManager.getConnections();
// clear connection manager
connectionManager.clear(); ```

## Constructor

#### new MoneroConnectionManager(proxyToWorker)

Construct a connection manager.

##### Parameters:

Name | Type | Description
--- | --- | ---
`proxyToWorker` | boolean | configure all connections to proxy to worker (default true)

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 61

### Members

#### PollType

Specify behavior when polling. One of PRIORITIZED (poll connections in order of priority until connected; default), CURRENT (poll current connection), or ALL (poll all connections).

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 68

### Methods

#### (async) addConnection(uriOrConnection) → {Promise.<MoneroConnectionManager>}

Add a connection. The connection may have an elevated priority for this manager to use.

##### Parameters:

Name | Type | Description
--- | --- | ---
`uriOrConnection` | string | MoneroRpcConnection | uri or connection to add

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 135

##### Returns:

this connection manager for chaining

Type
  Promise.<MoneroConnectionManager>

#### addListener(listener) → {MoneroConnectionManager}

Add a listener to receive notifications when the connection changes.

##### Parameters:

Name | Type | Description
--- | --- | ---
`listener` | MoneroConnectionManagerListener | the listener to add

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 94

##### Returns:

this connection manager for chaining

Type
  MoneroConnectionManager

#### (async) checkConnection() → {Promise.<MoneroConnectionManager>}

Check the current connection. If disconnected and auto switch enabled, switches to best available connection.

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 309

##### Returns:

this connection manager for chaining

Type
  Promise.<MoneroConnectionManager>

#### checkConnectionPromises() → {Array.<Promise>}

Check all managed connections, returning a promise for each connection check. Does not auto switch if disconnected.

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 342

##### Returns:

a promise for each connection in the order of getConnections().

Type
  Array.<Promise>

#### (async) checkConnections() → {Promise.<MoneroConnectionManager>}

Check all managed connections.

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 332

##### Returns:

this connection manager for chaining

Type
  Promise.<MoneroConnectionManager>

#### (async) clear() → {Promise.<MoneroConnectonManager>}

Remove all connections.

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 457

##### Returns:

this connection manager for chaining

Type
  Promise.<MoneroConnectonManager>

#### (async) disconnect() → {Promise.<MoneroConnectionManager>}

Disconnect from the current connection.

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 447

##### Returns:

this connection manager for chaining

Type
  Promise.<MoneroConnectionManager>

#### getAutoSwitch() → {boolean}

Get if auto switch is enabled or disabled.

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 409

##### Returns:

true if auto switch enabled, false otherwise

Type
  boolean

#### (async) getBestAvailableConnection(excludedConnections) → {Promise.<MoneroRpcConnection>}

Get the best available connection in order of priority then response time.

##### Parameters:

Name | Type | Description
--- | --- | ---
`excludedConnections` | Array.<MoneroRpcConnection> | connections to be excluded from consideration (optional)

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 365

##### Returns:

the best available connection in order of priority then response time, undefined if no connections available

Type
  Promise.<MoneroRpcConnection>

#### getConnection() → {MoneroRpcConnection}

Get the current connection.

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 211

##### Returns:

the current connection or undefined if no connection set

Type
  MoneroRpcConnection

#### getConnectionByUri(uri) → {MoneroRpcConnection}

Get a connection by URI.

##### Parameters:

Name | Type | Description
--- | --- | ---
`uri` | string | is the URI of the connection to get

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 231

##### Returns:

the connection with the URI or undefined if no connection with the URI exists

Type
  MoneroRpcConnection

#### getConnections() → {Array.<MoneroRpcConnection>}

Get all connections in order of current connection (if applicable), online status, priority, and name.

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 241

##### Returns:

the list of sorted connections

Type
  Array.<MoneroRpcConnection>

#### getListeners() → {Array.<MoneroConnectionManagerListener>}

Get all listeners.

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 125

##### Returns:

all listeners

Type
  Array.<MoneroConnectionManagerListener>

#### (async) getPeerConnections() → {Promise.<Array.<MoneroRpcConnection>>}

Collect connectable peers of the managed connections.

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 438

##### Returns:

connectable peers

Type
  Promise.<Array.<MoneroRpcConnection>>

#### getTimeout() → {int}

Get the request timeout.

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 429

##### Returns:

the request timeout before a connection is considered offline

Type
  int

#### hasConnection(uri) → {boolean}

Indicates if this manager has a connection with the given URI.

##### Parameters:

Name | Type | Description
--- | --- | ---
`uri` | string | URI of the connection to check

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 221

##### Returns:

true if this manager has a connection with the given URI, false otherwise

Type
  boolean

#### isConnected() → {boolean|undefined}

Indicates if the connection manager is connected to a node.

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 252

##### Returns:

true if the current connection is set, online, and not unauthenticated, undefined if unknown, false otherwise

Type
  boolean | undefined

#### (async) removeConnection(uri) → {Promise.<MoneroConnectionManager>}

Remove a connection.

##### Parameters:

Name | Type | Description
--- | --- | ---
`uri` | string | of the the connection to remove

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 151

##### Returns:

this connection manager for chaining

Type
  Promise.<MoneroConnectionManager>

#### removeListener(listener) → {MoneroConnectionManager}

Remove a listener.

##### Parameters:

Name | Type | Description
--- | --- | ---
`listener` | MoneroConnectionManagerListener | the listener to remove

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 105

##### Returns:

this connection manager for chaining

Type
  MoneroConnectionManager

#### removeListeners() → {MoneroConnectionManager}

Remove all listeners.

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 115

##### Returns:

this connection manager for chaining

Type
  MoneroConnectionManager

#### reset() → {MoneroConnectonManager}

Reset to default state.

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 471

##### Returns:

this connection manager for chaining

Type
  MoneroConnectonManager

#### setAutoSwitch(autoSwitch) → {MoneroConnectionManager}

Automatically switch to the best available connection as connections are polled, based on priority, response time, and consistency.

##### Parameters:

Name | Type | Description
--- | --- | ---
`autoSwitch` | boolean | specifies if the connection should auto switch to a better connection

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 399

##### Returns:

this connection manager for chaining

Type
  MoneroConnectionManager

#### (async) setConnection(uriOrConnection) → {Promise.<MoneroConnectionManager>}

Set the current connection. Provide a URI to select an existing connection without updating its credentials. Provide a MoneroRpcConnection to add new connection or replace existing connection with the same URI. Notify if current connection changes. Does not check the connection.

##### Parameters:

Name | Type | Description
--- | --- | ---
`uriOrConnection` | string | MoneroRpcConnection | is the uri of the connection or the connection to make current (default undefined for no current connection)

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 173

##### Returns:

this connection manager for chaining

Type
  Promise.<MoneroConnectionManager>

#### setTimeout(timeoutMs) → {MoneroConnectionManager}

Set the maximum request time before its connection is considered offline.

##### Parameters:

Name | Type | Description
--- | --- | ---
`timeoutMs` | int | the timeout before the connection is considered offline

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 419

##### Returns:

this connection manager for chaining

Type
  MoneroConnectionManager

#### startPolling(periodMs, autoSwitch, timeoutMs, pollType, excludedConnections) → {MoneroConnectionManager}

Start polling connections.

##### Parameters:

Name | Type | Description
--- | --- | ---
`periodMs` | number | poll period in milliseconds (default 20s)
`autoSwitch` | boolean | specifies to automatically switch to the best connection (default true unless changed)
`timeoutMs` | number | specifies the timeout to poll a single connection (default 5s unless changed)
`pollType` | number | one of PRIORITIZED (poll connections in order of priority until connected; default), CURRENT (poll current connection), or ALL (poll all connections)
`excludedConnections` | Array.<MoneroRpcConnection> | connections excluded from being polled

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 267

##### Returns:

this connection manager for chaining

Type
  MoneroConnectionManager

#### stopPolling() → {MoneroConnectionManager}

Stop polling connections.

Source:
  - src/main/js/common/MoneroConnectionManager.js, line 298

##### Returns:

this connection manager for chaining

Type
  MoneroConnectionManager