Class MoneroConnectionManager

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

Example usage:

// imports
import { MoneroRpcConnection, MoneroConnectionManager, MoneroConnectionManagerListener } from "monero-ts";

// create connection manager
let connectionManager = new MoneroConnectionManager();

// add managed connections with priorities
await connectionManager.addConnection({uri: "http://localhost:38081", priority: 1}); // use localhost as first priority
await connectionManager.addConnection({uri: "http://example.com"}); // default priority is prioritized last

// set current connection
await connectionManager.setConnection({uri: "http://foo.bar", username: "admin", password: "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().getIsOnline());
console.log("Connection is authenticated: " + connectionManager.getConnection().getIsAuthenticated());

// receive notifications of any changes to current connection
connectionManager.addListener(new class extends MoneroConnectionManagerListener {
   async 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();

Hierarchy

  • MoneroConnectionManager

Constructors

  • Construct a connection manager.

    Parameters

    • Optional proxyToWorker: boolean = true

      configure all connections to proxy to worker (default true)

    Returns MoneroConnectionManager

Properties

autoSwitch: any
connections: any
currentConnection: any
listeners: any
poller: any
proxyToWorker: any
responseTimes: any
timeoutMs: any
DEFAULT_AUTO_SWITCH: boolean = true
DEFAULT_POLL_PERIOD: number = 20000
DEFAULT_TIMEOUT: number = 5000
MIN_BETTER_RESPONSES: number = 3
PollType: {
    ALL: number;
    CURRENT: number;
    PRIORITIZED: number;
} = ...

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).

Type declaration

  • ALL: number
  • CURRENT: number
  • PRIORITIZED: number

Methods

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

    Returns Promise<MoneroConnectionManager>

    this connection manager for chaining

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

    Returns Promise<void>[]

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

  • Parameters

    • connections: any
    • Optional excludedConnections: any

    Returns Promise<boolean>

  • Parameters

    • excludedConnections: any

    Returns Promise<void>

  • Parameters

    • c1: any
    • c2: any

    Returns any

  • Parameters

    • p1: any
    • p2: any

    Returns number

  • Get if auto switch is enabled or disabled.

    Returns boolean

    true if auto switch enabled, false otherwise

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

    Parameters

    • Optional excludedConnections: MoneroRpcConnection[]

      connections to be excluded from consideration (optional)

    Returns Promise<MoneroRpcConnection>

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

  • Get the best connection from the given responses.

    Parameters

    • responses: any

      connection responses to update from

    Returns Promise<MoneroRpcConnection>

    the best response among the given responses or undefined if none are best

  • Get a connection by URI.

    Parameters

    • uri: string

      is the URI of the connection to get

    Returns MoneroRpcConnection

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

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

    Returns MoneroRpcConnection[]

    the list of sorted connections

  • Returns any[]

  • Get the request timeout.

    Returns number

    the request timeout before a connection is considered offline

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

    Parameters

    • uri: string

      URI of the connection to check

    Returns boolean

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

  • Indicates if the connection manager is connected to a node.

    Returns boolean

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

  • Parameters

    • connection: any

    Returns Promise<any[]>

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

    Parameters

    • autoSwitch: boolean

      specifies if the connection should auto switch to a better connection

    Returns MoneroConnectionManager

    this connection manager for chaining

  • 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

    • Optional uriOrConnection: string | Partial<MoneroRpcConnection>

      is the uri of the connection or the connection to make current (default undefined for no current connection)

    Returns Promise<MoneroConnectionManager>

    this connection manager for chaining

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

    Parameters

    • timeoutMs: number

      the timeout before the connection is considered offline

    Returns MoneroConnectionManager

    this connection manager for chaining

  • Start polling connections.

    Parameters

    • Optional periodMs: number

      poll period in milliseconds (default 20s)

    • Optional autoSwitch: boolean

      specifies to automatically switch to the best connection (default true unless changed)

    • Optional timeoutMs: number

      specifies the timeout to poll a single connection (default 5s unless changed)

    • Optional pollType: number

      one of PRIORITIZED (poll connections in order of priority until connected; default), CURRENT (poll current connection), or ALL (poll all connections)

    • Optional excludedConnections: MoneroRpcConnection[]

      connections excluded from being polled

    Returns MoneroConnectionManager

    this connection manager for chaining

Generated using TypeDoc