2.6.2.2 WebSocket Broadcasting

A scheduled job or long-poll fetches price updates and emits marketUpdate events to connected Socket.io clients:

// backend/src/socket/marketSocket.js
module.exports = (io) => {
  setInterval(async () => {
    // hypothetical array of coins
    const coins = ['dogecoin', 'shiba-inu'];
    const updates = {};

    for (const coin of coins) {
      updates[coin] = await fetchPrice(coin);
    }

    io.emit('marketUpdate', updates);
  }, 5000);
};

The frontend listens for marketUpdate and updates charts, widgets, or price tickers in real time.

Last updated