Send heartbeat to bitcoinity to keep the connection alive

Thanks to blueagain for figuring this out.
This allows for long queries to bitcoinity to not fail
when the requested exchange/currency pair is not received
quickly enough
This commit is contained in:
King_DuckZ 2022-05-15 01:31:32 +02:00
commit 5aef83f4e8
3 changed files with 61 additions and 3 deletions

View file

@ -0,0 +1,32 @@
// Copyright 2022, blueagain
// https://github.com/terrablue
import WebSocket from "ws";
const ws = new WebSocket("ws://bitcoinity.org/webs_bridge/websocket");
let ref = 2;
const send = (ws, data) => {
ws.send(JSON.stringify({...data, payload: {}, ref}));
ref++;
}
const join = (ws, topic) =>
send(ws, {topic, event: "phx_join"});
const currencies = ["USD"];
ws.on("open", () => {
ws.on("message", message => {
const parsed_message = JSON.parse(message);
if (currencies.includes(parsed_message.payload.data?.currency)) {
const {currency, exchange_name, last} = parsed_message.payload.data;
console.log(`currency: ${currency}, exchange: ${exchange_name}, last: ${last}`);
}
});
join(ws, "webs:markets");
join(ws, "webs:bitfinex_USD");
setInterval(() => {
send(ws, {topic: "phoenix", event: "heartbeat"})
}, 30000)
})