Developer Fundamentals

5. New Blocks Live

Subscribe to new blocks with filters/resume.

Prerequisites

Block Feeds

Block party! 🎉 Every ~10s.

const wsUrl = subscriptions.getBlockSubscriptionUrl('https://mainnet.vechain.org', { id: '0x...' });  // Resume from ID
const ws = new WebSocket(wsUrl);
ws.onmessage = (msg) => {
  const block = JSON.parse(msg.data);
  console.log('New Block #', block.number, 'Txs:', block.transactions);
};
const wsUrl = subscriptions.getBlockSubscriptionUrl('https://mainnet.vechain.org', { id: '0x...' });  // Resume from ID
const ws = new WebSocket(wsUrl);
ws.onmessage = (msg) => {
  const block = JSON.parse(msg.data);
  console.log('New Block #', block.number, 'Txs:', block.transactions);
};
const wsUrl = subscriptions.getBlockSubscriptionUrl('https://mainnet.vechain.org', { id: '0x...' });  // Resume from ID
const ws = new WebSocket(wsUrl);
ws.onmessage = (msg) => {
  const block = JSON.parse(msg.data);
  console.log('New Block #', block.number, 'Txs:', block.transactions);
};

Filters: beneficiary, id. Parse for number/timestamp/gas.
Chain pulsing!​

PRO TIP

in the snippet you may have spot { id: '0x...' }, resuming from a known ID is a trick to prevents misses when your service or node may go offline.​

Pitfalls: No resume? Gap on disconnect.