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
# This method requires WebSocket connection
wscat -c wss://go.getblock.io/<ACCESS-TOKEN>/
> {"jsonrpc":"2.0","method":"eth_subscribe","params":["newheads"],"id":"getblock.io"}const WebSocket = require('ws');
const ws = new WebSocket('wss://go.getblock.io/<ACCESS-TOKEN>/');
ws.on('open', () => {
ws.send(JSON.stringify({
jsonrpc: '2.0',
method: 'eth_subscribe',
params: ['newHeads'],
id: 'getblock.io'
}));
});
ws.on('message', (data) => {
console.log(JSON.parse(data));
});import asyncio
import websockets
import json
async def unsubscribe():
uri = "wss://go.getblock.io/<ACCESS-TOKEN>/"
async with websockets.connect(uri) as ws:
payload = {
"jsonrpc": "2.0",
"method": "eth_subscribe",
"params": ["newHeads"],
"id": "getblock.io"
}
await ws.send(json.dumps(payload))
response = await ws.recv()
print(json.loads(response))
asyncio.run(unsubscribe())Response Example
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x9cef478923ff08bf67fde6c64013158d"
}{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params": {
"subscription": "0x9cef478923ff08bf67fde6c64013158d",
"result": {
"number": "0x1a2b3c",
"hash": "0x4e3a3754410177e6937ef1f84bba68ea139e8d1a2258c5f85db9f1cd715a1bdd",
"parentHash": "0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238",
"timestamp": "0x65a1b2c3"
}
}
}Response Parameters
Parameter
Type
Description
Use Cases
Error Handling
Error Code
Description
SDK Integration
Last updated
Was this helpful?