⚠️Blast API (blastapi.io) ends Oct 31. Migrate to Dwellir and skip Alchemy's expensive compute units.
Switch Today →
Skip to main content

chain_subscribeNewHeads

Description

Subscribe to real‑time chain updates over WebSocket. Use to stream new or finalized heads or runtime version changes into your app.

Broadcasts new block headers as they are imported. Requires WebSocket.

JavaScript (polkadot.js)

import { ApiPromise, WsProvider } from '@polkadot/api';

const api = await ApiPromise.create({ provider: new WsProvider(S.wsBase) });
const unsub = await api.rpc.chain.subscribeNewHeads((header) => {
console.log('#', header.number.toString(), 'hash', header.hash.toHex());
});
// Later: await unsub();