eth_unsubscribe - Optimism
Example code for the eth_unsubscribe json-rpc method. Сomplete guide on how to use eth_unsubscribe json-rpc in GetBlock.io Web3 documentation.
This method cancels a subscription that was created with eth_subscribe. Once cancelled, no more notifications will be sent for that subscription. This should be called when real-time updates are no longer needed.
The eth_unsubscribe method cancels an existing WebSocket subscription on Optimism.
Parameters
subscriptionId
String
The subscription ID to cancel.
Yes
Request Sample
# This method requires WebSocket connection
wscat -c wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/
> {"jsonrpc":"2.0","method":"eth_unsubscribe","params":["0x1"],"id":"getblock.io"}const WebSocket = require('ws');
const ws = new WebSocket('wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/');
ws.on('open', () => {
ws.send(JSON.stringify({
jsonrpc: '2.0',
method: 'eth_unsubscribe',
params: ['0x1'],
id: 'getblock.io'
}));
});
ws.on('message', (data) => {
console.log(JSON.parse(data));
});import asyncio
import websockets
import json
async def unsubscribe():
uri = "wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/"
async with websockets.connect(uri) as ws:
payload = {
"jsonrpc": "2.0",
"method": "eth_unsubscribe",
"params": ["0x1"],
"id": "getblock.io"
}
await ws.send(json.dumps(payload))
response = await ws.recv()
print(json.loads(response))
asyncio.run(unsubscribe())Response
A successful response returns the following:
{
"jsonrpc": "2.0",
"id": "getblock.io",
"result": true
}Response Parameters
id: A unique request identifier, matching theidsent in the request body.jsonrpc: Specifies the use of JSON-RPC version 2.0.result: Boolean indicating whether the subscription was successfully cancelled.
Use Case
The eth_unsubscribe method is commonly used for:
Subscription management
Resource cleanup
Connection lifecycle management
Error Handling
-32602
Invalid subscription ID
-32603
Internal error
SDK Integration
Last updated
Was this helpful?