3

I have simple websocket API from api.bitfinex.com/ws that stream changes on BTC/USD market. I'm struggle how to make this to update simple csv file, so when receive new data from ws to update csv. I try to use fast-csv, but without success. Here is my node js code:

    const WebSocket = require('ws');
    const ws = new WebSocket("wss://api.bitfinex.com/ws");

    const fs = require('fs');

    ws.onopen = function(){
        ws.send(JSON.stringify({'event':'subscribe', 'channel':'ticker', 'pair':'btcusd'}))
    };

    ws.onmessage = function(msg){ 

        var response = JSON.parse(msg.data); 
        if (response[1] !="hb"){
            console.log("Bitfin " + response[7]); 
            //HERE I need to update existing CSV file,
            //for example 
            //BTC,xxxx
            //ETH,xxxx
            //two columns, and n rows..
        }   
    };

So, any idea how to stream into csv (BTCUSD in row 1 column 2; ETH in row 2 column 2 etc)

1 Answer 1

3

Seems to me that you should be using a writeStream. Something like that:

  const fs = require('fs')
  let writeStream = fs.createWriteStream(fileName)

  ws.onmessage = function(msg){
      //... your code ...
      writeStream.write(msg)
  }

  writeStream.on('finish', () => {
    console.log(`Finished writing!`)
  })
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.