Overview
TheuseVideoSync hook manages real-time video synchronization between all users in a room. It handles:
- Video playback state synchronization (play, pause, seek)
- Time drift correction to keep all viewers in sync
- Host-only controls with guest protection
- YouTube player state management
- Periodic sync checks for hosts
- Queued synchronization when player is not ready
Import
Parameters
The unique 6-character room identifier
Reference to the YouTube player component for controlling playback
Return Value
Returns an object with video control and synchronization functions:Synchronize the local player to match the provided state. Automatically queues sync if player is not ready.Parameters:
targetTime: Target playback time in secondsisPlaying: Whether video should be playing (true), paused (false), or no state change (null)timestamp: Server timestamp when the state was captured (for drift calculation)
Start periodic sync checks (every 5 seconds). Only hosts should call this. Emits current playback state to keep all viewers synchronized.
Stop periodic sync checks. Called when user is no longer host or leaves room.
Handle video play action. Host-only function that emits
play-video event with current time.Handle video pause action. Host-only function that emits
pause-video event with current time.Handle video seek action. Host-only function that emits
seek-video event with new time.Handle YouTube player state changes. Host-only function that automatically detects and emits play, pause, and seek events.Parameters:
state: YouTube player state constant (seeYT_STATES)
Set a new video URL for the room. Host-only function that emits
set-video event.Parameters:videoUrl: Full YouTube video URL
Callback for when a non-host attempts to control video. Can be used to show feedback to the user.
Usage Example
Sync Mechanism
Time Drift Calculation
The hook usescalculateCurrentTime utility to account for network latency and time drift:
Sync Thresholds
- Normal Sync: Syncs if time difference > 1.5 seconds
- Initial Sync: Always syncs when joining a room (queued sync)
- Feedback Loop Prevention: Skips sync for 500ms after user performs an action
Player Readiness
WhensyncVideo is called but the player is not ready:
- Sync request is queued
- Player readiness is checked every 50ms
- Once ready (up to 10 seconds), queued sync is executed
- This ensures sync works even when joining a room with video already playing
Periodic Sync Checks (Hosts Only)
Hosts emit sync checks every 5 seconds containing:- Current playback time
- Playing/paused state
- Timestamp for drift calculation
YouTube Player Integration
The hook handles YouTube player state transitions:Player States
State Change Handling
For hosts,handleYouTubeStateChange automatically:
- Detects seek operations by comparing time difference > 1 second
- Emits play/pause events on state change
- Handles seek during buffering
- Prevents duplicate events with action tracking
Host vs Guest Behavior
Host Capabilities
- Control video playback (play, pause, seek)
- Set new video URLs
- Emit periodic sync checks
- All control handlers are active
Guest Behavior
- Receive and apply sync updates
- Cannot control video directly
- Player controls should be disabled via
disabledprop - Automatically syncs to host’s playback state
Type Definitions
Socket Events
The hook interacts with these socket events: Emitted by hook:play-video:pause-video:seek-video:set-video:sync-check:
sync-update:video-played:video-paused:video-seeked:video-set: