eth_subscribe - AVAX
Example code for the eth_subscribe JSON-RPC method. Complete guide on how to use eth_subscribe JSON-RPC in GetBlock Web3 documentation.
Creates a real-time subscription over a WebSocket connection on the C-Chain. Available subscription types: newHeads (new block headers), logs (matching contract events), newPendingTransactions (mempool), and newAcceptedTransactions (Avalanche-specific — fires when transactions are accepted by consensus).
Parameters
subscriptionType
string
Yes
One of "newHeads", "logs", "newPendingTransactions", "newAcceptedTransactions"
filterObject
object
No
For "logs": filter with address and/or topics
Request Example
import axios from 'axios';
// Note: eth_subscribe requires WebSocket, but here's the message format
const data = JSON.stringify({
"jsonrpc": "2.0",
"method": "eth_subscribe",
"params": ["newHeads"],
"id": "getblock.io"
});
console.log('WebSocket message:', data);// WebSocket connection required
const ws = new WebSocket('wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/');
ws.onopen = () => {
ws.send(JSON.stringify({
"jsonrpc": "2.0",
"method": "eth_subscribe",
"params": ["newHeads"],
"id": "getblock.io"
}));
};
ws.onmessage = (event) => {
console.log('Received:', JSON.parse(event.data));
};Response Example
Response Parameters
result
string
Subscription ID, used to match incoming eth_subscription notifications and to call eth_unsubscribe
Use Cases
Real-time wallet activity notifications
Live DEX trade feeds for trading UIs
Streaming indexers reacting to new blocks instantly
Mempool surveillance (
newPendingTransactions) or consensus-accepted feeds (newAcceptedTransactions)
Error Handling
404
Not Found
Missing or invalid <ACCESS-TOKEN>
-32602
Invalid params
Request parameters are missing or malformed
429
Too Many Requests
Rate limit exceeded for your plan
-32600
Invalid Request
eth_subscribe was called over HTTP; use a WebSocket connection instead
SDK Integration
Last updated
Was this helpful?