# 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);
}
});
// check connection status every 10 seconds
await connectionManager.startCheckingConnection(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

### Methods

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

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

##### Parameters:

Name | Type | Description
--- | --- | ---
`connection` | MoneroRpcConnection | the connection to add

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

##### 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 81

##### 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 262

##### 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 311

##### 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 282

##### Returns:

this connection manager for chaining

Type
  Promise.<MoneroConnectionManager>

#### clear() → {MoneroConnectonManager}

Remove all connections.

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

##### Returns:

this connection manager for chaining

Type
  MoneroConnectonManager

#### disconnect() → {MoneroConnectionManager}

Disconnect from the current connection.

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

##### Returns:

this connection manager for chaining

Type
  MoneroConnectionManager

#### getAutoSwitch() → {boolean}

Get if auto switch is enabled or disabled.

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

##### 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 186

##### 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 154

##### 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 164

##### 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 174

##### Returns:

the list of sorted connections

Type
  Array.<MoneroRpcConnection>

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

Get all listeners.

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

##### Returns:

all listeners

Type
  Array.<MoneroConnectionManagerListener>

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

Collect connectable peers of the managed connections.

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

##### Returns:

connectable peers

Type
  Array.<MoneroRpcConnection>

#### getTimeout() → {int}

Get the request timeout.

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

##### Returns:

the request timeout before a connection is considered offline

Type
  int

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

Indicates if the connection manager is connected to a node.

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

##### 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 128

##### 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 92

##### Returns:

this connection manager for chaining

Type
  MoneroConnectionManager

#### removeListeners() → {MoneroConnectionManager}

Remove all listeners.

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

##### Returns:

this connection manager for chaining

Type
  MoneroConnectionManager

#### reset() → {MoneroConnectonManager}

Reset to default state.

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

##### Returns:

this connection manager for chaining

Type
  MoneroConnectonManager

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

Automatically switch to best available connection if current connection is disconnected after being checked.

##### Parameters:

Name | Type | Description
--- | --- | ---
`autoSwitch` | boolean | specifies if the connection should switch on disconnect

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

##### Returns:

this connection manager for chaining

Type
  MoneroConnectionManager

#### setConnection(uriOrConnection) → {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 224

##### Returns:

this connection manager for chaining

Type
  MoneroConnectionManager

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

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

##### Parameters:

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

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

##### Returns:

this connection manager for chaining

Type
  MoneroConnectionManager

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

Check the connection and start checking the connection periodically.

##### Parameters:

Name | Type | Description
--- | --- | ---
`periodMs` | number | is the time between checks in milliseconds (default 10000 or 10 seconds)

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

##### Returns:

this connection manager for chaining (after first checking the connection)

Type
  Promise.<MoneroConnectionManager>

#### stopCheckingConnection() → {MoneroConnectionManager}

Stop checking the connection status periodically.

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

##### Returns:

this connection manager for chaining

Type
  MoneroConnectionManager