Developer Fundamentals

-- III. Transaction Log

Learn how to monitor live transactions entering the pool using WebSockets and how to retrieve their details using a second request.

Listening to Transaction Logs

This lesson will take you through the most important information about how to listen to a transaction log and how to establish multiple connections to the node.

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

A simple connection can be established with this snippet:

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

const wsUrl = subscriptions.getNewTransactionsSubscriptionUrl(
  'https://mainnet.vechain.org'
);

const ws = new WebSocket(wsUrl);
ws.onopen = () => {
  console.log('Connected to', wsUrl);
};

ws.onmessage = (message) => {
  const addedTx = JSON.parse(message.data);
  console.log('New transaction', addedTx);
};

This will receive all new entries added to the transaction pool in the form of JSON-encoded strings.

Subscriptions receive the transaction ID as soon as it is added to the transaction pool. A transaction may either be:

  • Successfully included in a block

  • Reverted

  • Expired in the future

To obtain detailed information about a transaction, you need to make a second request, either directly from the node or using a Thor client.