Creates a real-time subscription to on-chain events. The node pushes event notifications to the client as JSON-RPC messages whenever a matching event occurs, without the client needing to poll.
Parameters
The event type to subscribe to. See subscription types below.
Optional filter options. Only applicable for the logs subscription type. Show Filter options (logs only)
A single contract address or array of addresses to filter logs by. Optional.
Array of topic filters in the same format as eth_getLogs. Optional.
Subscription Types
Type Description Notification payload newHeadsFires for each new block header appended to the chain Block header object logsFires for each new log matching the filter criteria Log object newPendingTransactionsFires for each new pending transaction hash added to the mempool Transaction hash string
Returns
A hex-encoded subscription ID. All subsequent event notifications from this subscription include this ID in params.subscription.
Event notifications arrive as unsolicited JSON-RPC messages with method: "eth_subscription":
{
"jsonrpc" : "2.0" ,
"method" : "eth_subscription" ,
"params" : {
"subscription" : "0x1887ec8b9589ccad00000000000532da" ,
"result" : { ... }
}
}
Example
Subscribe to new block headers:
Request
Response
Event Notification
{ "jsonrpc" : "2.0" , "method" : "eth_subscribe" , "params" : [ "newHeads" ], "id" : 1 }
Subscribe to contract logs:
Request
Response
Event Notification
{
"jsonrpc" : "2.0" ,
"method" : "eth_subscribe" ,
"params" : [
"logs" ,
{
"address" : "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913" ,
"topics" : [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
]
}
],
"id" : 1
}