0
votes

I want to retrieve JSON data of multiple ticker channels from a Bitfinex websocket API but it seems I can only connect to one currency pair.

How can I do it for multiple pairs?

Here is the sample code for one pair :

var bfx = new WebSocket("wss://api.bitfinex.com/ws");
bfx.onopen = function(){
   bfx.send(JSON.stringify({"event":"subscribe","channel":"ticker","pair":"BCHUSD"}));
};
var bchprc=0;
bfx.onmessage = function(msg){
    var response = JSON.parse(msg.data);
    bchprc = response[1];
    if(bchprc!="hb"){
        $("#bitfinex_bch_prc").html(response[7]);
    }
};

Refer to this link

1

1 Answers

0
votes

I use 'for' loop to retrieve them. I think that is the system allow to do.

for (let i of ['BTC','EOS','ZRX','XML','blah','blahh']) {
    let pair = {"event":"subscribe", "channel":"ticker"}
    if (i !== 'BTC') {
        pair.symbol = `t${i}BTC`  //3rd key is 'symbol'
        WSS.send(JSON.stringify(pair))
    }
    pair.symbol = "t${i}USD"
    WSS.send(JSON.stringify(pair))
}