Skip to content

Websockets

WebSockets provide persistent connections for real-time blockchain updates.


FeatureHTTPWebSocket
ConnectionRequest-ResponsePersistent
LatencyHigherLower
Real-timePolling neededPush-based
Use caseQueriesLive data

const ws = new WebSocket('ws://localhost:8546');
ws.onopen = () => {
ws.send(JSON.stringify({
jsonrpc: '2.0',
method: 'eth_subscribe',
params: ['newHeads'],
id: 1
}));
};
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('New block:', data.params.result);
};
ws.send(JSON.stringify({
jsonrpc: '2.0',
method: 'eth_subscribe',
params: ['newPendingTransactions'],
id: 2
}));

Terminal window
geth \
--ws \
--ws.addr 0.0.0.0 \
--ws.port 8546 \
--ws.api eth,net,web3 \
--ws.origins "*" \
--ws.ratelimit 500

  • WebSockets enable real-time updates
  • Subscribe to new blocks, pending transactions
  • Lower latency than HTTP polling

Last Updated: 2026-02-20