eth_subscribe - Mantle
Example code for the eth_subscribe JSON-RPC method. Complete guide on how to use eth_subscribe JSON-RPC in GetBlock Web3 documentation.
This method creates a subscription for specific events on the blockchain.
It requires a WebSocket connection. When an event occurs, the server pushes a notification to the client.
Parameters
subscriptionType
string
Type of subscription ("newHeads", "logs", "newPendingTransactions")
filterObject
object
(optional) Filter options for logs subscription
Request (WebSocket)
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://go.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));
};Subscription Types
newHeads
Fires when new block is added to chain
logs
Fires when logs matching filter are included
newPendingTransactions
Fires when new pending tx is added to pool
Response
Notification Example (newHeads)
Response Parameters
result
string
Subscription ID for managing the subscription
Use Case
The eth_subscribe method is essential for:
Real-time block monitoring
Event streaming
Live transaction tracking
DApp real-time updates
Notification systems
Error Handling
403
Forbidden
Missing or invalid ACCESS-TOKEN
-32000
Subscription not supported
HTTP used instead of WebSocket
Web3 Integration
Last updated
Was this helpful?