The timer system consists of server-side and client-side components working together through WebSockets for real-time updates.
-
Server-Side Timer Logic (
server/routes.ts)- Manages timer state in the database
- Broadcasts timer updates to connected clients
- Handles timer phase transitions
- Processes timer commands (start/reset)
-
WebSocket Communication
- Server establishes WebSocket server on
/wspath - Clients connect and receive real-time updates
- Initial state sent on connection
- Periodic ticks update time remaining
- Server establishes WebSocket server on
-
Global Timer State (
client/src/lib/timer-state.ts)- Centralized state management for the client
- Handles time synchronization with server
- Manages phase transitions
-
Timer UI Components
SafeZoneTimer- Visual display componentTimerDetails- Admin panel component- Both consume global timer state
The timer has four phases:
green: Initial phaseyellow: Middle phasewarning: Final phasestopped: Inactive state
- Timer ticks process at 100ms intervals but only update when necessary
- Phase transitions occur automatically when timeLeft reaches zero
- Updates broadcast to all connected WebSocket clients
- All timer data stored in database via storage interface
- Server maintains a Set of connected clients
- Updates broadcast to all connected clients
- Includes ping mechanism to keep connections alive
- Initial state sent on new connections
- Server is the source of truth for time
- Client estimates server time offset for smooth animations
- Animation frames handle visual countdown between WebSocket updates
- Authenticated admin users can start/reset the timer
- Update timer configuration (base times, extensions)
- Timer state changes immediately broadcast to all clients
-
Modify Timer Configuration: Edit the schema in
shared/schema.tsif you need to add new timer properties. -
Change Phase Logic: Update the phase transition logic in the interval handler in
server/routes.ts. -
UI Styling: Phase styles are defined in
safe-zone-timer.tsxwith color mappings. -
Add New Features:
- Server-side changes: Modify
routes.tsto add new endpoints - Client-side changes: Update the global timer state in
timer-state.ts
- Server-side changes: Modify
-
Authentication: Authentication requirements for timer control are in
routes.ts. Timer operations require admin authentication.
- Admin triggers timer action via API
- Server updates database
- WebSocket broadcasts update to all clients
- Clients update global timer state
- UI components react to state changes
- Animation frames provide smooth countdown