Overview
TheuseRoom hook is the primary hook for managing room functionality in Watch N Chill. It handles:
- Room state management and socket event synchronization
- User authentication and session management
- Real-time chat messaging and typing indicators
- User promotion and host controls
- Error handling and connection state
Import
Parameters
The unique 6-character room identifier (uppercase letters and numbers)
Return Value
Returns an object with the following properties:State Properties
The current room object containing:
id: Room IDhostId: Host user IDhostName: Host usernamehostToken: Host authentication tokenvideoUrl: Current video URL (optional)videoType: Video platform type ('youtube'ornull)videoState: Video playback state (playing, currentTime, duration, lastUpdateTime)users: Array of User objects in the roomcreatedAt: Room creation timestamp
The current user object containing:
id: User ID (UUID)name: UsernameisHost: Whether the user is a hostjoinedAt: Timestamp when user joined
Array of chat messages with:
id: Message ID (UUID)userId: Sender’s user IDuserName: Sender’s usernamemessage: Message content (1-1000 characters)timestamp: Message timestamproomId: Room IDisRead: Whether the message has been read
Array of users currently typing:
userId: User IDuserName: Usernametimestamp: Timestamp of typing event
Room-level error message (e.g., room not found, join failed)
Video synchronization or socket error message (auto-clears after 5 seconds)
Whether the room join process is in progress
Whether to display the guest information banner (shown when non-hosts join a room with video)
Whether to display the host controls dialog
Whether to display the “copied” feedback (shown for 2 seconds after copying room ID)
Action Functions
Show or hide the guest information banner
Show or hide the host controls dialog
Show or hide the copied feedback indicator
Promote a user to host status (only available to current hosts)Parameters:
userId: The UUID of the user to promote
Send a chat message to the roomParameters:
message: Message content (1-1000 characters)
Notify other users that current user started typing
Notify other users that current user stopped typing
Mark all messages in the current chat as read
Copy the room ID to clipboard and show feedback for 2 seconds
Share the room URL using the Web Share API (or copy to clipboard as fallback)
Usage Example
State Management
The hook manages state through several mechanisms:Socket Event Handlers
The hook listens to these socket events:room-joined: Successfully joined room, updates room and user stateuser-joined: Another user joined, adds to user listuser-left: User left room, removes from user list and typing indicatorsuser-promoted: User promoted to host, updates user permissionsvideo-set: Video URL changed, updates room video statenew-message: New chat message received, adds to messages arrayuser-typing: User started typing, adds to typing indicatorsuser-stopped-typing: User stopped typing, removes from typing indicatorsroom-error: Room-level error occurred (e.g., all hosts left)error: Socket-level error occurred (e.g., authentication failed)
Session Management
The hook integrates withroomSessionStorage to handle:
- Room Creator Flow: Automatically joins with host credentials when navigating from create page
- Join Flow: Uses stored username when navigating from join page
- Direct Access: Prompts for username if accessing room URL directly without credentials
Automatic Cleanup
When the component unmounts, the hook automatically:- Emits a
leave-roomevent to notify the server - Removes socket event listeners to prevent memory leaks
- Clears internal state and prevents duplicate join attempts
Error Handling
The hook implements sophisticated error handling:- Error Deduplication: Prevents showing the same error multiple times within 5 seconds
- Context-Aware Errors: Ignores certain errors when user is already successfully in room
- Auto-Clear: Sync errors automatically clear after 5 seconds
- Room Closure: Displays toast notification and redirects home when all hosts leave
Type Definitions
Related Hooks
- useVideoSync - Manages video playback synchronization
- useCreateRoom - Handles room creation flow
- useJoinRoom - Handles room joining flow