

Read and listen
Ask the ledger questions, then subscribe for news. After a reconnect, fill the gap.
Key points
- account_info, account_lines and tx answer one question each.
- subscribe pushes each new transaction for the accounts you watch.
- If the link drops, subscribe again and fetch what you missed.
The lesson
A program can ask the ledger, or listen to it.
Asking is a question with one answer:
account_info: what is this account's balance?account_lines: which trust lines does it hold?tx: what happened to this transaction?
Ask for the validated ledger when the answer must be final.
Listening is a subscription. You say which accounts to watch. The server then pushes each new transaction as it happens, like a doorbell. No need to keep asking.
Connections . Wi-Fi blinks, servers restart. xrpl.js reconnects by itself, but the server has forgotten your subscription. So on every reconnect, do two things:
- Subscribe again.
- Backfill: ask for the transactions you missed while you were away.
Remember which transactions you already handled, and skip repeats.
Read:
const { result } = await client.request({ command: "account_info", account: address, ledger_index: "validated", }) console.log(result.account_data.Balance) // drops, as a string await client.request({ command: "account_lines", account: address }) await client.request({ command: "tx", transaction: hash })txreturnstx_json,meta,ledger_indexandvalidated. Long lists page withmarker.Listen, with reconnect and backfill:
let last = 0 const seen = new Set() function handle(hash, ledger) { if (seen.has(hash)) return seen.add(hash) last = Math.max(last, ledger) // credit, notify or store it here } client.on("transaction", (ev) => { if (ev.validated) handle(ev.hash, ev.ledger_index) }) client.on("connected", async () => { // first connect and every reconnect await client.request({ command: "subscribe", accounts: [address] }) if (!last) return const missed = await client.request({ command: "account_tx", account: address, ledger_index_min: last, }) for (const t of missed.result.transactions) handle(t.hash, t.ledger_index) }) await client.connect()The stream sends a message per transaction, with
validatedset. Act only onvalidated: true. Startingaccount_txat the last ledger seen may repeat one; theseenset skips it. Persistlastto survive a restart.Related: Reading metadata, Running a node.