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:

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:
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:
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:
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:
Returns:
a promise for each connection in the order of getConnections().
Type
Array.<Promise>

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

Check all managed connections.
Source:
Returns:
this connection manager for chaining
Type
Promise.<MoneroConnectionManager>

clear() → {MoneroConnectonManager}

Remove all connections.
Source:
Returns:
this connection manager for chaining
Type
MoneroConnectonManager

disconnect() → {MoneroConnectionManager}

Disconnect from the current connection.
Source:
Returns:
this connection manager for chaining
Type
MoneroConnectionManager

getAutoSwitch() → {boolean}

Get if auto switch is enabled or disabled.
Source:
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:
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:
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:
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:
Returns:
the list of sorted connections
Type
Array.<MoneroRpcConnection>

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

Collect connectable peers of the managed connections.
Source:
Returns:
connectable peers
Type
Array.<MoneroRpcConnection>

getTimeout() → {int}

Get the request timeout.
Source:
Returns:
the request timeout before a connection is considered offline
Type
int

isConnected() → {boolean}

Indicates if the connection manager is connected to a node.
Source:
Returns:
true if the current connection is set, online, and not unauthenticated. false otherwise
Type
boolean

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

Remove a connection.
Parameters:
Name Type Description
uri string of the the connection to remove
Source:
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:
Returns:
this connection manager for chaining
Type
MoneroConnectionManager

removeListeners() → {MoneroConnectionManager}

Remove all listeners.
Source:
Returns:
this connection manager for chaining
Type
MoneroConnectionManager

reset() → {MoneroConnectonManager}

Reset to default state.
Source:
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:
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 update credentials of existing connection with 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:
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:
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:
Returns:
this connection manager for chaining (after first checking the connection)
Type
Promise.<MoneroConnectionManager>

stopCheckingConnection() → {MoneroConnectionManager}

Stop checking the connection status periodically.
Source:
Returns:
this connection manager for chaining
Type
MoneroConnectionManager

Documentation generated by JSDoc 3.6.11 on Sun Aug 28 2022 17:32:38 GMT-0400 (Eastern Daylight Time)