1. Polling or WebSocket from a crypto provider.
2. Caching in Redis to reduce API calls (e.g., store updated prices for 30–60 seconds).
3. Broadcast to frontend clients via Socket.io or respond through REST endpoints.
// backend/src/services/marketDataService.js
const axios = require('axios');
const redisClient = require('../config/redisClient');
async function fetchPrice(coinSymbol) {
const cacheKey = `price:${coinSymbol}`;
const cached = await redisClient.get(cacheKey);
if (cached) return JSON.parse(cached);
// Example: CoinGecko
const url = `https://api.coingecko.com/api/v3/simple/price?ids=${coinSymbol}&vs_currencies=usd`;
const res = await axios.get(url);
const priceData = res.data;
// Cache result
await redisClient.set(cacheKey, JSON.stringify(priceData), { EX: 30 });
return priceData;
}
module.exports = { fetchPrice };