Skip to content

Instantly share code, notes, and snippets.

@bitgord
Created December 10, 2016 21:11
Show Gist options
  • Save bitgord/6ac91702c66267d152dafd7aec3486cd to your computer and use it in GitHub Desktop.
Save bitgord/6ac91702c66267d152dafd7aec3486cd to your computer and use it in GitHub Desktop.
Get Bitfinex API data with Websockets
// First go to http://docs.bitfinex.com and click on the websockets tab
// Scroll down and find the SSL websocket connection URL
For example: wss://api.bitfinex.com/ws
// In a new Javascript file create a variable for new websocket
var ws = new WebSocket('wss://api.bitfinex.com/ws');
// Create function to send on open
ws.onopen = function() {
ws.send(JSON.stringify({"event":"subscribe", "channel":"ticker", "pair":"BTCUSD"}));
};
// Tell function what to do when message is received and log messages to "btc" div
ws.onmessage = function(msg) {
// create a variable for response and parse the json data
var response = JSON.parse(msg.data);
// save hb variable from bitfinex
var hb = response[1];
if(hb != "hb") {
document.getElementById("btc").innerHTML = "ASK: " + response[3] + "<br> LAST: " + response[7] + "<br> BID: " + response[1];
}
};
@rahulEth
Copy link

does bitfinex support batch request .....
Ex: ws.send(JSON.stringify([{"event":"subscribe", "channel":"ticker", "pair":"BTCUSD"},{"event":"subscribe", "channel":"ticker", "pair":"ETHUSD"},{"event":"subscribe", "channel":"ticker", "pair":"XRPUSD"}]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment