Skip to main content

chain_subscribeNewHeads

Subscribe to receive notifications when new block headers are produced on Astar. This WebSocket subscription provides real-time updates for each new block.

Use Cases#

  • Block monitoring - Track new blocks in real-time for cross-chain DeFi, multi-VM smart contracts, and XCM-enabled interoperability with Ethereum and Cosmos
  • Event indexing - Trigger processing when new blocks arrive
  • Chain synchronization - Keep external systems in sync with the chain

Parameters#

This method takes no parameters.

Returns#

Returns a subscription ID. The subscription emits Header objects for each new block:

FieldTypeDescription
parentHashHashParent block hash
numberBlockNumberBlock number
stateRootHashState trie root hash
extrinsicsRootHashExtrinsics trie root hash
digestDigestBlock digest with consensus logs

Code Examples#

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

const provider = new WsProvider('wss://api-astar.n.dwellir.com/YOUR_API_KEY');
const api = await ApiPromise.create({ provider });

// Subscribe to new heads
const unsubscribe = await api.rpc.chain.subscribeNewHeads((header) => {
console.log(`New block #${header.number}`);
console.log(` Hash: ${header.hash.toHex()}`);
console.log(` Parent: ${header.parentHash.toHex()}`);
console.log(` State root: ${header.stateRoot.toHex()}`);
});

// Later: unsubscribe()

Subscription vs Polling#

ApproachLatencyResource UsageUse Case
subscribeNewHeadsImmediateLow (push-based)Real-time monitoring
Polling getHeaderBlock time + poll intervalHigher (repeated requests)Simple integrations