eth_unsubscribe _ Somnia

Example code for the eth_unsubscribe JSON-RPC method. Complete guide on how to use eth_unsubscribe JSON-RPC in GetBlock Web3 documentation

This method cancels an existing subscription created with eth_subscribe on the Somnia network. This is important for resource management, especially when monitoring high-throughput events.

circle-exclamation

Parameters

Parameter
Type
Required
Description

subscriptionId

string

Yes

The subscription ID to cancel

Request Example

const WebSocket = require('ws');

const ws = new WebSocket('wss://go.getblock.io/<ACCESS-TOKEN>/');

ws.on('open', () => {
  const unsubscribeRequest = {
    jsonrpc: '2.0',
    id: 1,
    method: 'eth_unsubscribe',
    params: ['0x9cef478923ff08bf67fde6c64013158d']
  };
  ws.send(JSON.stringify(unsubscribeRequest));
});

ws.on('message', (data) => {
  const response = JSON.parse(data);
  console.log('Unsubscribed:', response.result);
});

Response Example

Response Parameters

Parameter
Type
Description

result

boolean

True if unsubscribed, false otherwise

Use Cases

  • Clean up subscriptions when done

  • Manage connection resources

  • Switch between different subscriptions

  • Handle application shutdown

Error Handling

Error Code
Description

-32602

Invalid subscription ID

-32603

Internal error

false result

Subscription not found

SDK Integration

Last updated

Was this helpful?