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
This method creates a subscription over WebSocket on the Somnia network for real-time event notifications. Given Somnia's ~100ms block times and high throughput, subscriptions enable efficient real-time monitoring without polling. This method requires a WebSocket connection.
This method requires a WebSocket connection (not HTTP).
Parameters
subscriptionType
string
Yes
Type: "newHeads", "logs", "newPendingTransactions"
filterObject
object
No
Filter options (for "logs" type)
Subscription Types
newHeads- Subscribe to new block headerslogs- Subscribe to contract event logsnewPendingTransactions- Subscribe to pending transactions
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
subscription
string
Subscription ID
result
object
Event data (varies by subscription type)
Use Cases
Real-time block monitoring
Live event tracking for dApps
Instant transaction notifications
Building live dashboards
MEV and arbitrage detection
Error Handling
-32602
Invalid subscription type or filter
-32603
Internal error
Connection closed
WebSocket disconnected
SDK Integration
Last updated
Was this helpful?