eth_subscribe - zksync

Creates a real-time subscription over a WebSocket connection. Available subscription types are newHeads (new block headers) and logs (matching contract events).

Only available over WebSocket transport — use wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/. Note: zkSync does not support the syncing subscription type.

Parameters

ParameterTypeRequiredDescription
subscriptionTypestringYesOne of "newHeads", "logs"
filterObjectobjectNoFor "logs": filter with address and/or topics

Request Example

cURL (wscat)

bash# This method requires WebSocket connection
wscat -c wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/

> {"jsonrpc":"2.0","method":"eth_subscribe","params":["newheads"],"id":"getblock.io"}

JavaScript (ws)

javascriptconst WebSocket = require('ws');

const ws = new WebSocket('wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/');

ws.on('open', () => {
    ws.send(JSON.stringify({
        jsonrpc: '2.0',
        method: 'eth_subscribe',
        params: ['newHeads'],
        id: 'getblock.io'
    }));
});

ws.on('message', (data) => {
    console.log(JSON.parse(data));
});

Python (websockets)

pythonimport asyncio
import websockets
import json

async def unsubscribe():
    uri = "wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/"
    async with websockets.connect(uri) as ws:
        payload = {
            "jsonrpc": "2.0",
            "method": "eth_subscribe",
            "params": ["newHeads"],
            "id": "getblock.io"
        }
        await ws.send(json.dumps(payload))
        response = await ws.recv()
        print(json.loads(response))

asyncio.run(unsubscribe())

Rust (tokio-tungstenite)

rustuse tokio_tungstenite::connect_async;
use futures_util::{SinkExt, StreamExt};
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let url = "wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/";
    let (mut ws, _) = connect_async(url).await?;
    
    let payload = json!({
        "jsonrpc": "2.0",
        "method": "eth_subscribe",
        "params": ["newHeads"],
        "id": "getblock.io"
    });
    
    ws.send(payload.to_string().into()).await?;
    
    if let Some(msg) = ws.next().await {
        println!("{:?}", msg?);
    }
    
    Ok(())
}

Response Example

json{
    "jsonrpc": "2.0",
    "id": "getblock.io",
    "result": "0x9cef478923ff08bf67fde6c64013158d"
}

Response Parameters

FieldTypeDescription
resultstringSubscription ID, used to match incoming eth_subscription notifications and to call eth_unsubscribe

Use Cases

  • Real-time wallet activity notifications
  • Live DEX trade feeds for trading UIs
  • Streaming indexers that react to new L2 blocks instantly

Error Handling

Status CodeError MessageCause
403ForbiddenMissing or invalid <ACCESS-TOKEN>
-32602Invalid paramsRequest parameters are missing or malformed
429Too Many RequestsRate limit exceeded for your plan
-32600Invalid Requesteth_subscribe was called over HTTP; use a WebSocket connection instead

SDK Integration

javascriptimport { Provider } from 'zksync-ethers';

const provider = Provider.getDefaultProvider();
// Or with explicit WebSocket: new Provider('wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/');

provider.on('block', (blockNumber) => {
    console.log('New block:', blockNumber);
});
javascriptimport { createPublicClient, webSocket } from 'viem';
import { zksync } from 'viem/chains';

const client = createPublicClient({
    chain: zksync,
    transport: webSocket('wss://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/')
});

const unwatch = client.watchBlockNumber({
    onBlockNumber: (blockNumber) => console.log('New block:', blockNumber)
});