Skip to main content

eth_subscribe

Creates a new subscription for particular events. The node returns a subscription ID. For each event that matches the subscription, a notification with relevant data is sent together with the subscription ID.

Parameters

Specify one of the following subscription events:

  • newHeads: Subscribing to this returns a notification each time a new header is appended to the chain, including chain reorganizations. In a chain reorganization, the subscription emits all new headers for the new chain. Therefore the subscription can emit multiple headers at the same height.

  • logs: Returns logs that are included in new imported blocks and match the given filter criteria. In case of a chain reorganization, previously sent logs that are on the old chain are resent with the removed property set to true. Logs from transactions that ended up in the new chain are emitted. Therefore a subscription can emit logs for the same transaction multiple times. This parameter has the following fields:

    • address: (optional) Either an address or an array of addresses. Only logs that are created from these addresses are returned.

    • topics: (optional) Only logs that match these specified topics are returned.

    Infura Recommendation

    We strongly recommend specifying a filter (address or topics or both) when subscribing to the logs event.

  • newPendingTransactions: Returns the hash for all transactions that are added to the pending state and are signed with a key that's available in the node. When a transaction that was previously part of the canonical chain isn't part of the new canonical chain after a reorganization, it's emitted again.

note

The newPendingTransactions event is only available on mainnet.

Returns

  • subscription ID: The ID of the newly created subscription on the node.

Example

Replace <YOUR-API-KEY> with an API key from your Infura dashboard.

WebSocket connections only

Subscription methods are available for WebSocket connections only.

Request

wscat -c wss://opbnb-mainnet.infura.io/ws/v3/<YOUR-API-KEY> -x '{"jsonrpc": "2.0", "id": 1, "method": "eth_subscribe", "params": ["newHeads"]}'

Response

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