Skip to content
Discussion options

You must be logged in to vote
  1. Implement heartbeat (CRITICAL)
const ws = new WebSocket(url);

function heartbeat() {
  clearTimeout(this.pingTimeout);
  this.pingTimeout = setTimeout(() => {
    ws.terminate(); // force reconnect
  }, 30000);
}

ws.on('open', () => {
  ws.ping();
});

ws.on('pong', heartbeat);

ws.on('close', () => {
  reconnect();
});
  1. Add robust reconnect logic (you already started 👍)
let retry = 0;

function reconnect() {
  const delay = Math.min(1000 * 2 ** retry, 30000); // exponential backoff
  setTimeout(connect, delay);
  retry++;
}
  1. Resubscribe after reconnect (IMPORTANT)
{
  "type": "subscribe",
  "channels": ["ticker", "level2"],
  "product_ids": ["BTC-USD"]
}

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@asseph
Comment options

Answer selected by asseph
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug GitHub or a GitHub feature is not working as intended Codespaces Your development environment, in the cloud. Run VS Code and code on GitHub's cloud platform, source:ui Discussions created via Community GitHub templates
2 participants