eth_subscribe-Ethereum
The eth_subscribe method is part of the Ethereum JSON-RPC API and is used to subscribe to specified event types. Available only via WebSocket
Subscribes to a specified event type, optionally restricted to one ormany objects. This method is available via websocket only.
The eth_subscribe method is part of the Ethereum JSON-RPC API and is used to subscribe to specified event types. This method is available only via WebSocket connections and allows clients to receive real-time updates about blockchain events.
Supported Networks
The eth_subscribe RPC Ethereum method is supported on:
Mainnet
Testnet: Sepolia, Holesky
Parameters
The method accepts the following parameters:
type (string): A subscription type. Possible values include:
newHeads: Subscribes to new headers appended to the chain, including chain reorganizations.
logs: Subscribes to logs included in new imported blocks that match the specified filter criteria.
newPendingTransactions: Subscribes to transaction hashes for all transactions added to the pending state and signed with a key available in the node.
objects (optional, hex string): Additional arguments such as an address, multiple addresses, or topics for filtering logs or events.
Request
URL (API Endpoint)
To interact with the Ethereum eth_subscribe endpoint using WebSocket, use the following example:
Response
The response may indicate success or an error, depending on the availability of WebSocket notifications.
Response
Response Description
result: On success, returns the subscription ID for the requested event type.
eth_subscribe error: On failure, provides error details, such as "notifications not supported."
value: The details of subscribed event updates, depending on the subscription type, such as block headers, logs, or transaction hashes.
Use Case
The eth_subscribe RPC Ethereum method is commonly used in Web3 applications and monitoring tools to:
Track new block headers for real-time blockchain synchronization.
Monitor specific contract logs using eth_subscribe logs to capture events emitted by smart contracts.
Observe new pending transactions for analytics or operational insights.
For example, a Web3 application can use the Ethereum eth_subscribe method to provide live updates about blockchain activity, enhancing user interaction and real-time feedback.
Code Example
Here is an eth_subscribe example of how to query the method using JavaScript:
Common Errors
When using the eth_subscribe RPC Ethereum method, the following issues may occur:
Invalid URL or ACCESS-TOKEN: Ensure the WebSocket URL and token are correct and active.
eth_subscribe error: This could happen if the requested event type or parameters are invalid.
Unsupported Notifications: If WebSocket notifications are not enabled on the node, the error "notifications not supported" may be returned.
By integrating the Web3 eth_subscribe method into your application, you can provide real-time updates for blockchain events. Use this core API method to enhance user experiences and enable live monitoring of Ethereum network activities.
Last updated