eth_subscribe - GIWA
Example code for the eth_subscribe JSON-RPC method. Complete guide on how to use eth_subscribe JSON-RPC in GetBlock Web3 documentation.
This method opens a push subscription over a WebSocket connection and returns a subscription ID. Supported event types are newHeads, logs, newPendingTransactions, and syncing. It requires a wss:// endpoint rather than HTTP.
Parameters
subscriptionType
string
Yes
One of newHeads, logs, newPendingTransactions, syncing
options
object
No
Filter options (used with the logs subscription)
Request
curl --location --request POST 'https://shared.ap-southeast-1.getblock.io/<ACCESS-TOKEN>/' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"method": "eth_subscribe",
"params": ["logs", { "address": "0x4200000000000000000000000000000000000006", "topics": ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"] }],
"id": "getblock.io"
}'const axios = require('axios');
const response = await axios.post('https://shared.ap-southeast-1.getblock.io/<ACCESS-TOKEN>/', {
jsonrpc: '2.0',
method: 'eth_subscribe',
params: ['logs', { address: '0x4200000000000000000000000000000000000006', topics: ['0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'] }],
id: 'getblock.io'
}, {
headers: { 'Content-Type': 'application/json' }
});
console.log(response.data.result);import requests
response = requests.post(
'https://shared.ap-southeast-1.getblock.io/<ACCESS-TOKEN>/',
headers={'Content-Type': 'application/json'},
json={
'jsonrpc': '2.0',
'method': 'eth_subscribe',
'params': ['logs', { 'address': '0x4200000000000000000000000000000000000006', 'topics': ['0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'] }],
'id': 'getblock.io'
}
)
print(response.json())Response
{
"jsonrpc": "2.0",
"id": "getblock.io",
"result": "0x9cef478923ff08bf67fde6c64013158d"
}Response Parameters
jsonrpc
string
JSON-RPC protocol version ("2.0")
id
string
Request identifier matching the request
result
string
Subscription ID used to correlate pushed notifications
Use Cases
Live Heads: Receive newHeads pushes to follow the ~1 second block cadence
Event Streams: Stream matching logs without polling
Mempool Feeds: Receive newPendingTransactions as they arrive
Sync Monitoring: Watch syncing state transitions in real time
Error Handling
-32602
Invalid params
Unknown subscription type or malformed options
-32601
Method not found
The endpoint was called over HTTP instead of WebSocket
Web3 Integration
Last updated
Was this helpful?