6

For Example there is a change in a account in a program how do i listen to that changes to do something. I tried using webhooks but they listen to transaction's and the account involved in that tx does not gets updated as fast as the webhook get's triggered

1 Answer 1

4

There is accountSubscribe websocket method to subscribe for changes in a specific account.You can use the @solana/web3.js library which hides the implementation details of the websocket method.

Here is a snippet on how you can achieve this:

import { Connection } from "@solana/web3.js"

// Create a connection
const connection = new Connection(<URL>);

// Subscribe to account changes
const id = connection.onAccountChange(<PUBKEY>, (accountInfo) => {
  // Handle account change...
});

// Don't forget to unsubscribe when you no longer use it
connection.removeAccountChangeListener(id);
1
  • "Don't forget to unsubscribe when you no longer use it" is this necissary when the process closes? My unsubscribe requests get a "unsubscribe request ignored" warning if I attempt to tidy (i.e. end subscriptions) upon process close
    – 1owk3y
    Commented Sep 9, 2023 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.