Search code examples
javascriptnode.jsccxt

Getting real time data from API into route


I am using the following two files to get data from two APIs. Please find below my minimum viable example:

poloniex.js

const Poloniex = require('poloniex-api-node')
const poloniex = new Poloniex()

async function getExchangeTicker() {
  poloniex.returnTicker((err, ticker) => {
    if (err) {
      console.log(err.message)
    } else {
      //console.log(ticker)
      return ticker
    }
  })
}

module.exports = {
  getExchangeTicker,
}

cctx.js

const ccxt = require ('ccxt')

async function getExchangeTicker() {
  const bitfinex = new ccxt.bitfinex({ verbose: true })  
  const data = await bitfinex.fetchTicker()
  return data
}

module.exports = {
  getExchangeTicker,
}

scheduler.js

const exchangePoloniex = require('../exchange/poloniex')
const exchangeCCTX = require('../exchange/cctx')

async function getAllTickers() {
  const exchanges = [
    exchangePoloniex,
    exchangeCCTX,
  ]

  let res
  exchanges.forEach((exchange) => {
    res = exchange.getExchangeTicker()
  })
  return res
}

async function runScheduler() {
  let res
  setInterval(() => {
    this.res = getAllTickers()
  }, 3000)
  console.log("res: " + res)
  return res
}

runScheduler()

I am running a scheduler to pool data from these files, but only get res: undefined back.

Any suggestions how to properly get the data from these two APIs?

I highly appreciate your replies!


Solution

  • I don't know the two APIs you are accessing, so I can't really judge whether your code from poloniex.js or cctx.js is good - I assume you can tell through console.logs, etc, that you are getting the API data you require from each separately.

    But I can see some logic problems in your scheduler.js file:

    1. In getAllTickers, your .forEach loop overwrites res every iteration, so only the last result will be visible.
    2. Since it's an async function, getAllTickers returns a promise. You'll need to use .then or something similar on what it returns.
    3. The console.log and return in runScheduler complete before the setInterval has a chance to execute, even once, so no data from there is available.

    I think the following setup may be what you want:

    const exchangePoloniex = require('../exchange/poloniex')
    const exchangeCCTX = require('../exchange/cctx')
    
    async function getAllTickers() {
    
      let updatedTickers = []
      updatedTickers[0] = await exchangePoloniex.getExchangeTicker()
      updatedTickers[1] = await exchangeCCTX.getExchangeTicker()
    
      return updatedTickers
    }
    
    function runScheduler() {
      let tickers
      setInterval(() => {
        tickers = getAllTickers()
        console.log(tickers) // tickers is a promise at this point
    
        tickers.then((data) => {
            console.log(data) // should be the data returned from your apis
            // store the data in your db
        })
      }, 3000)
    }
    
    runScheduler()
    

    Notice that runScheduler did not have to be async, because you are not doing anything with the return value - all the work is inside the setInterval callback

    If you need to serve this data in response to a browser request, you could then fetch it from your db, knowing it has been updated within the last 3 seconds.