Skip to content

Instantly share code, notes, and snippets.

@jmoz
Created June 2, 2020 07:59
Show Gist options
  • Save jmoz/63dfd59eaff2cd8d61d23b8f1906e674 to your computer and use it in GitHub Desktop.
Save jmoz/63dfd59eaff2cd8d61d23b8f1906e674 to your computer and use it in GitHub Desktop.
FTX Websockets Javascript
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}`);
}
@Jolly-Pirate
Copy link

You forgot const WebSocket = require('ws');

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