Estimated reading time:

15 mins

Difficulty:

Beginner

Key takeaway:

Learn how subscriptions on the XRPL allow real-time updates without repeatedly querying the ledger, how the nice-XRPL library simplifies building reactive web apps, and how to use the useBalance() hook to track account balances automatically.

Lesson 3

Requests and subscriptions

How subscriptions keep you updated on the XRPL

Now that we know a little about getting information from the ledger using requests, lets look into the concept of a subscription.

You can think of a subscription just like a request. It doesn’t sign a transaction, and it’s purely meant to query the ledger for information. But, instead of sending a ping to the ledger each time you want to retrieve information, a subscription lets you stay connected to the ledger and it sends you an update any time the ledger changes. You can create multiple simultaneous subscriptions to keep track of multiple accounts’ activity at the same time.

A quick intro to the nice-XRPL library

To build this we’ll use the nice-xrpl React library which has some simple hooks into xrpl.js. xrpl.js is the main library for interacting with the ledger using Javascript.

Nice-XRPL is a React.js powered library which extends xrpl.js and focuses on building wallet-based dApps and web apps. Its core strength is in abstracting some of the XRPL.js functionality to make building a reactive UI quickly and intuitively. One of React’s big advantages is that it focuses on reactive components, which automatically update when server data (or ledger data) changes without requiring you to refresh your browser.

Get an account’s balance with useBalance()

Remember, you don’t have to own an account to query information about it. You can use the nice-XRPL library to perform both signed transactions and requests. The library distinguishes actions on an ‘account’ as requests and actions on a ‘wallet’ as signed transactions. Since you need a private key to perform a signed transaction, we presume you own it, and we use the ‘wallet’ name.

One of the easiest ways to get data from the XRP Ledger is by using the useBalance() hook. By passing in an account string, useBalance() will return the balance. This hook is reactive, so if you call it in your app, it will automatically update any time a balance changes on that account.

In the next lesson we’ll demonstrate a simple application in a code sandbox that you can explore and edit for yourself.

END OF LESSON

Now that you've learned a bit about subscriptions on the XRP Ledger, and the Nice-XRPL library, test yourself with a quiz!

What is the main difference between a request and a subscription on the XRP Ledger?
What does the nice-XRPL library focus on?
Which React hook from the nice-XRPL library allows you to get an account's balance?
Which of the following is true about subscriptions on the XRPL?
Which of the following is true about both requests and subscriptions on the XRPL?