Created
June 2, 2020 07:59
-
-
Save jmoz/63dfd59eaff2cd8d61d23b8f1906e674 to your computer and use it in GitHub Desktop.
FTX Websockets Javascript
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
const symbols = [ | |
"BTC-PERP", | |
"BTC/USD", | |
"ETH-PERP", | |
"ETH/USD", | |
]; | |
const ws_init = function () { | |
ws = new WebSocket("wss://ftx.com/ws/"); | |
ws.onopen = function () { | |
for (s of symbols) { | |
ws.send(`{"op": "subscribe", "channel": "trades", "market": "${s}"}`); | |
} | |
}; | |
ws.onclose = function () { | |
setTimeout(ws_init, 5000); | |
}; | |
ws.onerror = function (error) { | |
console.log('WS error: ' + error); | |
}; | |
ws.onmessage = function (e) { | |
let data = JSON.parse(e.data); | |
if (data.channel == "trades" && data.type == "update") { | |
handleTrades(data); | |
} | |
}; | |
const ping = function () { | |
ws.send('{"op":"ping"}'); | |
}; | |
setInterval(ping, 10000); | |
} | |
ws_init(); | |
const handleTrades = function (data) { | |
if (data.market == "BTC/USD") { | |
btc_usd = data.data[0].price; // set the btc global for calculations, used in app.js | |
} | |
$("#btcPrice").text(`${data.market}: ${data.data[0].price}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You forgot
const WebSocket = require('ws');