December 10, 20258 min read

Building a Real-Time Price Engine with WebSockets

Dhiraj Kumar

Dhiraj Kumar

Software Engineer & Full-Stack Developer

WebSocketsNode.jsReactBackend

When I joined Bireena Infotech, the jewellery wholesale platform was using REST polling — clients would hit an endpoint every 30 seconds to check for price updates. With gold and silver rates fluctuating throughout the day, 30-second latency meant lost revenue for retailers making bulk orders.

The Problem

The platform served 50+ wholesale retailers managing 500+ SKUs with dynamic pricing based on live metal rates. The existing polling approach had three critical issues: server overload during peak hours, price staleness causing order discrepancies, and excessive bandwidth consumption on mobile clients.

Architecture Decisions

I evaluated three approaches: Server-Sent Events (SSE), WebSockets, and long polling. WebSockets won because of full-duplex communication, allowing the server to push updates instantly without client requests. I chose Socket.io over raw WebSockets for its automatic fallback to long-polling and built-in reconnection logic.

  • Socket.io server on Node.js with Express
  • Bull queue for processing rate-change events
  • Redis for caching current prices (sub-millisecond reads)
  • React client with custom useWebSocket hook

Implementation Details

The price engine listens to an upstream metal-rate API via a cron job every 2 minutes. When rates change, a Bull job recalculates all 500+ SKU prices and publishes them to a Redis channel. The Socket.io server subscribes to this channel and broadcasts diffs to connected clients.

// Price recalculation worker
const priceQueue = new Bull('price-recalc');
priceQueue.process(async (job) => {
  const { metalRate, purity } = job.data;
  const skus = await Sku.find({ active: true });
  for (const sku of skus) {
    sku.currentPrice = calculatePrice(sku.weight, metalRate, purity, sku.makingCharge);
    await sku.save();
  }
  await redis.publish('price-updates', JSON.stringify(skus));
});

On the client side, the Socket.io connection is wrapped in a custom React hook that handles reconnection, state synchronization, and connection health monitoring. The UI updates instantly via React state — no page reloads, no polling intervals.

Results

  • Price updates delivered in under 100ms (down from 30s polling)
  • Server CPU usage dropped 40% by eliminating 50+ polling clients
  • Zero order discrepancies due to stale pricing since launch
  • ₹8.5L monthly GMV handled without performance degradation

Key Takeaways

WebSockets are not always the answer — for this use case (frequent writes, many clients), they were perfect. But don't underestimate the complexity of connection management. Socket.io handles most of it, but you still need to think about auth, rate limiting, and scaling across multiple Node.js instances (Redis adapter).

Dhiraj Kumar

Dhiraj Kumar

Dhiraj Kumar is a software engineer and full-stack developer interested in modern web technologies, artificial intelligence, automation, and agentic systems. Through his portfolio and technical writing, he explores practical approaches to building reliable software and AI-powered applications.

Enjoyed this article? Let's connect!

Read More Articles