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:

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:

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:
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>

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

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

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

Disconnect from the current connection.
Source:
Returns:
this connection manager for chaining
Type
Promise.<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>

getListeners() → {Array.<MoneroConnectionManagerListener>}

Get all listeners.
Source:
Returns:
all listeners
Type
Array.<MoneroConnectionManagerListener>

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

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

getTimeout() → {int}

Get the request timeout.
Source:
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:
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:
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:
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 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:
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:
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:
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:
Returns:
this connection manager for chaining
Type
MoneroConnectionManager

stopPolling() → {MoneroConnectionManager}

Stop polling connections.
Source:
Returns:
this connection manager for chaining
Type
MoneroConnectionManager