Expand description
A client for subscribing to messages from the RPC server.
The PubsubClient
implements Solana WebSocket event
subscriptions.
This is a blocking API. For a non-blocking API use the asynchronous client
in crate::nonblocking::pubsub_client
.
PubsubClient
contains static methods to subscribe to events, like
PubsubClient::account_subscribe
. These methods each return their own
subscription type, like AccountSubscription
, that are typedefs of
tuples, the first element being a handle to the subscription, like
AccountSubscription
, the second a Receiver
of RpcResponse
of
whichever type is appropriate for the subscription. The subscription handle
is a typedef of PubsubClientSubscription
, and it must remain live for
the receiver to continue receiving messages.
Because this is a blocking API, with blocking receivers, a reasonable pattern for using this API is to move each event receiver to its own thread to block on messages, while holding all subscription handles on a single primary thread.
While PubsubClientSubscription
contains methods for shutting down,
PubsubClientSubscription::send_unsubscribe
, and
PubsubClientSubscription::shutdown
, because its internal receivers block
on events from the server, these subscriptions cannot actually be shutdown
reliably. For a non-blocking, cancelable API, use the asynchronous client
in crate::nonblocking::pubsub_client
.
By default the block_subscribe
and vote_subscribe
events are
disabled on RPC nodes. They can be enabled by passing
--rpc-pubsub-enable-block-subscription
and
--rpc-pubsub-enable-vote-subscription
to agave-validator
. When these
methods are disabled, the RPC server will return a “Method not found” error
message.
§Examples
This example subscribes to account events and then loops forever receiving them.
use anyhow::Result;
use solana_sdk::commitment_config::CommitmentConfig;
use solana_pubsub_client::pubsub_client::PubsubClient;
use solana_rpc_client_api::config::RpcAccountInfoConfig;
use solana_sdk::pubkey::Pubkey;
use std::thread;
fn get_account_updates(account_pubkey: Pubkey) -> Result<()> {
let url = "wss://api.devnet.solana.com/";
let (mut account_subscription_client, account_subscription_receiver) =
PubsubClient::account_subscribe(
url,
&account_pubkey,
Some(RpcAccountInfoConfig {
encoding: None,
data_slice: None,
commitment: Some(CommitmentConfig::confirmed()),
min_context_slot: None,
}),
)?;
loop {
match account_subscription_receiver.recv() {
Ok(response) => {
println!("account subscription response: {:?}", response);
}
Err(e) => {
println!("account subscription error: {:?}", e);
break;
}
}
}
Ok(())
}
Re-exports§
pub use crate::nonblocking::pubsub_client::PubsubClientError;
Structs§
- A client for subscribing to messages from the RPC server.
- A subscription.