Skip to content

Instantly share code, notes, and snippets.

@webuti
Created February 26, 2018 19:43
Show Gist options
  • Save webuti/f0a31e8f914fda06ef4102fd4a957c70 to your computer and use it in GitHub Desktop.
Save webuti/f0a31e8f914fda06ef4102fd4a957c70 to your computer and use it in GitHub Desktop.
bitfinex ws ticket
const WebSocket = require('ws')
const wss = new WebSocket('wss://api.bitfinex.com/ws/2')
var bitfinexcr = ['tBTCUSD', 'tETHUSD', 'tXRPUSD', 'tBCHUSD', 'tDSHUSD', 'tETCUSD', 'tLTCUSD', 'tREPUSD', 'tXMRUSD', 'tZECUSD'];
wss.onopen = function () {
bitfinexcr.forEach(function (currency) {
wss.send(JSON.stringify({
"event": "subscribe",
"channel": "ticker",
"pair": currency
}));
});
};
var symbol = [];
var pair = [];
wss.onmessage = function (msg) {
var response = JSON.parse(msg.data);
var hb = response[1];
if (hb != "hb") {
switch (response.event) {
case 'subscribed':
eval(" symbol[" + response.chanId + "] = '" + response.symbol + "'; pair[" + response.chanId + "] = '" + response.pair + "'; ");
break;
default:
if (response[0]) {
switch (pair[response[0]]) {
case 'BTCUSD':
console.log(pair[response[0]] + ' BID: ' + response[1][0] + ' ASK: ' + response[1][2]);
break;
default:
console.log(pair[response[0]] + ' BID: ' + response[1][0] + ' ASK: ' + response[1][2]);
break;
}
}
break;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment