Last active
August 16, 2020 20:42
-
-
Save richwednesday/10bfc9614412cfaf47ad7c85fa0faf09 to your computer and use it in GitHub Desktop.
Streaming Price of Bitcoin from FTX> https://docs.ftx.com/#websocket-api
This file contains hidden or 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
import WebSocket from 'ws'; | |
const ws = new WebSocket('wss://ftx.com/ws/'); | |
let ping; | |
function open() { | |
ws.on('open', function open() { | |
ws.send(JSON.stringify({ | |
op: 'subscribe', | |
channel: 'ticker', | |
market: 'BTC-PERP' | |
})) | |
setInterval(() => { | |
if (ping === "yes") | |
open(); | |
ping = "yes"; | |
ws.send(JSON.stringify({ op: 'ping' })) | |
}, 15000); | |
}); | |
} | |
open(); | |
ws.on('message', async (data) => { | |
let payload | |
try { | |
payload = JSON.parse(data); | |
} catch(e) { | |
console.error('ftx send bad json', data); | |
} | |
if (payload) | |
{ | |
if (payload.channel === "ticker" && payload.type === "update") | |
priceData = payload.data; | |
else if (payload.type === "pong") | |
ping = "done"; | |
} | |
}); | |
ws.on('error', (error) => { | |
console.error(error); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment