Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
878 B
1
Indexable
Never
const Binance = require('binance-api-node').default;
const Redis = require('redis');

const binanceClient = Binance();
const redisClient = Redis.createClient();

const symbols = ['ETH', 'BTC'];

// Define an asynchronous function to handle the ticker updates
async function handleTickerUpdate(symbol, price) {
  return new Promise((resolve, reject) => {
    redisClient.set(symbol, price, (error, result) => {
      if (error) {
        reject(error);
      } else {
        resolve(result);
      }
    });
  });
}

// Subscribe to the real-time price feed for each symbol
symbols.forEach(symbol => {
  binanceClient.ws.ticker(symbol + 'USDT', async ticker => {
    const price = parseFloat(ticker.bestBid);
    try {
      await handleTickerUpdate(symbol, price);
    } catch (error) {
      console.error(`Error updating ${symbol} price in Redis: ${error}`);
    }
  });
});