Developer Fundamentals

1. WebSockets & Transfers

Connect to a VeChain node and filter live VET transfers.

Prerequisites

The Direct Line 📞

Think of standard blockchain data reading like checking your mailbox every hour to see if a letter arrived. That’s "polling," and it's exhausting!.

WebSockets are different. They work like a direct phone line that stays open, letting the blockchain call you the second something happens.

In JavaScript, we use the @vechain/sdk-network to manage these connections easily. Nice!

Let's start by listening to VET Transfers. VET is the native currency, so we track its movement in real time to build transparent payment systems.

import { subscriptions } from '@vechain/sdk-network';
import WebSocket from 'ws';

// Build a custom URL to listen to the Testnet
const wsUrl = subscriptions.getVETtransfersSubscriptionUrl('https://testnet.vechain.org');
const ws = new WebSocket(wsUrl);

ws.onmessage = (message) => {
    const transfer = JSON.parse(message.data);
    console.log('New Transfer received!', transfer);
};
import { subscriptions } from '@vechain/sdk-network';
import WebSocket from 'ws';

// Build a custom URL to listen to the Testnet
const wsUrl = subscriptions.getVETtransfersSubscriptionUrl('https://testnet.vechain.org');
const ws = new WebSocket(wsUrl);

ws.onmessage = (message) => {
    const transfer = JSON.parse(message.data);
    console.log('New Transfer received!', transfer);
};
import { subscriptions } from '@vechain/sdk-network';
import WebSocket from 'ws';

// Build a custom URL to listen to the Testnet
const wsUrl = subscriptions.getVETtransfersSubscriptionUrl('https://testnet.vechain.org');
const ws = new WebSocket(wsUrl);

ws.onmessage = (message) => {
    const transfer = JSON.parse(message.data);
    console.log('New Transfer received!', transfer);
};

You're getting it! 🌟 You can even add filters so you only hear about transfers from a specific sender or to a specific recipient.

TO REMEMBER

The amounts arrive as "hexlified" BigInts (like 0xa7b7...), so remember to convert them using BigInt() to make them readable.

Almost there!