# eth_unsubscribe - Mantle

> 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. It requires a WebSocket connection.

### Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| subscriptionId | string | The subscription ID returned by eth_subscribe |

> **Info:** eth_unsubscribe requires a WebSocket connection.

### Request (WebSocket)

#### JavaScript (WebSocket)

#### ws-example.js

```javascript
// WebSocket connection required
const ws = new WebSocket('wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/');

ws.onopen = () => {
    ws.send(JSON.stringify({
        "jsonrpc": "2.0",
        "method": "eth_unsubscribe",
        "params": ["0x9ce59a13059e417087c02d3236a0b1cc"],
        "id": "getblock.io"
    }));
};

ws.onmessage = (event) => {
    console.log('Received:', JSON.parse(event.data));
};
```

#### JavaScript (axios - note)

#### axios-note.js

```javascript
import axios from 'axios';

// Note: eth_unsubscribe requires WebSocket
const data = JSON.stringify({
    "jsonrpc": "2.0",
    "method": "eth_unsubscribe",
    "params": ["0x9ce59a13059e417087c02d3236a0b1cc"],
    "id": "getblock.io"
});

console.log('WebSocket message:', data);
```

#### Python

#### ws-example.py

```python
import websocket
import json

def on_message(ws, message):
    print(f"Received: {message}")

def on_open(ws):
    ws.send(json.dumps({
        "jsonrpc": "2.0",
        "method": "eth_unsubscribe",
        "params": ["0x9ce59a13059e417087c02d3236a0b1cc"],
        "id": "getblock.io"
    }))

ws = websocket.WebSocketApp(
    "wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/",
    on_message=on_message,
    on_open=on_open
)
ws.run_forever()
```

#### Rust

#### rust-example.rs

```rust
// Note: eth_unsubscribe requires WebSocket connection
let message = r#"{
    "jsonrpc": "2.0",
    "method": "eth_unsubscribe",
    "params": ["0x9ce59a13059e417087c02d3236a0b1cc"],
    "id": "getblock.io"
}"#;
```

### Response

#### response.json

```json
{
    "jsonrpc": "2.0",
    "id": "getblock.io",
    "result": true
}
```

### Response Parameters

| Field | Type | Description |
| --- | --- | --- |
| result | boolean | True if subscription was cancelled successfully |

### Use Case

The `eth_unsubscribe` method is essential for:

* Cleaning up subscriptions
* Resource management
* Stopping event streams
* Connection lifecycle management

### Error Handling

| Status Code | Error Message | Cause |
| --- | --- | --- |
| 403 | Forbidden | Missing or invalid ACCESS-TOKEN |
| -32000 | Subscription not found | Invalid subscription ID |

### Web3 Integration

#### Ethers.js

#### ethers-unsubscribe.js

```javascript
import { ethers } from 'ethers';

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

// Unsubscribe by removing listener
provider.off('block');
// Or close the connection
await provider.destroy();
```

#### Viem

#### viem-unsubscribe.js

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

const client = createPublicClient({
    chain: mantle,
    transport: webSocket('wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/')
});

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

// Unsubscribe
unwatch();
```
