const MAX_RESTARTS = 5;
const RESTART_DELAY_MS = 30_000;
let restarts = 0;
function start() {
const stream = createStreamClient({
keyId: process.env.PARLAYX_KEY_ID!,
privateKeyHex: process.env.PARLAYX_PRIVATE_KEY_HEX!,
});
/* A confirmed session means the credential and endpoint are good, so the
budget below is about consecutive failures, not lifetime ones. */
stream.on("welcome", () => {
restarts = 0;
});
stream.on("snapshot", (frame) => {
/* apply the snapshot to your local book */
});
stream.on("terminated", ({ reason }) => {
console.error(`stream terminated: ${reason}`);
/* A revoked or kicked key refuses every retry, so never restart on these. */
if (reason === "goodbye:authRevoked" || reason === "goodbye:kicked") return;
/* Everything else is worth one more try, but bound it. A revoked or unknown
key 401s at the upgrade with no goodbye frame, so it arrives here as
`maxReconnectAttempts`, indistinguishable from a real outage. Without a
cap, each restart buys a fresh reconnect budget and the loop never ends. */
if (restarts >= MAX_RESTARTS) {
console.error("giving up; check the signing key, the clock, and the endpoint");
return;
}
restarts += 1;
setTimeout(start, RESTART_DELAY_MS);
});
stream.subscribe([{ venue: "polymarket", tokenId: "71045620195867637" }]);
return stream;
}
const stream = start();