Flutter Web in Two Tabs: Both Polling, Both Wrong
Your app works. Then a user opens it in a second tab, and things start going wrong in ways that never show up in testing. Both tabs poll the server, so your request volume doubles for one user. Both hold a WebSocket, so your connection count is wrong and your server thinks you have twice the traffic you do. Both raise the same desktop notification, so it appears twice. The user signs out in one…
Two tabs open an app simultaneously, and problems arise in ways that weren't discovered during testing. Each tab polls the server, causing the request volume to double. Both tabs maintain WebSocket connections, causing the server to think it's handling twice the traffic. The same desktop notification is raised twice, and if a user signs out in one tab, the other tab continues without any issue, still displaying data and refreshing it.
Users frequently open second tabs, and most web apps fail to handle this scenario. The platform provides a BroadcastChannel for messaging between same-origin browsing contexts, but it falls short in several aspects. There's no built-in way to know how many tabs are open, and there's no inherent mechanism to ensure only one tab performs the workload.
Developers must implement a solution for these issues, which involves leader election and handling tab closures appropriately.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.