eth_subscribe

Creates a new subscription over particular events. The node will return a subscription id.

📘

For each event that matches the subscription a notification with relevant data is sent together with the subscription id.

Parameters

Object NameRequiredTypeDetail
SUBSCRIPTIONStringThe method of subscription. Choose from: newHeads, logs, newPendingTransactions, or syncing.
SUBSCRIPTION ARGUMENTStringThe arguments that go with the subscription.

Result

A hex code of an integer that represents the subscription ID.

Example

🚧

eth_subscribe will not work on HTTP/HTTPS, since HTTP/HTTPS do not support server push notifications. Use WebSocket or IPC instead.

Depending on 4 different methods of SUBSCRIPTION, we have different examples as follows:
a) If SUBSCRIPTION is newHeads:
Fires a notification each time a new header is appended to the chain, including chain reorganizations. In case of a chain reorganization the subscription will emit all new headers for the new chain. Therefore the subscription can emit multiple headers on the same height.

SUBSCRIPTION ARGUMENTS is not required.

Request

wscat -c "<your-endpoint>"

> {"jsonrpc":"2.0","method":"eth_subscribe","params": ["newHeads"],"id":1}

Return

< {"jsonrpc":"2.0","id":1,"result":"0x2b436f7e0edce2fb956f2da870fab594"}

< {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0x2b436f7e0edce2fb956f2da870fab594","result":{"parentHash":"0x17ffb2bf7115b425d436e55a91331f31d6e7ed06143c0d009ff078ecd00e5101","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x829bd824b016326a401d083b33d092293333a830","stateRoot":"0x8d4d9fa7cae87be0439a6bc2b2b6f00e550c76106edb35a2bdb0713aeedd9d99","transactionsRoot":"0xaeca0405f73618b678ab8dc320589cab9b47569d43f63d3b17440543311b0c51","receiptsRoot":"0xaac169c7f3a31eb2fccc643be73d77e3a00647cf2cb3e00cc1126092812da7ed","logsBloom":"0x8cbed9abab59ca8f1d82a745a5594b484e06300d5246ec2a57ffd26a1c9b08aaceec0948a4d81f94c25c7999649641611675f33918c9ffa11ed1366c647eb12302744c00515c8c6d5b8f756a5250762353a24a54de5fbd203e809641cc0158e0132809ad2e5bf403914d5c8a286679f13d21e450632004a90a32e338daf90cd55804c58883235d9e0678d4445dbfc8d5360e3539ed9c4ff99528354e6c139834b60b6155e3df7d5bab8d65fa8dcf58c743541b6b761f2b3999f1f6f7dc9990db33f4e27aeca154e6a9be0513c06553700902e35564fe66100e88a18607326ffe0df4fd4c4c4151736c9cf469c30eca0f23c7f408b14ea5f2e1ea1dd262827423","difficulty":"0x2da6f7cb9a2d82","number":"0xdc837e","gasLimit":"0x1c950d7","gasUsed":"0x127b861","timestamp":"0x623cec9b","extraData":"0xe4b883e5bda9e7a59ee4bb99e9b1bc4a1721","mixHash":"0xa361b647b546dd207aa4a1305875e864c249f1f59f7fc01349fb9be4b880a481","nonce":"0x1cbd3b000e2a6e84","baseFeePerGas":"0xb35abbb50","hash":"0xd366a387da9e3be9d352213a00410b50805d03a619c8f8628b15e06bbd3d11d1"}}}

< {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0x2b436f7e0edce2fb956f2da870fab594","result":{"parentHash":"0xd366a387da9e3be9d352213a00410b50805d03a619c8f8628b15e06bbd3d11d1","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x433022c4066558e7a32d850f02d2da5ca782174d","stateRoot":"0xf562db1e91eada6d4e2a9d2a28af92313b12fbdaef23857d393929cb17caacdf","transactionsRoot":"0x77a3a3442d69f3642364f8874c9def7eaaaff29fa62a156065577e32ac430e6e","receiptsRoot":"0xbb833bc77af5c4fce8cf2413bfc42836a09a3979e670a046c11273240db452a6","logsBloom":"0x02a00102490a735033a02090800040040082014008230401108520000700012113068100c0240a4240005801001101410200800828a06885100228c0f32524a0200110010140083808030008118008202000220041460110281034189a614200c208828826000c0801047422010008c2a0101100109b8c03000a21910008a05010440580807034242258041400001000404842230140024856a0014c68102081260058090008388206005583880104004026c0420010060028200408903818c61d04945a7c040490110842048406019020202810458902140500a10ec070600c80702109200089400000108600414a800052040320e00151810a485000209505","difficulty":"0x2dacb4aa93a0c7","number":"0xdc837f","gasLimit":"0x1c8de84","gasUsed":"0x742117","timestamp":"0x623ceca1","extraData":"0x4b31506f6f6c2e636f6d202f2050303033","mixHash":"0x0b26c6f2b3bc2c075b010f9eacb7aa155ef8af3d109ea79db99891e608b2f9b6","nonce":"0x0300d702032fe740","baseFeePerGas":"0xb9ee01b05","hash":"0x29576bdee516f24036cf191a85c757eeaa8e13fc30c07e0a2f78c252ec5f1b07"}}}

b) If SUBSCRIPTION is logs:
Returns logs that are included in new imported blocks and match the given filter criteria.
In case of a chain reorganization previous sent logs that are on the old chain will be resend 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.

SUBSCRIPTION ARGUMENTS has the following choices:

KeyRequiredTypeDetail
addressString/ArrayAn address or an array of addresses. Only logs that are created from these addresses are returned.
topicsArrayArray of topic specifiers.

Request

wscat -c <your-endpoint>

> {"jsonrpc":"2.0","id": 1, "method": "eth_subscribe", "params": ["logs", {"address": "0x8320fe7702b96808f7bbc0d4a888ed1468216cfd", "topics": ["0xd78a0cb8bb633d06981248b816e7bd33c2a35a6089241d099fa519e361cab902"]}]}

Return

< {"jsonrpc":"2.0","id":1,"result":"0x16636ca7ef5f318c1790a9ab0707bf02"}

c) If SUBSCRIPTION is 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.

SUBSCRIPTION ARGUMENTS is not required.

Request

wscat -c <your-endpoint>

> {"jsonrpc":"2.0","id": 2, "method": "eth_subscribe", "params": ["newPendingTransactions"]}

Return

< {"jsonrpc":"2.0","id":2,"result":"0x597bd00e383ce6653d79247ffd195457"}

< {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0x597bd00e383ce6653d79247ffd195457","result":"0x5a3269a66da5dfba034d32fa23a5c910872be9a2f76acfddc1ad8235a3ce7651"}}

< {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0x597bd00e383ce6653d79247ffd195457","result":"0xc5209fbd421109b91223a657506ee19ed895ebd29b348cd936aa4c5b64e0036a"}}

< {"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0x597bd00e383ce6653d79247ffd195457","result":"0x4f6807cd8b083c61ae3805c2ae8a4eece605fcd50c7aeeff497650e5079be029"}}

d) If SUBSCRIPTION is 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.

SUBSCRIPTION ARGUMENTS is not required.

Request

wscat -c <your-endpoint>

> {"jsonrpc":"2.0","id": 2, "method": "eth_subscribe", "params": ["syncing"]}

Return

< {"jsonrpc":"2.0","id":2,"result":"0x7ee1216931fec83cbca74aab167ec5e7"}
wscat -c <your-endpoint>

> {"jsonrpc":"2.0","id": 1, "method": "eth_unsubscribe", "params": ["0x7ee1216931fec83cbca74aab167ec5e7"]}

Return

< {"jsonrpc":"2.0","id":1,"result":true}