···121121 - sends invitation+registration token to server
122122 - added to the realm
123123 - go to subsequent runs
124124+125125+* WebRTC Full-Mesh Implementation Plan
126126+127127+** Overview
128128+129129+Implement a full-mesh WebRTC system where every client in a realm establishes direct
130130+peer-to-peer connections with all other clients. The existing WebSocket infrastructure
131131+serves as the signaling channel, leveraging the newly refactored broadcast protocol.
132132+133133+** Architecture Components
134134+135135+*** Protocol Extensions (src/common/protocol/messages-rtc.js)
136136+137137+Create new WebRTC message schemas:
138138+- rtc.offer - SDP offer with connectionId
139139+- rtc.answer - SDP answer with connectionId
140140+- rtc.ice-candidate - ICE candidate exchange
141141+- rtc.peer-state - Connection state updates
142142+- rtc.request-connection - Initiate connection with polite flag
143143+- rtc.peer-joined - Server message when peer joins (includes member list)
144144+- rtc.peer-left - Server message when peer leaves
145145+146146+*** Server-Side Updates
147147+148148+**** broadcastToRealm Function Enhancement
149149+150150+Update the function signature to:
151151+- Take complete messages instead of payloads
152152+- Add skipSelf flag (default true)
153153+- For rtc.peer-joined, set skipSelf=false to include sender
154154+155155+**** Handler Updates (handler-realm.js)
156156+157157+- On client join: Broadcast rtc.peer-joined to ALL members (including self)
158158+- On client leave: Broadcast rtc.peer-left to remaining members
159159+- Existing broadcast mechanism handles WebRTC signaling perfectly
160160+161161+*** WebRTC Utilities (src/common/webrtc.js)
162162+163163+Core utilities for WebRTC:
164164+- RTC_CONFIG with STUN servers
165165+- DATA_CHANNEL_CONFIG for reliable messaging
166166+- PerfectNegotiation class for glare-free negotiation
167167+- ConnectionHealthMonitor for ping/pong health checks
168168+169169+*** Client WebRTC Manager (src/client/webrtc-manager.js)
170170+171171+Main orchestrator that:
172172+- Manages all peer connections
173173+- Handles incoming RTC messages
174174+- Routes signaling between peers
175175+- Emits events for UI updates
176176+- Provides public API for sending messages
177177+178178+*** Client Peer Connection (src/client/peer-connection.js)
179179+180180+Individual peer connection handler:
181181+- RTCPeerConnection lifecycle management
182182+- Perfect negotiation implementation
183183+- Data channel setup and messaging
184184+- Health monitoring with ping/pong
185185+- Automatic reconnection with exponential backoff
186186+- Connection state tracking
187187+188188+*** UI Components
189189+190190+**** PeerList Component
191191+- Shows all realm members
192192+- Connection status indicators
193193+- Real-time state updates
194194+195195+**** MessageInterface Component
196196+- Send messages via WebRTC or server broadcast
197197+- Display incoming messages
198198+- Mode selection (P2P vs server relay)
199199+200200+** Connection Flow
201201+202202+*** Initial Join
203203+1. Client authenticates via WebSocket
204204+2. Server sends realm.status
205205+3. Server broadcasts rtc.peer-joined to ALL members
206206+4. Client sees own join message with member list
207207+5. Client initializes WebRTCManager
208208+6. Client connects to all existing members
209209+210210+*** Peer-to-Peer Connection
211211+1. Initiator creates RTCPeerConnection (polite=true)
212212+2. Creates data channel, triggering negotiation
213213+3. Sends offer via realm.broadcast to target peer
214214+4. Target creates RTCPeerConnection (polite=false)
215215+5. Exchanges answer and ICE candidates
216216+6. Data channel opens, health monitoring starts
217217+218218+*** Reconnection
219219+1. Health monitor detects issues or connection drops
220220+2. Exponential backoff timer starts
221221+3. New connection attempt with fresh connectionId
222222+4. ICE restart or full renegotiation
223223+224224+** Key Design Decisions
225225+226226+*** Perfect Negotiation Pattern
227227+Prevents glare when both peers try to connect simultaneously by using
228228+polite/impolite roles.
229229+230230+*** Health Monitoring
231231+Proactive ping/pong messages detect connection issues before browser APIs,
232232+enabling faster recovery.
233233+234234+*** Connection ID Tracking
235235+Each connection attempt has unique ID to ensure offer/answer pairs match
236236+during concurrent connections.
237237+238238+*** Leveraging Existing Infrastructure
239239+WebRTC signaling is just another payload type in the existing broadcast
240240+system - no new server complexity.
241241+242242+** Implementation Order
243243+244244+*** Phase 1: Core Infrastructure
245245+1. Create protocol message schemas
246246+2. Update broadcastToRealm function
247247+3. Add peer join/leave broadcasts
248248+4. Create WebRTC utilities module
249249+250250+*** Phase 2: Client Connection Management
251251+1. Implement WebRTCManager
252252+2. Create PeerConnection class
253253+3. Add perfect negotiation
254254+4. Implement health monitoring
255255+256256+*** Phase 3: UI Integration
257257+1. Update main app to initialize WebRTC
258258+2. Create PeerList component
259259+3. Add MessageInterface with dual modes
260260+4. Style connection indicators
261261+262262+*** Phase 4: Robustness
263263+1. Add reconnection logic
264264+2. Implement ICE restart
265265+3. Handle edge cases
266266+4. Add comprehensive error handling
267267+268268+** Testing Strategy
269269+270270+*** Unit Tests
271271+- Perfect negotiation scenarios
272272+- Health monitoring logic
273273+- Message routing
274274+275275+*** Integration Tests
276276+- Full connection flow with mocks
277277+- Signaling message flow
278278+- State management
279279+280280+*** E2E Tests
281281+- Real browser testing
282282+- Network condition simulation
283283+- Multi-peer scenarios
284284+285285+*** Load Tests
286286+- Mesh scalability limits
287287+- Message throughput
288288+- Connection stability