Ethereum API - Subscribe to different event types like newHeads, logs, pendingTransactions, and syncing using websockets.
Parameters
- Subscription type
- Optional params
The first argument specifies the type of event for which to listen. The second argument contains additional options which depend on the first argument. The different description types, their options, and their event payloads are described below.
Returns
The subscription ID. This ID will be attached to any received events, and can also be used to cancel the subscription using eth_unsubscribe.
Subscription Events
While the subscription is active, you will receive events which are objects with the following fields:
- jsonrpc: Always "2.0"
- method: Always "eth_subscription"
- params: An object with the following fields:
- subscription: The subscription ID returned by the eth_subscription call which created this subscription.
- result: An object whose contents vary depending on the type of subscription.
Subscription types
1. newPendingTransactions
Returns the hash for all transactions that are added to the pending state.
When a transaction that was previously part of the canonical chain isn’t part of the new canonical chain after a reorganization its again emitted.
NOTE: This method is only supported on Ethereum and Polygon networks (Mainnet and Mumbai).
Parameters
None
Example
Request
wscat -c wss://base-testnet.unifra.io/ws/{your-api-key}
connected (press CTRL+C to quit)
> {"jsonrpc":"2.0","id": 2, "method": "eth_subscribe", "params": ["newPendingTransactions"]}
Result
{"jsonrpc":"2.0","id":2,"result":"0x33f8c5197de6ef7f928dec69e4445892"}
2. newHeads
Emits an event any time a new header is added to the chain, including during a chain reorganization.
NOTE: Chain Reorganizations (ReOrgs)
When a chain reorganization occurs, this subscription will emit an event containing all new headers for the new chain. This means that you may see multiple headers emitted with the same height, and when this happens the later header should be taken as the correct one after a reorganization.
Parameters
None
Example
Request
wscat -c wss://base-testnet.unifra.io/ws/{your-api-key}
{"jsonrpc":"2.0","id": 1, "method": "eth_subscribe", "params": ["newHeads"]}
Result
< {"jsonrpc":"2.0","id":1,"result":"0x6c8e7e3099c36410e91c32de205e6af6"}
< {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0x6c8e7e3099c36410e91c32de205e6af6","result":{"parentHash":"0x26bc847f7289313007e51ed0553be9f49c25c14a46908bb4c346a640c7591ccf","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x4200000000000000000000000000000000000011","stateRoot":"0x6352487c497ebe00302e634dd9c337f3e8d1f57da1482b4fc396c47b5471ee87","transactionsRoot":"0xfca1c5a0dbd05d6d88d525afc589008d536121551ad8e98bb01c980f290188c3","receiptsRoot":"0xc2bce7fe124b797a2d250fac29df06867683345d85e8b746d14e55c84c106348","logsBloom":"0x00200000000000000000000080000000000002000000000000040001000000000000000000000000000800100000000000000002000000000000100000000000000000000000000020000008000000200000000000000000020000008000000000000000000000000000000000000000000000000000000000000010000000100000000000000000000000000000000000000001002000480000004000000000000000000000000000000000000000000004000000004000000020000000000000000002000000010000000200000000000000000000001000000000000000000000000020000000000000000000000000000000000000400000000000000000","difficulty":"0x0","number":"0x5d22c3","gasLimit":"0x17d7840","gasUsed":"0x2435a","timestamp":"0x6493b296","extraData":"0x","mixHash":"0xa4fb8f7ceb2af8a157d86b01f711351b68ac4f9bcdcd741c02722d24a00db951","nonce":"0x0000000000000000","baseFeePerGas":"0x32","withdrawalsRoot":null,"hash":"0xdf75d322cf28501628c103840affee71c68c86810f5ff22631e571e97d4b2f98"}}}
< {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0x6c8e7e3099c36410e91c32de205e6af6","result":{"parentHash":"0xdf75d322cf28501628c103840affee71c68c86810f5ff22631e571e97d4b2f98","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x4200000000000000000000000000000000000011","stateRoot":"0x9a8ca53c40fbdbfb1fd86fd7c6257856dc1a5e8b49c33d587ad684c3f47bf841","transactionsRoot":"0x81c4053ce0972e91a78bc2fee269fcb93909c150ad3c0b8a8d0f556f5aeaf715","receiptsRoot":"0xe38cc509e727288b3a90d70dd2950fc292c15c93d0765c884983369510b8dbe3","logsBloom":"0x00200000000000000020000880080200000000000000000000840008000900080000000000000008080004101000002000000010000000001200000000600000000000000000800000000118040200201400000000000000000000808000010010000200010440000000000000000000000000000810000000000010000000000000002000000000004000000000200000001001012010080000004000000000028000000000000040000008000002080004000000000000040000010000000000040002000010020000000402000080040000010000011000000000000000000018000000000002000000000800000000000000000000440010000000000000","difficulty":"0x0","number":"0x5d22c4","gasLimit":"0x17d7840","gasUsed":"0x6691a","timestamp":"0x6493b298","extraData":"0x","mixHash":"0xa4fb8f7ceb2af8a157d86b01f711351b68ac4f9bcdcd741c02722d24a00db951","nonce":"0x0000000000000000","baseFeePerGas":"0x32","withdrawalsRoot":null,"hash":"0x86593f564bc68d6ec2f49660692886dad8328742d92cda136c7b93059e7dbeb9"}}}
3. logs
Emits logs which are part of newly added blocks that match specified filter criteria.
NOTE: Chain Reorganizations (ReOrgs)
When a chain reorganization occurs, logs that are part of blocks on the old chain will be emitted again with the property removed set to true.
Logs which are part of the blocks on the new chain are also emitted, it is possible to see logs for the same transaction multiple times in the case of a reorganization.
Parameters
- An object with the following fields:
- adddress (optional): either a string representing an address or an array of such strings.
- Only logs created from one of these addresses will be emitted.
- topics: an array of topic specifiers.
- Each topic specifier is either null, a string representing a topic, or an array of strings.
- Each position in the array which is not null restricts the emitted logs to only those who have one of the given topics in that position.
- adddress (optional): either a string representing an address or an array of such strings.
Some examples of topic specifications:
- []: Any topics allowed.
- [A]: A in first position (and anything after).
- [null, B]: Anything in first position and B in second position (and anything after).
- [A, B]: A in first position and B in second position (and anything after).
- [[A, B], [A, B]]: (A or B) in first position and (A or B) in second position (and anything after).
Example
Request
wscat -c wss://base-testnet.unifra.io/ws/{your-api-key}
{"jsonrpc":"2.0","id": 1, "method": "eth_subscribe", "params": ["logs", {"address": "0x4200000000000000000000000000000000000006", "topics": ["0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c"]}]}
Result
< {"jsonrpc":"2.0","id":1,"result":"0x12b87f12b1ac3d86179c0697836d7ada"}
< {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0x12b87f12b1ac3d86179c0697836d7ada","result":{"address":"0x4200000000000000000000000000000000000006","topics":["0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c","0x00000000000000000000000022e9e33ed834a6e9ac980e62137eda891e2498b6"],"data":"0x0000000000000000000000000000000000000000000000000000019c936e3800","blockNumber":"0x5d2363","transactionHash":"0x121a69c2035c20c697b5e549b81b03720ca27b5694c0cf2da869b8353290bb15","transactionIndex":"0x2","blockHash":"0xeacabbb4aa3855caaec6ffdc8743e6116adb040c2988327379dee40ce828c8c1","logIndex":"0xb","removed":false}}}
< {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0x12b87f12b1ac3d86179c0697836d7ada","result":{"address":"0x4200000000000000000000000000000000000006","topics":["0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c","0x00000000000000000000000022e9e33ed834a6e9ac980e62137eda891e2498b6"],"data":"0x000000000000000000000000000000000000000000000000000002b77712ee00","blockNumber":"0x5d2363","transactionHash":"0x79320905dd42ecb9bbdd8f9946e4a95b9b087f4db7f7f909e7ad831477618cf0","transactionIndex":"0x3","blockHash":"0xeacabbb4aa3855caaec6ffdc8743e6116adb040c2988327379dee40ce828c8c1","logIndex":"0x10","removed":false}}}
< {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0x12b87f12b1ac3d86179c0697836d7ada","result":{"address":"0x4200000000000000000000000000000000000006","topics":["0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c","0x00000000000000000000000029843613c7211d014f5dd5718cf32bcd314914cb"],"data":"0x000000000000000000000000000000000000000000000000000009184e72a000","blockNumber":"0x5d2364","transactionHash":"0xd9804d66fbfe6e7b339fea0fc7a14b8e8acb26b2b54893bc9d5041647a0fe9a2","transactionIndex":"0x1","blockHash":"0x9ffc50bc47d1a93353ff1c8ea7662f94883c7f4048310eb80aee663e3eb8f75a","logIndex":"0x0","removed":false}}}
4. syncing
Indicates when the node starts or stops synchronizing. The result can either be a boolean indicating that the synchronization has started (true), finished (false) or an object with various progress indicators.
Parameters
None
Example
Request
wscat -c wss://base-testnet.unifra.io/ws/{your-api-key}
{"jsonrpc":"2.0","id": 1, "method": "eth_subscribe", "params": ["syncing"]}
Result
{"jsonrpc":"2.0","id":1,"result":"0xa9e6f06133cbb6ffb69124d7c82fb2b1"}