By BDubs · AI Rook Trading Engine I run an algorithmic trading engine that depends on a real-time WebSocket feed from MMT (a crypto market data provider). For weeks, my bot kept going dark. No crash. No error in my logs. Just… silence. The WebSocket would die with a 1011 read_error, and I'd have to restart the whole thing. The root cause was a single number that was wrong by a factor of 2.5. The Symptom Every 5 to 6 minutes, the MMT WebSocket connection would close. The engine would reconnect, but the reconnect cycle burned through rate limits and caused gaps in candle data. In live trading, gaps in data mean missed signals. Missed signals mean missed entries. I assumed MMT was rate-limiting me. I assumed my code had a memory leak. I assumed the VPS network was unstable. It was none of those things. The Root Cause MMT's server closes idle WebSocket sessions after approximately 300 seconds (5 minutes). My engine had a "keepalive" mechanism that re-subscribed to the stats channel every 10 minutes: // BEFORE — keepalive fires every 10 minutes setInterval(() => { try { if (ws.readyState === ws.OPEN) { ws.send(JSON.stringify({ type: 'subscribe', channel: 'stats', exchange: EXCHANGE, symbol: SYMBOL, tf: TF })); } } catch(e) { /* non-fatal */ } }, 10 * 60 * 1000); // every 10 minutes Enter fullscreen mode Exit fullscreen mode The math is brutal: the server kills the connection at ~5 minutes. My keepalive fires at 10 minutes. The connection is already dead by then. The stats channel was chosen as the keepalive because it's lightweight — just funding rates, liquidation counts, and mark price. But re-subscribing to a channel on a dead socket is useless. What I needed was a keepalive that fires before the server's timeout, not after. The Fix Change one number: // AFTER — keepalive fires every 4 minutes (before 300s timeout) }, 4 * 60 * 1000); // every 4 min (MMT server closes idle sessions at ~5min) Enter fullscreen mode Exit fullscreen mode That's it. Six characters changed: 10 → 4. The keepalive now fires every 240 seconds, comfortably ahead of the server's ~300-second idle timeout. The connection stays alive. No more 1011 read_error. No more reconnect loops. Why This Is a Great Bug Smash Story Because it's the kind of bug that wastes days. When a WebSocket dies silently, you don't know if the problem is your code, your network, your server, or your provider. You start debugging in every direction except the one that matters. The fix was a single constant. The investigation could have been hours of tcpdump, proxy logs, and support tickets. Instead it was: read the provider docs, find the idle timeout, compare it to your keepalive interval. The difference between 10 * 60 * 1000 and 4 * 60 * 1000 is the difference between a trading engine that stays online and one that goes dark every five minutes. Lessons Always know your provider's idle timeout. If you're using WebSockets for anything critical, the first thing to check is how long the server keeps idle connections alive. Your keepalive interval must be shorter than the server's timeout. Not equal. Shorter. With margin. If the server kills at 300 seconds, fire at 240, not 300. Silent failures are the worst. A WebSocket 1011 read_error doesn't crash your process. It just stops working. Monitor connection state, not just process state. This fix was part of commit c12d52c in the Rook Engine — an open-source algorithmic trading system. The full diff is two lines.
MMT Killed My WebSocket Every 5 Minutes — A 6-Character Fix
Full Article
Original Source
Read the full article at Dev →KhanList aggregates and links to publicly available news content. We do not host full articles from third-party sources. Always verify important information with original sources.