Skip to main content

Command Palette

Search for a command to run...

Real-Time XAUUSD Price Tracking Using Node.js and WebSockets

The next gold move could happen in minutes. Make sure you're watching real-time data, not yesterday's prices.

Updated
โ€ข2 min read
Real-Time XAUUSD Price Tracking Using Node.js and WebSockets
P
Full-stack developer with 7+ years of experience in .NET, specializing in building scalable systems and designing technical architectures. I have a strong focus on AI-driven applications, where I bridge practical engineering with intelligent solutions. Creator of RealMarketAPI, a platform built to deliver reliable, real-time market data for developers and businesses. Iโ€™m driven by curiosity and a passion for learning, always exploring new technologies and better ways to solve problems. Outside of work, I enjoy music, coffee, and occasionally trying my luck at being a comedian.

Gold is one of the most actively traded assets in the world. Whether you're a trader, investor, or developer building financial applications, having access to real-time gold prices is essential.

In this tutorial, we'll build a simple real-time gold price tracker using Node.js and WebSockets. By the end, you'll have a live dashboard that updates automatically whenever the market price changes.

Why Real-Time Data Matters

Many free APIs provide delayed market data, which can be unsuitable for trading applications. Real-time streaming allows your application to:

  • Receive updates instantly

  • Reduce API requests

  • Improve user experience

  • React faster to market movements

Prerequisites

Before getting started, you'll need:

  • Node.js installed

  • A RealMarketAPI account

  • Basic JavaScript knowledge

Step 1: Create a New Project

mkdir gold-tracker 

cd gold-tracker 

npm init -y 

npm install ws 

Step 2: Connect to the WebSocket

Create a file called index.js.

const WebSocket = require('ws');

const ws = new WebSocket('YOUR_WEBSOCKET_URL');

ws.on('open', () => { console.log('Connected'); });

ws.on('message', (data) => { const tick = JSON.parse(data); console.log(tick); });

ws.on('close', () => { console.log('Disconnected'); }); 

Step 3: Subscribe to Gold Prices

After connecting, subscribe to the XAUUSD symbol.

ws.send(JSON.stringify({ action: 'subscribe', symbol: 'XAUUSD' }));

Now every new price update will be streamed directly to your application.

Step 4: Display Live Prices

ws.on('message', (data) => { const tick = JSON.parse(data);
console.clear();
console.log('Gold Price Tracker');
console.log('------------------');
console.log(`Bid: ${tick.bid}`);
console.log(`Ask: ${tick.ask}`);
console.log(`Time: ${tick.timestamp}`);
}); 

Step 5: Add Price Change Detection

Let's highlight significant market movements.

let lastPrice = null;

ws.on('message', (data) => { const tick = JSON.parse(data);
if (lastPrice && tick.bid > lastPrice) {
    console.log('๐Ÿ“ˆ Gold moving up');
}

if (lastPrice && tick.bid < lastPrice) {
    console.log('๐Ÿ“‰ Gold moving down');
}

lastPrice = tick.bid;
});

Possible Enhancements

You can extend this project by:

Sending Telegram alerts Creating a web dashboard with React Storing historical prices Calculating moving averages Detecting breakout opportunities

Final Thoughts

Building a real-time gold price tracker is a great introduction to financial market data and WebSocket development. With just a few lines of code, you can create applications that react instantly to market movements.

If you're building trading tools, dashboards, or automated strategies, real-time streaming data can significantly improve both performance and user experience.

More from this blog

R

RealMarketAPI - Real-Time Market Data API

2 posts

Welcome to the RealMarketAPI blog.

This blog shares insights, tutorials, product updates, and best practices for working with real-time financial market data. Readers can expect content on API integration, WebSocket streaming, AI agents, algorithmic trading infrastructure, fintech development, market data engineering, and SaaS growth.

We publish practical guides, code examples, architecture discussions, industry trends, and announcements related to RealMarketAPI. Whether you're building tradin