Created
December 10, 2016 21:11
-
-
Save bitgord/6ac91702c66267d152dafd7aec3486cd to your computer and use it in GitHub Desktop.
Get Bitfinex API data with Websockets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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"}]))