Is there a WebSocket version of getProgramAccounts
that allows me to fetch PDAs with supplied seeds?
1 Answer
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])