# KoalaSync — How It Works (Step-by-Step) This guide walks through the complete user flow of KoalaSync, from creating a room to synchronized playback. It is designed for **store reviewers**, **end-users**, and **manual testers** to understand exactly what happens at each step, what data is sent, and where it goes. --- ## Step 1: Installing the Extension 1. Download the extension from the [Releases](https://github.com/Shik3i/KoalaSync/releases) page (or install from the Chrome Web Store / Firefox Add-ons). 2. The extension adds a small icon to your browser toolbar. 3. On first install, a unique 8-character **Peer ID** is generated locally and stored in `chrome.storage.local`. This ID is never sent to any external service — it only travels to the relay server when you join a room. > **What's stored locally**: `peerId` (8-char hex), `username` (customizable, defaults to a readable adjective-noun pair), `serverUrl`, `filterNoise` preference. All stored via `chrome.storage.sync` and `chrome.storage.local`. --- ## Step 2: Connecting to the Relay Server When you open the extension popup (with saved room credentials) or when a saved room configuration exists from a previous session, the background service worker connects to the relay server: 1. **WebSocket Handshake** (on demand): `background.js` opens a WebSocket to `wss://syncserver.koalastuff.net/socket.io/?EIO=4&transport=websocket` only when needed (popup opened or active room). 2. **Security Checks** (server-side): - The server checks the client's **IP rate limit** (max 10 connections per 60 seconds). - The server validates the **authentication token** (hardcoded in `shared/constants.js`) to verify this is a legitimate KoalaSync client. - The server checks the **extension version** against `MIN_VERSION` to reject outdated clients. 3. **Connection Established**: The server responds with an Engine.IO handshake (`0{...}`), followed by a Socket.IO namespace join (`40`). The connection status dot in the popup turns green. > **Data sent to server**: `token` (authentication), `version` (e.g., `1.3.1`). No personal data is transmitted during connection. --- ## Step 3: Creating a Room Click **"Create Room"** in the popup's Room tab: 1. The extension generates a random Room ID (e.g., `happy-koala-42`) and a random 6-character password. 2. Room IDs are restricted to `[a-zA-Z0-9-]` (alphanumeric + hyphens only). 3. The extension emits a `JOIN_ROOM` event to the server. > **Data sent in `JOIN_ROOM`**: > ```json > { > "roomId": "happy-koala-42", > "password": "x7k2m9", > "peerId": "a1b2c3d4", > "username": "MyName", > "tabTitle": "YouTube - My Video", > "protocolVersion": "1.0.0" > } > ``` 4. **Server-side processing**: - All fields are **sanitized**: `roomId` is stripped of invalid characters and clamped to 64 chars; `peerId` clamped to 16 chars; `password` clamped to 128 chars; `username` clamped to 30 chars. - The server **hashes the password** with a keyed SHA-256 HMAC and stores only that hash in RAM (the plaintext is never stored). - A new room object is created in memory with the peer's data. - The server responds with `ROOM_DATA` containing the list of peers in the room. 5. **Popup updates**: The Room tab switches to the "Active Room" view, showing your Room ID and an invitation link. --- ## Step 4: Sharing an Invitation Link Click the **📋 Copy** button next to the invite link: 1. The extension constructs a URL in this format: ``` https://sync.koalastuff.net/join.html#join:::: ``` - `serverFlag`: `0` for official server, `1` for custom server. - `encodedServerUrl`: Only populated if using a custom server. 2. **Important**: The room credentials are in the **URL hash** (`#`), which means they are **never sent to the web server** — the hash fragment stays entirely in the browser. The landing page server never sees your room ID or password. 3. Send this link to your friend via any messaging app. --- ## Step 5: Your Friend Opens the Invitation Link When your friend opens the link in their browser: 1. **`join.html` loads** on `sync.koalastuff.net`. The page displays "INVITATION DETECTED" with the Room ID. 2. **Extension detection**: The page checks for `document.documentElement.dataset.koalasyncInstalled`, which is set by `bridge.js` (a content script injected only on `sync.koalastuff.net`). 3. **If the extension IS installed**: - The page shows "Joining room automatically..." - After 500ms, the page dispatches a `KOALASYNC_JOIN_REQUEST` custom DOM event with `{ roomId, password, useCustomServer, serverUrl }`. - `bridge.js` catches this event and forwards it to `background.js` via `chrome.runtime.sendMessage`. - `background.js` stores the credentials in `chrome.storage.sync` and emits `JOIN_ROOM` to the server. - The server validates the password against the stored keyed SHA-256 HMAC hash. - On success, the server responds with `ROOM_DATA` and broadcasts `PEER_STATUS { status: 'joined' }` to all existing peers. - The join page updates to show "✅ Successfully joined!". 4. **If the extension is NOT installed**: - The page shows download links (Chrome Web Store / GitHub). - The user installs the extension, returns to the link, and the flow continues from step 3. --- ## Step 6: Selecting a Video Tab Both users now need to select which browser tab contains the video to sync: 1. Open a video on any website (YouTube, Twitch, Netflix, etc.). 2. In the extension popup → **Sync** tab → use the **"Target Tab"** dropdown. 3. The dropdown lists all open tabs, filtered to exclude noise (search engines, social media — configurable via Settings). 4. Tabs with a **matching video title** are highlighted with a ⭐ prefix for easy identification. 5. Selecting a tab causes `background.js` to set `currentTabId` and inject `content.js` into that tab via `chrome.scripting.executeScript`. > **What `content.js` does on injection**: Finds the first `