1

Is there a WebSocket version of getProgramAccounts that allows me to fetch PDAs with supplied seeds?

1 Answer 1

2

If you know the seeds you can use connection.onAccountChange to listen to account changes via web socket. You can then subscribe to all the accounts you are interested in. If you use an ascending id as a seed for example then in the client you can easily find them all.

export const [globalLevel1GameDataAccount] = PublicKey.findProgramAddressSync(
  [Buffer.from("level1", "utf8")],
  programId
)

useEffect(() => {
    if (!globalLevel1GameDataAccount) return

    const subscriptionId = connection.onAccountChange(
      globalLevel1GameDataAccount,
      (accountInfo) => {
        const decoded = program.coder.accounts.decode(
          "gameDataAccount",
          accountInfo.data
        )
        console.log("New player position via socket", decoded.playerPosition)
        setGameDataAccount(decoded)
      }
    )

    return () => {
      connection.removeAccountChangeListener(subscriptionId)
    }
  }, [connection, globalLevel1GameDataAccount, program])

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.