WebsocketAccountSubscriber
TheWebsocketAccountSubscriber provides real-time account updates through WebSocket connections. It automatically handles connection management and provides event-based updates.
Constructor
pubsub- Shared reference to a PubsubClient instancepubkey- The account public key to subscribe tocommitment- Commitment level for the subscription (e.g., confirmed, finalized)
Methods
subscribe
sync is true, then uses event-based updates.
Parameters:
subscription_name- User-defined identifier for the subscriptionsync- If true, fetches account data on start to seed initial stateon_update- Callback function invoked on each account update
UnsubHandle- Handle to unsubscribe from the stream
AccountUpdate
The account update structure passed to callbacks:owner- The program that owns the accountlamports- Current lamport balancepubkey- The account’s public keydata- Account data bytesslot- Slot number when the update occurred
Features
- Automatic Reconnection: Automatically reconnects on WebSocket disconnections
- Slot Ordering: Only processes updates from later slots to prevent stale data
- Initial Sync: Optionally fetches initial account state before streaming updates
- Base64Zstd Encoding: Uses compressed encoding for efficient data transfer