eth_subscribe - Somnia
Example code for the eth_subscribe JSON-RPC method. Complete guide on how to use eth_subscribe JSON-RPC in GetBlock Web3 documentation
Parameters
Parameter
Type
Required
Description
Subscription Types
Request Example
const WebSocket = require('ws');
const ws = new WebSocket('wss://go.getblock.io/<ACCESS-TOKEN>/');
ws.on('open', () => {
// Subscribe to new block headers
const subscribeRequest = {
jsonrpc: '2.0',
id: 1,
method: 'eth_subscribe',
params: ['newHeads']
};
ws.send(JSON.stringify(subscribeRequest));
});
ws.on('message', (data) => {
const response = JSON.parse(data);
console.log('Received:', response);
});import asyncio
import websockets
import json
async def subscribe():
uri = "wss://go.getblock.io/<ACCESS-TOKEN>/"
async with websockets.connect(uri) as websocket:
# Subscribe to new block headers
request = {
"jsonrpc": "2.0",
"id": 1,
"method": "eth_subscribe",
"params": ["newHeads"]
}
await websocket.send(json.dumps(request))
while True:
response = await websocket.recv()
print(json.loads(response))
asyncio.run(subscribe())Response Example
Response Parameters
Parameter
Type
Description
Use Cases
Error Handling
Error Code
Description
SDK Integration
Last updated
Was this helpful?