> For the complete documentation index, see [llms.txt](https://docs.getblock.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.getblock.io/api-reference/somnia/eth_unsubscribe-_-somnia.md).

# eth\_unsubscribe \_ Somnia

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.

{% hint style="warning" %}
&#x20;This method requires a WebSocket connection, not HTTP.
{% endhint %}

## Parameters

| Parameter      | Type   | Required | Description                   |
| -------------- | ------ | -------- | ----------------------------- |
| subscriptionId | string | Yes      | The subscription ID to cancel |

## Request Example

{% tabs %}
{% tab title="WebSocket (JavaScript)" %}

```javascript
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);
});
```

{% endtab %}

{% tab title="WebSocket (Python)" %}

```python
import asyncio
import websockets
import json

async def unsubscribe():
    uri = "wss://go.getblock.io/<ACCESS-TOKEN>/"
    async with websockets.connect(uri) as websocket:
        request = {
            "jsonrpc": "2.0",
            "id": 1,
            "method": "eth_unsubscribe",
            "params": ["0x9cef478923ff08bf67fde6c64013158d"]
        }
        await websocket.send(json.dumps(request))
        response = await websocket.recv()
        print(json.loads(response))

asyncio.run(unsubscribe())
```

{% endtab %}
{% endtabs %}

## Response Example

```json
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": true
}
```

## 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

{% tabs %}
{% tab title="Ethers.js" %}

```javascript
const { ethers } = require('ethers');

const provider = new ethers.WebSocketProvider('wss://go.getblock.io/<ACCESS-TOKEN>/');

// Subscribe and store unsubscribe function
const listener = (blockNumber) => console.log('Block:', blockNumber);
provider.on('block', listener);

// Later, unsubscribe
provider.off('block', listener);
```

{% endtab %}

{% tab title="Viem" %}

```javascript
import { createPublicClient, webSocket } from 'viem';
import { somnia } from 'viem/chains';

const client = createPublicClient({
  chain: somnia,
  transport: webSocket('wss://go.getblock.io/<ACCESS-TOKEN>/')
});

const unwatch = client.watchBlocks({
  onBlock: (block) => console.log(block)
});

// Later, unsubscribe
unwatch();
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.getblock.io/api-reference/somnia/eth_unsubscribe-_-somnia.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
