# KoalaSync Architecture This document describes the communication flows and internal logic of the KoalaSync system. ## 1. Extension Startup & Connection - **Initialization**: On startup, `background.js` reads settings (Server URL, Last Room) from `chrome.storage.sync`. - **WebSocket Handshake**: 1. Background creates a `new WebSocket` to `/socket.io/?EIO=4&transport=websocket&version=1.0.0&token=...`. 2. Server performs security checks: - **IP Rate Limit**: Checks if the IP has exceeded 10 connections/min. - **Auth Token**: If a server token is required, it must match. - **Version Check**: Client version must be `>= MIN_VERSION`. 3. Server responds with an Engine.IO handshake (packet type `0`). 4. Background sends `40` to join the default Socket.IO namespace. 5. Server responds with `40`. - **Room Join**: If a Room ID is stored, Background emits `42["join_room", {...}]`. - **Reconnect Logic**: If the connection drops, Background uses an exponential backoff (1s, 2s, 4s... max 30s) to reconnect. ## 2. Media Event Synchronization When a user presses Play/Pause in a synchronized tab: 1. **Detection**: `content.js` listens to native `play`/`pause` events on the `