A client for one ElectrumX server.

It answers requests, hands notifications to listeners, keeps an idle connection open with a ping, and says when the connection is gone – it does not reconnect: which server to use next is the application's decision.

Example

const client = new ElectrumClient('wss://electrum.example:50004/');
await client.connect();
const chain = await verifyChain(client, networks.doichain);
if (!chain.ok) throw new Error(`this server follows another chain: ${chain.reason}`);
const stop = client.on('blockchain.headers.subscribe', ([header]) => console.log(header));
const tip = await client.request('blockchain.headers.subscribe');

Hierarchy

  • ElectrumClient

Constructors

Properties

Accessors

Methods

Constructors

Properties

onclose: null | ((event) => void) = null

Called when the connection drops without close(), so the caller can pick a new server.

Type declaration

    • (event): void
    • Parameters

      • event: unknown

      Returns void

url: string

Accessors

  • get connected(): boolean
  • True while the socket is open and requests can be sent.

    Returns boolean

Methods

  • Closes the connection. onclose is not called: this was on purpose.

    Returns void

  • Opens the connection. Resolves once the server accepted it.

    Returns Promise<void>

  • Listens for a notification, e.g. blockchain.headers.subscribe after subscribing to it with request.

    Parameters

    • method: string
    • listener: ((params) => void)
        • (params): void
        • Parameters

          • params: any[]

          Returns void

    Returns (() => void)

    a function that stops listening

      • (): void
      • Listens for a notification, e.g. blockchain.headers.subscribe after subscribing to it with request.

        Returns void

        a function that stops listening

  • Asks the server one question.

    Type Parameters

    • T = any

    Parameters

    • method: string

      an ElectrumX method, e.g. blockchain.transaction.get

    • params: unknown[] = []

      its parameters

    Returns Promise<T>

    whatever the server answers; a result of 0, false or null stays that value

    Throws

    Error ESOCKET without a connection, ETIMEDOUT <method> when the answer stays away, or the server's own error message