pub async fn subscribe_program_logs<F, Fut>(
rpc_endpoint: &str,
program_id: &str,
commitment: CommitmentConfig,
on_notification: F,
) -> Result<LogSubscriptionHandle, SubscriptionError>Expand description
Subscribe to program log notifications and invoke a callback for each one.
Spawns a background task that:
- Connects to the PubSub endpoint derived from
rpc_endpoint. - Subscribes to logs mentioning
program_id. - For each notification, spawns
on_notification(notification)as a Tokio task. - When
handle.stop.send(true)is called, drains remaining buffered notifications (up to 1s), waits for all spawned tasks, then returns.
Returns after the subscription is established. If setup fails, an error is returned before any background task is left running.
ยงExample
use std::sync::{Arc, Mutex};
use simulator_client::subscribe_program_logs;
use solana_commitment_config::CommitmentConfig;
let handle = subscribe_program_logs(
"https://api.mainnet-beta.solana.com",
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
CommitmentConfig::confirmed(),
|notification| async move {
println!("sig: {}", notification.value.signature);
},
)
.await?;
// ... do other work ...
handle.stop.send(true).ok();
handle.join_handle.await.ok();