mirror of
https://github.com/Shik3i/KoalaSync.git
synced 2026-07-26 12:08:15 +00:00
3.9 KiB
3.9 KiB
KoalaSync Architecture
This document describes the communication flows and internal logic of the KoalaSync system.
1. Extension Startup & Connection
- Initialization: On startup,
background.jsreads settings (Server URL, Username, Last Room) fromchrome.storage.sync. - WebSocket Handshake:
- Background creates a
new WebSocketto/socket.io/?EIO=4&transport=websocket&version=1.0.0. - Server performs security checks:
- IP Rate Limit: Checks if the IP has exceeded connection limits.
- Protocol Version: Client must match the server's protocol (currently
1.0.0).
- Server responds with Engine.IO handshake (
0) and the client joins the namespace (40).
- Background creates a
- Room Join: Background emits
JOIN_ROOMcontainingroomId,password,peerId, andusername. - Deduplication: If a user joins with a
peerIdthat already has an active socket, the server kills the old socket to prevent "Ghost Peers".
2. Media Event Synchronization
When a user interacts with a video:
- Detection:
content.jslistens to native events (play,pause,seeked) on the<video>element. - Prevention of Loops: Uses
lastTargetStateto distinguish between user actions and programmatic actions triggered by the extension. - Reporting:
content.jssends aCONTENT_EVENTtobackground.js. - Relay: The Server forwards the event to all other peers in the room.
- Execution: Remote peers receive the command and call
video.play(),video.pause(), orvideo.currentTime = targetTime.
3. Two-Phase Force Sync
Ensures all peers are frame-perfect and buffered before resuming:
- Prepare: Initiator sends
FORCE_SYNC_PREPAREwith the target timestamp. - Buffer: Peers seek and pause. Once buffered (
readyState >= 3), they send aFORCE_SYNC_ACK. - Execute: Once the Initiator collects ACKs (or after a 5s timeout), they send
FORCE_SYNC_EXECUTE. - Resume: All peers call
play()simultaneously.
4. Peer Lifecycle & Dual Heartbeat
To maintain a clean room state and eliminate "Ghost Peers":
- Session Heartbeat (Background): Every 30 seconds,
background.jssends an "I'm alive" signal to the server. This keeps you in the room even if no video is playing. - Video Heartbeat (Content): Every 15 seconds,
content.jssends current playback metadata (time, title, state) if a video is found. - Server Pruning: The server runs a "Reaper" every 2 minutes. If a peer has sent zero activity (no events and no heartbeats) for 5 minutes, they are forcefully disconnected.
- Immediate Cleanup: Rooms are deleted instantly when the last peer leaves or disconnects.
5. Security & Stability
- Service Worker Lifecycle: Uses
chrome.alarmsto prevent the Manifest V3 service worker from suspending while in an active room. - Rate Limiting: Server-side per-socket and per-IP rate limits to prevent sync-spamming or DoS.
- Noise Filtering: Uses a curated blacklist of domains (Search Engines, Social Media) to declutter the "Target Tab" selector in the popup.
- Diagnostics: A "Dev" tab provides real-time access to the underlying
<video>state (readyState,paused,currentTime) for easier troubleshooting.
6. Constant Synchronization & Consistency
To maintain a "Single Source of Truth" across the server and extension without using a bundler:
- Relay Server & Extension Modules:
background.jsandpopup.jsimport constants directly fromshared/constants.js. - Content Scripts: To ensure zero-latency execution,
content.jsuses a manual mirror ofEVENTS. - Synchronization: The
./scripts/sync-constants.shscript ensures that theshared/folder within theextension/directory is kept up-to-date with the rootshared/source. - Verification: Any protocol change requires a manual verification sweep across all three constant locations (Shared, Server, and Content Script Mirror).