The logsSubscribe method opens a stream that emits a notification any time a transaction is committed and its logs match the supplied filter. Pair it with logsUnsubscribe to stop receiving notifications.
-
filter: filter criteria for log subscriptions. Accepts one of:"all"- subscribe to all transactions except simple vote transactions."allWithVotes"- subscribe to all transactions including simple vote transactions.- An object:
{ "mentions": ["<pubkey>"] }- subscribe to transactions that mention exactly one of the provided base-58 encoded pubkeys.
-
config(optional):object- Configuration object containing:commitment:string- The commitment level. One ofprocessed,confirmed,finalized. Defaults tofinalized.
// initiate websocket stream first
wscat -c wss://solana-mainnet.g.alchemy.com/v2/<-- ALCHEMY APP API KEY -->
// then call subscription
{"jsonrpc":"2.0","id":1,"method":"logsSubscribe","params":[{"mentions":["11111111111111111111111111111111"]},{"commitment":"finalized"}]}// subscribe response
{"jsonrpc":"2.0","result":24040,"id":1}
// notification
{
"jsonrpc": "2.0",
"method": "logsNotification",
"params": {
"result": {
"context": { "slot": 5208469 },
"value": {
"signature": "5h6xBEauJ3PK6SWCZ1PGjBvj8vDdWG3KpwATGy1ARAXFSDwt8GFXM7W5Ncn16wmqokgpiKRLuS83KUxyZyv2sUYv",
"err": null,
"logs": [
"Program 11111111111111111111111111111111 invoke [1]",
"Program 11111111111111111111111111111111 success"
]
}
},
"subscription": 24040
}
}Use logsUnsubscribe with the subscription id returned by logsSubscribe to cancel the stream.
subscription_id:number- The subscription id to cancel.
{"jsonrpc":"2.0","id":1,"method":"logsUnsubscribe","params":[subscription_id]}{"jsonrpc":"2.0","result":true,"id":1}When using @solana/web3.js, call connection.removeOnLogsListener(subscriptionId) instead of sending the raw JSON-RPC request.