If I want to react to an account changing, I can use the onAccountChange
function in web3.js, like described here for example. I'm building a client in rust though, does anchor_client
or solana_sdk
have any equivalent to this (which isn't just me manually querying the account over and over)?
-
1Found the place in the rust sdk: docs.rs/solana-client/latest/solana_client/nonblocking/… . Will answer my question once I figure out how to use it correctly unless someone else does so first :).– McBainCommented Jun 11 at 14:40
Add a comment
|
1 Answer
Figured it out:
let listener_client = PubsubClient::new(<WSS URL>).await.unwrap();
// Can add a config instead of None here, but None for simplicity
let (stream, shutdown_handle) = listener_client.account_subscribe(&mempool, None)
.await.unwrap();
while let Some(data) = stream.next() {
println!("Received an update! {}",data);
}
// Stream closed
...
// Somewhere else if we want to close the websocket
shutdown_handle();
Also check out the new mango client for a more involved example, this was very helpful for me.