You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
From PR #85 review — currently all connected clients independently auto-save the game to the database on a 60s debounce. With multiple users connected, this means redundant writes to Supabase.
Proposal
Implement a leader-election mechanism using Supabase's Realtime Presence API so that only the first/oldest connected client is responsible for saving to the database. This models the peer-to-peer connection as a coordinated primary/secondary election.
Considerations
Use Supabase Presence to track connected peers and determine the "leader" (e.g., lowest SENDER_ID or first to join)
Only the leader triggers saveRemoteGame on the auto-save debounce
If the leader disconnects, a new leader is elected and takes over save responsibility
Non-leader clients still broadcast patches via realtime — they just don't write to the DB
Edge case: if the leader crashes without a clean disconnect, presence should eventually time out and trigger re-election
Context
From PR #85 review — currently all connected clients independently auto-save the game to the database on a 60s debounce. With multiple users connected, this means redundant writes to Supabase.
Proposal
Implement a leader-election mechanism using Supabase's Realtime Presence API so that only the first/oldest connected client is responsible for saving to the database. This models the peer-to-peer connection as a coordinated primary/secondary election.
Considerations
SENDER_IDor first to join)saveRemoteGameon the auto-save debounceRelated
src/games/sync/useRealtimeGameSync.ts—scheduleAutoSave()function