The Weak Spot in Leader Election
When users open multiple tabs of a real-time web application — whether it is an analytical dashboard, trading platform, or collaborative workspace — each tab typically attempts to spin up its own WebSocket. Multiplying active connections per client wastes client memory and dramatically inflates infrastructure costs on your back-end servers.
For years, the standard engineering answer has been leader election via navigator.locks paired with a BroadcastChannel. One tab claims the lock, hosts the active socket, and broadcasts payloads to sibling tabs. While functional, this design harbors an inherent structural flaw: the connection is tied directly to a single browser window. If the user closes the leader tab, the lock drops, a new leader is elected, and an entirely new WebSocket handshake must take place. During that brief reconnect window, live streams pause, leaving users with stale data or UI flickers.
Decoupling the Connection with SharedWorker
Rather than managing complicated tab handoffs, modern client-side architecture allows us to move network ownership completely outside of the tab lifecycle. By utilizing a SharedWorker, your application runs an independent execution thread shared across all tabs under the same origin.
Core Architectural Advantages:
- True Tab Independence: The worker script spins up once on the first tab request and persists as long as at least one tab remains alive.
- Seamless Messaging: Each connected tab receives an isolated
MessagePort, routing outbound commands and inbound payloads through a single centralized pipeline. - Zero Reconnect Gaps: Closing any tab — including the one that opened first — has zero impact on the active socket. There are no elections, handovers, or connection drops.
Handling Connection Lifecycles Cleanly
While leader election automatically releases locks upon tab termination, a SharedWorker does not fire an explicit disconnect event when a tab closes. Abandoned ports simply linger in the worker's memory until garbage collected, which prevents an accurate headcount of active tabs.
To maintain clean connection management and ensure the socket terminates when all tabs are closed, implement an explicit shutdown handshake:
- Attach an event listener to
pagehideinside each tab, dispatching a lightweight exit message via the port. - Remove the disconnected port from the worker's active connection registry.
- Terminate the upstream WebSocket cleanly as soon as the active port counter drops to zero.
Business Impact and Engineering Roadmap
Shifting from tab-bound sockets to a SharedWorker pipeline directly optimizes server connection pools, eliminates client-side latency spikes, and delivers the frictionless experience users expect from enterprise-grade web applications.
Browser support is rock solid across all modern desktop browsers and modern mobile Safari. For scenarios involving legacy environments, the optimal engineering strategy is progressive enhancement: deploy SharedWorker as your primary transport layer, falling back to Web Locks only when the worker context is unavailable.
Services on this topic
Landing page turnkey
Selling landing page turnkey: offer, structure, design, development, SEO foundation and ad launch. From 10,000 UAH.
from 10,000 UAH
Corporate website
Corporate website turnkey: company presentation, service catalog, CRM, admin panel, SEO structure. From 18,000 UAH.
from 18,000 UAH
Online store
Online store turnkey: product catalog, cart, online payment, CRM and warehouse integrations, SEO structure. From 25,000 UAH.
from 25,000 UAH
Need this for your project?
Development turnkey: analysis → implementation → launch → support. Tell us about your task — we will reply with an estimate.
Relevant articles
Robust Support Chat Architecture: Presence Leases, Reconnects, and Message Guarantees
Learn how to architect resilient B2B support and dispatch chats with server-managed presence, monotonic event streams, and reliable reconnect logic.
Fixing INP on Shopify Stores: A Practical Guide to Faster Storefront Interactions
Learn how to optimize Interaction to Next Paint (INP) on your Shopify store, eliminate UI freezes, and drive higher e-commerce conversion rates.
Eliminating Anchoring Bias in Sprint Estimates: Real-Time Architecture for Team Planning
Discover how WebSocket architecture and zero-knowledge voting patterns prevent cognitive bias and streamline agile sprint estimation.
Graph Analytics and AI: Slashing Downtime and Resolving Outages in Seconds
Discover how combining graph databases with AI allows engineering teams to identify the root cause of complex cascading outages in seconds instead of hours.