Skip to content
BlockhashBlockhashBlock 123,868,705 Sign in Join the waitlist Dashboard

Guide

Stream blocks and logs on Robinhood Chain over WebSocket

Blockhash’s WebSocket streams Robinhood Chain’s new blocks and logs with eth_subscribe, to viem or any client that speaks JSON-RPC.

Can I stream pending transactions on Robinhood Chain?

No: Robinhood Chain has a sequencer and no public mempool.

A transaction is first seen in its block.

So eth_subscribe refuses newPendingTransactions, with an error that says why.

A block’s head and its logs reach a socket once its logs are read, each block once and in order, within about a quarter of a second of the block.

{"jsonrpc":"2.0","id":3,"method":"eth_subscribe","params":["newPendingTransactions"]}
{"jsonrpc":"2.0","id":3,"error":{"code":-32601,"message":"newPendingTransactions is not offered: Robinhood Chain has a sequencer and no public mempool, so there are no pending transactions to stream: a transaction is first seen in its block. Subscribe to logs, or to newHeads"}}

How do I connect with viem’s WebSocket transport?

Open a socket to wss://robinhood-mainnet.rpc.blockhash.xyz/v1/<key>.

watchBlockNumber subscribes to newHeads, and watchEvent to logs with a filter.

viem’s watchBlocks asks for each whole block with eth_getBlockByNumber as well, at 20 units a block.

keepAlive sends net_version every 30 seconds by default, which costs nothing, and takes 5 units from the speed.

reconnect opens the socket again when it closes, 5 times 2 seconds apart by default, and asks each subscription again.

Blocks made while the socket was closed are not sent again: read them with eth_getLogs.

The sample ran against Blockhash’s WebSocket with viem 2.55.10.

Endpoint
Subscriptions
newHeads, logs
Chain ID
4663
import { createPublicClient, parseAbiItem, webSocket } from 'viem'
import { robinhood } from 'viem/chains'

const client = createPublicClient({
  chain: robinhood,
  transport: webSocket(`wss://robinhood-mainnet.rpc.blockhash.xyz/v1/${process.env.BLOCKHASH_KEY}`, {
    keepAlive: { interval: 30000 },
    reconnect: { attempts: 10, delay: 2000 },
  }),
})

// newHeads: each new block’s number
const stopHeads = client.watchBlockNumber({ onBlockNumber: (n) => console.log(n) })

// logs: USDG's transfers, one contract's events
const stopLogs = client.watchEvent({
  address: '0x5fc5360d0400a0fd4f2af552add042d716f1d168',
  event: parseAbiItem('event Transfer(address indexed from, address indexed to, uint256 value)'),
  onLogs: (logs) => console.log(logs.length),
})

How do I subscribe by hand with eth_subscribe?

Any WebSocket client works: send JSON-RPC messages, and read each notification by its subscription’s id.

A logs filter matches as eth_getLogs matches: an address or a list of them, and up to four positions of topics.

A logs subscription with no address matches every log of the chain.

Where your app has a contracts list, a logs subscription names its addresses, each one of the app’s.

eth_unsubscribe with the id ends a subscription, and answers true.

Where a block leaves the chain, each of its logs that matched is sent again with removed: true, and the block in its place follows.

const socket = new WebSocket(`wss://robinhood-mainnet.rpc.blockhash.xyz/v1/${process.env.BLOCKHASH_KEY}`)

socket.onopen = () => {
  socket.send(JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'eth_subscribe', params: ['newHeads'] }))
  socket.send(JSON.stringify({
    jsonrpc: '2.0', id: 2, method: 'eth_subscribe',
    params: ['logs', { address: '0x5fc5360d0400a0fd4f2af552add042d716f1d168' }],
  }))
}
socket.onmessage = (event) => {
  const message = JSON.parse(event.data)
  // the answer to each eth_subscribe is its id; each notification names the id it is for
  if (message.method === 'eth_subscription') console.log(message.params.subscription, message.params.result)
}
socket.onclose = (event) => console.log('closed', event.code)

What does a WebSocket stream cost?

eth_subscribe and eth_unsubscribe cost 10 units each.

Each notification costs 0.04 units a byte of it as sent, rounded up, for newHeads and logs alike.

A notification takes nothing from the plan’s speed, and counts toward the month, the bill and a spending limit.

A head is about 1,638 bytes, so a newHeads subscription costs 66 units a block.

The head the samples were sent was 1,637 bytes.

At ten blocks a second, that is up to 57 million units a day: up to $11.40 a day on Pay as you go.

A log is about 751 bytes at the median, so each log a logs subscription is sent costs about 31 units.

A logs subscription costs the logs it matches.

Every price is on Compute units.

A subscribe or unsubscribe
10 units
A notification
0.04 units a byte
A head
66 units
A log, at the median
31 units

How does a client stay connected?

  • A message is one call or a batch of up to 20 calls.
  • A message holds up to 1 MB.
  • Up to 200 messages may wait for their answers on one socket, and past that it is closed 1008.
  • A message over 2 MB closes the socket 1009.
  • The server pings every 30 seconds, and ends a socket that did not answer the last ping.
  • A WebSocket client answers a ping by itself, as the protocol requires.
  • The socket also answers at wss://api.blockhash.xyz/v1/<key>, where Cloudflare ends one idle for 100 seconds.
  • A key holds 100 sockets at once on the Free plan, and 2,000 on Pay as you go.
  • A socket holds up to 1,000 subscriptions.
  • A socket that does not read what it is sent is closed.

Every bound is on Limits and pricing.

What does each close code mean?

A client opens the socket again for each of these but 1008 and 1009, which name something to fix first.

A close for 1008 comes after a last error, sent with id null, in the words a call would be refused in.

CodeWhyWhat to do
1001The server is restarting.Open the socket again.
1008The key was revoked or ruled out, the account can no longer be served, or too many messages wait for their answers.Read the last error, sent first, and fix what it names.
1009A message was too large.Send less in one message.
1012Blocks were missed while the service restarted, past what it holds.Open the socket again, and read what was missed with eth_getLogs.
1013The service is not available now.Open the socket again in a moment.
PageWhat it documents
RPCJSON-RPC 2.0 over HTTPS, with your key as the last part of the URL.
Compute unitsWhat each call costs, what is charged, and what a call takes from a plan’s speed.
Limits and pricingWhat the Free plan gives, how fast and how large a request may be, and what a million compute units cost.