Skip to main content

eth_unsubscribe

This API allows you to cancel specific subscriptions.

1.Request Parameters

ParameterTypeRequiredDescription
idinteger or stringrequiredUnique identifier for each request. Used by the client to match requests with server responses. Typically expressed as a number or string.
jsonrpcstringrequiredIndicates the JSON-RPC protocol version. The version used on the network is "2.0", so always use this value.
methodstringrequiredSpecifies the JSON-RPC method name to execute. Use "eth_unsubscribe" here.
paramsarrayrequiredArguments required for execution, passed as an array. For "eth_unsubscribe", provide the subscription_id as the argument.

The arguments passed in params vary depending on the subscription type. Below is a description of the arguments for each subscription type.

ArgumentTypeRequiredDescription
subscription_idstringrequiredSpecifies the subscription ID to cancel.
{
"jsonrpc": "2.0",
"id": 1,
"method": "eth_unsubscribe",
"params": ["0x52f404d003f4a461c8aa72f27062b4bc"]
}

2. Response

When you receive a successful response using the above parameters, a response in the following format is returned.

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

3. How to use

This API is WebSocket-based; you can subscribe using a WebSocket communication tool. This example uses wscat as the WebSocket client tool, which makes it easy to connect to the WebSocket server and send/receive data.

3.1. Connect to Websocket channel

Open a terminal window and enter the command below. Specify the protocol and network you want to subscribe to, and enter the API from the project you created in the console.

# Set protocol, network and your api key in the URL to connect (e.g., wss://ethereum-mainnet.nodit.io/FwG...)
wscat -c wss://{protocol}-{network}.nodit.io/{your_api_key}

3.2. Subscribe a specific event

Once the WebSocket connection is established, subscribe to the desired event from the examples below.

{ "jsonrpc": "2.0", "id": 1, "method": "eth_subscribe", "params": ["newHeads"] }
{
"jsonrpc": "2.0",
"id": 1,
"method": "eth_subscribe",
"params": [
"logs",
{
"address": "0xdAC17F958D2ee523a2206206994597C13D831ec7",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
]
}
]
}
{
"jsonrpc": "2.0",
"id": 1,
"method": "eth_subscribe",
"params": ["newPendingTransactions", true]
}

3.3. Listen subscription

The first response returns the subscription ID.

{
"jsonrpc": "2.0",
"id": 1,
"result": "0xb273410795d4411d707f272834cdd60e"
}

Thereafter, results matching the subscription type are returned whenever events occur.

{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params": {
"subscription": "0xb273410795d4411d707f272834cdd60e",
"result": {
# ...
}
}
}

3.4. Unsubscribe

Press CTRL+C in the terminal to close the connection and cancel the subscription. Alternatively, use eth_unsubscribe to cancel the subscription while keeping the connection open. Send the subscription ID linked to the channel in params as shown in the example below to cancel that subscription.

{
"jsonrpc": "2.0",
"id": 1,
"method": "eth_unsubscribe",
"params": ["0x540e1706d67fd05fc8f3318dc7e86fc7"]
}

If the subscription was canceled successfully, you will receive the following response.

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