Unichain

eth_subscribe
The eth_subscribe method creates a subscription to specific events on the Ethereum blockchain. It requires a WebSocket connection and allows clients to receive real-time notifications about new blocks, pending transactions, logs, and synchronization status.
Use Cases
- Building real-time blockchain explorers and dashboards
- Monitoring smart contract events without constant polling
- Creating responsive dApps that update instantly when state changes
- Tracking pending transactions to detect when they're mined
- Receiving immediate notification when a payment arrives
- Implementing efficient event-driven architecture for blockchain applications
- Monitoring smart contract activity with minimal server load
- Building high-performance trading systems requiring real-time data
- Tracking node synchronization in real-time
- Implementing webhooks for blockchain events
Method Details
Subscription Types
newHeads
Subscribe to receive information about new blocks added to the blockchain. This is useful for applications that need to react to new blocks or monitor chain progress.
Request
Response
Initial response with subscription ID:
Subsequent notifications when new blocks are added:
Key Block Header Fields
The notification includes a complete block header with the following key fields:
- number: The block height (hexadecimal)
- hash: The block hash (may be null if still pending)
- parentHash: Hash of the parent block
- timestamp: Unix timestamp when block was mined
- gasUsed: Total gas used by all transactions in the block
- gasLimit: Maximum gas allowed in the block
- baseFeePerGas: Base fee per gas (post-EIP-1559)
- miner: Address of the miner/validator who produced the block
- difficulty: Block difficulty (will be 0 after The Merge)
- size: Size of the block in bytes
logs
Subscribe to receive logs that match specific filter criteria. This is particularly useful for tracking events emitted by smart contracts.
Request with Filter Options
Filter Options Explained
The second parameter is a filter object that can include:
- address (optional): String or array of strings with addresses to filter logs
- topics (optional): Array of topic filters, supports null for wildcard
- fromBlock (optional): Block height from which to start getting logs (hexadecimal)
- toBlock (optional): Block height to which to get logs (hexadecimal)
Topics Array Format
Topics are ordered and correspond to the event signature and indexed parameters:
- First topic: keccak256 hash of the event signature (e.g.,
Transfer(address,address,uint256)) - Second topic: First indexed parameter
- Third topic: Second indexed parameter (if present)
- Fourth topic: Third indexed parameter (if present)
Topics can be:
- A single topic: exact match required
- Null: any value accepted (wildcard)
- Array of topics: any of the topics in the array will match (OR condition)
Response
Initial response with subscription ID:
Subsequent notifications when matching logs are emitted:
Log Object Fields
Every log notification contains:
- address: The address of the contract that emitted the log
- topics: Array of 0-4 32-byte topics, indexed parameters from the event
- data: Contains the non-indexed parameters of the log
- blockNumber: Block in which the transaction was included
- transactionHash: Hash of the transaction
- transactionIndex: Integer of the transaction's position in the block
- blockHash: Hash of the block
- logIndex: Integer of the log index position in the block
- removed:
truewhen the log was removed due to a chain reorganization,falseif it's a valid log
newPendingTransactions
Subscribe to receive notification of new pending transactions as they're added to the transaction pool.
Request
Response
Initial response with subscription ID:
Subsequent notifications when new pending transactions are added:
Important Notes
- WebSocket Required:
eth_subscribeis only available over WebSocket connections, not HTTP - Subscription Limits: Many providers limit the number of active subscriptions per connection
- Missed Events: If connection drops, events occurring during disconnection are missed
- Chain Reorganizations: Events may be marked as removed during chain reorganizations
- Client Support: Not all Ethereum clients support all subscription types
- Public Node Restrictions: Public nodes like Infura may have throttling for subscriptions
- Filter Precision: More specific filters improve performance and reduce data volume
- Security Considerations: WebSockets have different security requirements than HTTP
- Guaranteed Delivery: Unlike message queues, WebSocket does not guarantee delivery
- Connection Stability: Long-running subscriptions should implement reconnection logic
See also
- eth_unsubscribe - Cancels an active subscription
- eth_newFilter - HTTP-based alternative for log filtering
- eth_getLogs - Query historical events
- eth_getFilterChanges - Poll-based alternative for logs
- eth_newBlockFilter - HTTP-based alternative for new blocks
- eth_newPendingTransactionFilter - HTTP-based alternative for pending transactions
- WebSockets Guide - Comprehensive guide to WebSocket connections
- Event Logging Guide - Guide to contract events and logs
Parameters
Subscription type: "newHeads", "logs", "newPendingTransactions", or "syncing"
Optional filter object (required for "logs" subscription) (JSON)