eth_subscribe - Mantle

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

ParameterTypeDescription
subscriptionTypestringType of subscription ("newHeads", "logs", "newPendingTransactions")
filterObjectobject(optional) Filter options for logs subscription

Request (WebSocket)

jsimport 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);
javascript// 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));
};
pythonimport websocket
import json

def on_message(ws, message):
    print(f"Received: {message}")

def on_open(ws):
    ws.send(json.dumps({
        "jsonrpc": "2.0",
        "method": "eth_subscribe",
        "params": ["newHeads"],
        "id": "getblock.io"
    }))

ws = websocket.WebSocketApp(
    "wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/",
    on_message=on_message,
    on_open=on_open
)
ws.run_forever()
rust// Note: eth_subscribe requires WebSocket connection
// Example message format for WebSocket client
let message = r#"{
    "jsonrpc": "2.0",
    "method": "eth_subscribe",
    "params": ["newHeads"],
    "id": "getblock.io"
}"#;

Subscription Types

TypeDescription
newHeadsFires when new block is added to chain
logsFires when logs matching filter are included
newPendingTransactionsFires when new pending tx is added to pool

Response

json{
    "jsonrpc": "2.0",
    "id": "getblock.io",
    "result": "0x9ce59a13059e417087c02d3236a0b1cc"
}

Notification Example (newHeads)

json{
    "jsonrpc": "2.0",
    "method": "eth_subscription",
    "params": {
        "subscription": "0x9ce59a13059e417087c02d3236a0b1cc",
        "result": {
            "number": "0x3f6778",
            "hash": "0x9b14d73f45c836bfb0e1f59453c39fe8cddab45a0f78670dc6190e5c85b65a8e",
            "parentHash": "0x...",
            "timestamp": "0x..."
        }
    }
}

Response Parameters

FieldTypeDescription
resultstringSubscription 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

Status CodeError MessageCause
403ForbiddenMissing or invalid ACCESS-TOKEN
-32000Subscription not supportedHTTP used instead of WebSocket

Web3 Integration

javascriptimport { ethers } from 'ethers';

const provider = new ethers.WebSocketProvider('wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/');

provider.on('block', (blockNumber) => {
    console.log('New block:', blockNumber);
});
javascriptimport { createPublicClient, webSocket } from 'viem';
import { mantle } from 'viem/chains';

const client = createPublicClient({
    chain: mantle,
    transport: webSocket('wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/')
});

const unwatch = client.watchBlocks({
    onBlock: (block) => console.log('New block:', block.number)
});