Boost Your App's Responsiveness with These 17 Subscription Methods

Boost Your App's Responsiveness with These 17 Subscription Methods

Table of Contents

  1. Introduction
  2. Generating the Signature
    1. Submitting the Transaction Signature
    2. Subscribing to Own Account
      • Subscribing to Family Seed
      • Subscribing to Mnemonic
  3. Listening to Transaction Events
    1. Extracting Useful Fields
    2. Matching Transaction Hash with ID
    3. Checking Delivered Amount
  4. Closing the WebSocket Connection
  5. Implementing Ledger Events
    1. Getting Current Ledger Index
    2. Checking Last Ledger Sequence
    3. Displaying Transaction Status
    4. Closing the Connection

Implementing Subscription Methods and Event Handling in TypeScript

In this tutorial, we will learn how to implement subscription methods and event handling in our main script. We will focus on generating the signature, submitting the transaction signature, subscribing to our own account, listening to transaction events, and implementing ledger events.

Generating the Signature

First, we need to generate the signature for our transaction. This signature serves as a unique identifier for the transaction. We can generate the signature using the provided code. The generated signature should match the transaction hash output from the server.

Submitting the Transaction Signature

Once we have generated the signature, we need to submit it to the Ripple network. In our previous video, we used the sum command for this purpose. However, in this script, we will submit the transaction signature directly to Ripple. This code will invoke or trigger the transaction event, which we will subscribe to later.

Subscribing to Own Account

Before subscribing to the transaction event, it is preferable to subscribe to our own account. This ensures that we receive event notifications for our own actions. We can subscribe to either our own account or the destination account. However, subscribing to our own account provides more assurance about our own actions.

Listening to Transaction Events

Once the payment is processed, the transaction event will be triggered. We need to listen to this event and extract useful information from it. In this video, we will focus on extracting the transaction hash, the delivered amount, and the transaction status. We will match the generated ID with the transaction hash to ensure the validity of the transaction.

Matching Transaction Hash with ID

When the transaction event is triggered, we can compare the transaction hash returned by Ripple with the generated ID. If the two match, we know which request triggered the response. We can then proceed to extract the necessary details from the event.

Checking Delivered Amount

Inside the transaction event, we can also check if the delivered amount is in XRP or IOUs. If the delivered amount is a STRING, it indicates an XRP transaction. We can convert the delivered amount from drops to XRP and display it. If the delivered amount is not a string and not undefined, it may be an IOU transaction. In this case, we can print out the delivered amount and the currency.

Closing the WebSocket Connection

After handling the transaction event, it is important to responsibly close the bidirectional WebSocket connection. This ensures proper cleanup and frees up system resources. We can use the close method to close the connection.

Implementing Ledger Events

Apart from transaction events, we can also listen to ledger events. These events provide information about the current ledger index. We can make use of this information to enhance the predictability of our script.

Getting Current Ledger Index

The ledger event provides the current ledger index. We can extract this information and use it for our benefit. This way, we can ensure that our transaction is included before the specified ledger sequence.

Checking Last Ledger Sequence

Our transaction has an optional field called the last ledger sequence. This field specifies the ledger sequence number before which our transaction must be included. By calculating the current ledger index and adding a buffer of a few seconds, we can ensure that our transaction has enough time to be included.

Displaying Transaction Status

If the current ledger index is already greater than the ledger index Mentioned in the payload, it means our transaction was not included in the ledger. In this case, we can display a message indicating that the transaction was not seen in the last ledger. This allows users to retry sending the payment or take appropriate action.

Closing the Connection

Finally, we need to close the WebSocket connection after handling the ledger event. This ensures proper cleanup and concludes the script execution.

Overall, this tutorial provides a step-by-step guide to implementing subscription methods and event handling in TypeScript. By following these instructions, You can enhance the functionality of your scripts and Create a more responsive application.

Pros:

  • Enhances script functionality.
  • Provides real-time updates on transaction status.
  • Enables better user experience.

Cons:

  • Requires understanding of subscription methods and event handling.
  • Complexity increases with the addition of ledger events.

Most people like

Find AI tools in Toolify

Join TOOLIFY to find the ai tools

Get started

Sign Up
App rating
4.9
AI Tools
20k+
Trusted Users
5000+
No complicated
No difficulty
Free forever
Browse More Content