From 0281180bd1a9c543336fbcdcb6d697b8832a5ae4 Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Mon, 28 Jul 2025 14:30:21 -0500 Subject: [PATCH] Fix continuous longpoll requests due to Phoenix LiveView bug MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added workaround to clear the longpoll fallback timer after WebSocket connection is established. The issue was causing hundreds of unnecessary longpoll requests per second even when WebSocket was working properly. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- assets/js/app.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/assets/js/app.js b/assets/js/app.js index ea10bbf..f122f9c 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -321,6 +321,27 @@ window.addEventListener("phx:page-loading-stop", (_info) => topbar.hide()); // connect if there are any LiveViews on the page liveSocket.connect(); +// Workaround for Phoenix LiveView bug where fallback timer isn't cleared +// after successful WebSocket connection +window.addEventListener("phx:live_socket:connect", (info) => { + const socket = liveSocket.socket; + if (socket && socket.fallbackTimer) { + clearTimeout(socket.fallbackTimer); + socket.fallbackTimer = null; + console.log("[LiveSocket] Cleared fallback timer after successful connection"); + } +}); + +// Also check periodically in case the event doesn't fire +setTimeout(() => { + const socket = liveSocket.socket; + if (socket && socket.isConnected() && socket.fallbackTimer) { + clearTimeout(socket.fallbackTimer); + socket.fallbackTimer = null; + console.log("[LiveSocket] Cleared lingering fallback timer"); + } +}, 5000); + // expose liveSocket on window for web console debug logs and latency simulation: // >> liveSocket.enableDebug() // >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session