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.
Subscription type: "newHeads", "logs", "newPendingTransactions", or "syncing"
Optional filter object (required for "logs" subscription)
Contract address(es) to filter logs (for logs subscription)
Topics to filter logs (for logs subscription)
Subscription ID that can be used to identify subscription and unsubscribe later
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.
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_subscribe",
"params": ["newHeads"]
}
Initial response with subscription ID:
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x9cef478923ff08bf67fde6c64013158d"
}
Subsequent notifications when new blocks are added:
{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params": {
"subscription": "0x9cef478923ff08bf67fde6c64013158d",
"result": {
"difficulty": "0x0",
"extraData": "0xd883010b05846765746888676f312e32302e35856c696e7578",
"gasLimit": "0x1c9c380",
"gasUsed": "0xe8c35a",
"logsBloom": "0x24a0a00e2800a84002408401021c8200410a40340440800900000820804830202d00004204204804800900110314a0088ac11a0a402809a044404a943d0812ce408101342401004a6080490900245000240140901901124288a20d840008200014150600a2080832000220250912a0400124009c00202430c00298b0a8d0822d1a10188080000c0124203a041100601880308406a2600d0420a140801000a4c80a2a0080a5108042302008080a0050a000a44214011610400002090000170003800020d0c1840022008408a4810401d881002202a2100a00204244120200000000810600da940000a0018a40406100006100c044a0202400200420000200211000880",
"miner": "0x86c501ed1cb7123feb9db41f10eca511fb3e1416",
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"nonce": "0x0000000000000000",
"number": "0x1b4",
"parentHash": "0x3863a513b3d30160cf76cefee31652f188ddd2e2c9b169bc7d35e324f99cd24e",
"receiptsRoot": "0x28d996993bebb61fb8056d9938f148c3c683e772ae5d53b63ef9bcc601072a66",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"size": "0x973",
"stateRoot": "0xb9c737605971351a00b9a978b72271874ef3cf0a2dc87a7c4325993ddf84e83f",
"timestamp": "0x64",
"transactionsRoot": "0xf25ff1909e48c93a122b52d9c576b62c141a2f51f3d9b7c53afa7cdb2eec62b2",
"baseFeePerGas": "0x42214d6b"
}
}
}
The notification includes a complete block header with the following key fields:
Subscribe to receive logs that match specific filter criteria. This is particularly useful for tracking events emitted by smart contracts.
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_subscribe",
"params": [
"logs",
{
"address": "0x8320fe7702b96808f7bbc0d4a888ed1468216cfd",
"topics": ["0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab902"]
}
]
}
The second parameter is a filter object that can include:
Topics are ordered and correspond to the event signature and indexed parameters:
Transfer(address,address,uint256)
)Topics can be:
Initial response with subscription ID:
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x4a8a4c0517381924f9838102c5a4dcb7"
}
Subsequent notifications when matching logs are emitted:
{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params": {
"subscription": "0x4a8a4c0517381924f9838102c5a4dcb7",
"result": {
"address": "0x8320fe7702b96808f7bbc0d4a888ed1468216cfd",
"blockHash": "0x61cdb2a09ab99abf791d474f20c2ea89bf8de2923a2d42bb49944c8c993cbf04",
"blockNumber": "0x29e87",
"data": "0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003",
"logIndex": "0x0",
"removed": false,
"topics": ["0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab902"],
"transactionHash": "0xe044554a0a55067caafd07f8020ab9f2af60bdfe337e395ecd84b4877a3d1ab4",
"transactionIndex": "0x0"
}
}
}
Every log notification contains:
true
when the log was removed due to a chain reorganization, false
if it's a valid logSubscribe to receive notification of new pending transactions as they're added to the transaction pool.
{
"id": 1,
"jsonrpc": "2.0",
"method": "eth_subscribe",
"params": ["newPendingTransactions"]
}
Initial response with subscription ID:
{
"jsonrpc": "2.0",
"id": 1,
"result": "0xc3b33aa549fb9a60e95d21862596617c"
}
Subsequent notifications when new pending transactions are added:
{
"jsonrpc": "2.0",
"method": "eth_subscription",
"params": {
"subscription": "0xc3b33aa549fb9a60e95d21862596617c",
"result": "0xd6fdc5cc41a9959e922f30cb772a9aef46f4daea279307bc5f7024edc8f1c857"
}
}
eth_subscribe
is only available over WebSocket connections, not HTTP