The transactionSubscribe websocket method enables real-time transaction events. To use it, provide a TransactionSubscribeFilter and optionally include TransactionSubscribeOptions for further customization.
vote: A boolean flag to include/exclude vote-related transactions.failed: A boolean flag to include/exclude transactions that failed.signature: Filters updates to a specific transaction based on its signature.accountInclude: A list of accounts for which you want to receive transaction updates. This means that only one of the accounts must be included in the transaction updates (e.g., Account 1 OR Account 2).accountExclude: A list of accounts you want to exclude from transaction updates.accountRequired: Transactions must involve these specified accounts to be included in updates. This means that all of the accounts must be included in the transaction update (e.g., Account 1 AND Account 2).
You can include up to 50,000 addresses in the accountsInclude, accountExclude and accountRequired arrays.
commitment: Specifies the commitment level for fetching data, dictating at what stage of the transaction lifecycle updates are sent. The possible values are processed, confirmed and finalizedencoding: Sets the encoding format of the returned transaction data. The possible values are base58, base64 and jsonParsedtransactionDetails : Determines the level of detail for the returned transaction data. The possible values are full, signatures, accounts and noneshowRewards: A boolean flag indicating if reward data should be included in the transaction updates.maxSupportedTransactionVersion: Specifies the highest version of transactions you want to receive updates. To get Versioned Transactions, set the value to 1.
maxSupportedTransactionVersion is required to return the accounts and full-level details of a given transaction (i.e., transactionDetails: "accounts" | "full").
In this example, we are subscribing to transactions that contain the Raydium account 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8. Whenever a transaction occurs that contains 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8in the accountKeys of the transaction, we will receive a websocket notification.Based on the subscription options, the transaction notification will be sent at the processed commitment level,jsonParsedencoding, full transaction details, and will show rewards.
Report incorrect code
Copy
Ask AI
const WebSocket = require('ws');// Create a WebSocket connectionconst ws = new WebSocket('wss://atlas-mainnet.helius-rpc.com/?api-key=<API_KEY>');// Function to send a request to the WebSocket serverfunction sendRequest(ws) { const request = { jsonrpc: "2.0", id: 420, method: "transactionSubscribe", params: [ { accountInclude: ["675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8"] }, { commitment: "processed", encoding: "jsonParsed", transactionDetails: "full", showRewards: true, maxSupportedTransactionVersion: 0 } ] }; ws.send(JSON.stringify(request));}// Function to send a ping to the WebSocket serverfunction startPing(ws) { setInterval(() => { if (ws.readyState === WebSocket.OPEN) { ws.ping(); console.log('Ping sent'); } }, 30000); // Ping every 30 seconds}// Define WebSocket event handlersws.on('open', function open() { console.log('WebSocket is open'); sendRequest(ws); // Send a request once the WebSocket is open startPing(ws); // Start sending pings});ws.on('message', function incoming(data) { const messageStr = data.toString('utf8'); try { const messageObj = JSON.parse(messageStr); console.log('Received:', messageObj); } catch (e) { console.error('Failed to parse JSON:', e); }});ws.on('error', function error(err) { console.error('WebSocket error:', err);});ws.on('close', function close() { console.log('WebSocket is closed');});