Parameters
-
publicKey- The public key of the account to monitor, as a base-58 encoded string -
<commitment>- (optional) The level of commitment desired when querying state. Default is “finalized”. -
encoding- (optional) Data encoding for the account data:"base58"- Base-58 encoding (default)"base64"- Base-64 encoding"base64+zstd"- Compressed Base-64 encoding"jsonParsed"- JSON format (if program supports it)
Response
-
result- An integer subscription id (needed to unsubscribe) -
subscription- An object containing account information:lamports- Number of lamports assigned to the accountowner- Base-58 encoded public key of the program this account has been assigned todata- Account data, encoded according to specified encoding parameterexecutable- Boolean indicating if the account contains a programrentEpoch- The epoch at which this account will next owe rent
accountSubscribe code examples
This example subscribes to changes in a token account.
Note that subscriptions require a WebSocket connection and WebSocket cat for you to use this method in the console.Install WebSocket cat with:
npm install -g wscatUse case
TheaccountSubscribe method in Solana is essential for applications that need to monitor account state changes in real-time. Common use cases include:
- Token tracking: Monitor token account balances for changes to update user interfaces instantly
- Program monitoring: Track program-owned accounts to detect state changes in smart contracts
- Wallet integration: Keep wallet balances and account states synchronized with the blockchain
- DeFi applications: Monitor liquidity pools, lending positions, or other DeFi-related accounts
- Gaming: Track in-game asset accounts and player state changes in real-time