Electing one WebSocket leader across browser tabs with the Web Locks API
A developer building a realtime issue tracker with IndexedDB and a WebSocket needed only one connection per browser when the same workspace was open in many tabs. They use navigator.locks: every tab requests the same named lock, and the browser grants it to one tab, which holds leadership until it closes. When the leader closes, the browser releases the lock and the next queued tab takes over, so no explicit failover code is needed. Tabs coordinate through a BroadcastChannel using seven message types, with the leader writing data to IndexedDB before announcing it to followers. Followers that detect a version gap reload from storage rather than repairing it themselves.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.


Discussion (0)
Log in to join the discussion and vote.
Log in