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.

circle-exclamation

Parameters

Parameter
Type
Required
Description

subscriptionType

string

Yes

Type: "newHeads", "logs", "newPendingTransactions"

filterObject

object

No

Filter options (for "logs" type)

Subscription Types

  • newHeads - Subscribe to new block headers

  • logs - Subscribe to contract event logs

  • newPendingTransactions - Subscribe to pending transactions

Request Example

subscribe.js
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);
});

Response Example

Response Parameters

Parameter
Type
Description

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

Error Code
Description

-32602

Invalid subscription type or filter

-32603

Internal error

Connection closed

WebSocket disconnected

SDK Integration

Last updated

Was this helpful?