Blocks

Connection

The connection is managed using WebSockets, which connect directly to a VeChain node.

A simple connection can be established with this snippet:

import WebSocket from 'ws';
const ws = new WebSocket('wss://mainnet.vechain.org/subscriptions/block');
ws.onmessage = (message) => {
    console.log('New block', message.data);
}

This will receive a new block as soon as it is added to the blockchain, in the form of JSON-encoded strings.

Options

To resume listening from a specific block position, the options can include a blockID to continue from where a previous listener may have disconnected.

For additional details on the options, refer to the documentation of BlockSubscriptionOptions.

Block Details

The blocks are received as JSON-encoded strings. These strings must be parsed into usable objects, resulting in an object of type BlockDetail.

An example result is:

Example Project

Last updated

Was this helpful?