System Architecture
Watch N Chill is built on a custom Next.js server architecture with Socket.IO for real-time communication, Redis for state persistence, and React for the frontend.The application uses a custom Next.js server instead of the standard Next.js server to integrate Socket.IO for bidirectional real-time communication.
Architecture Diagram
Core Components
Custom Next.js Server
Custom Next.js Server
The application uses a custom Node.js HTTP server that integrates both Next.js and Socket.IO:This approach allows Socket.IO to share the same HTTP server as Next.js, enabling real-time features.
server.ts
Socket.IO Real-Time Layer
Socket.IO Real-Time Layer
Socket.IO provides bidirectional event-based communication between clients and server:Key Features:
- WebSocket with fallback to polling
- Room-based event broadcasting
- Connection state management
- Automatic reconnection
- Room management (create, join, leave)
- Video control (play, pause, seek, sync)
- Chat messaging (send, typing indicators)
- User management (promote to host)
Redis State Management
Redis State Management
Redis serves as the single source of truth for room state:Data Structures:
room:{roomId}- Complete room state (users, video, metadata)chat:{roomId}- List of last 20 chat messagesactive-rooms- Set of all active room IDsrl:{ip}- Rate limit counters per IP
- Rooms: 24 hours (auto-cleanup)
- Chat: 24 hours
- Rate limits: 1 minute to 1 hour
React Frontend Architecture
React Frontend Architecture
The frontend uses Next.js App Router with a component-driven architecture:Page Structure:
/- Landing page/create- Create room form/join- Join room form/room/[roomId]- Main room interface
- React Context for Socket.IO connection
- Custom hooks for room and video sync logic
- Local component state for UI
- Session storage for user data
Data Flow
Room Creation Flow
Video Sync Flow
Technology Stack
Backend
- Runtime: Node.js
- Framework: Next.js 14+ (Custom Server)
- WebSocket: Socket.IO v4
- Validation: Zod schemas
- Database: Redis (Upstash)
Frontend
- Framework: React 18+ / Next.js 14+
- Routing: App Router
- Styling: Tailwind CSS
- State: React Context + Hooks
- WebSocket: Socket.IO Client
Key Design Decisions
- Custom Server
- Redis State
- Validation Strategy
- Rate Limiting
Why Custom Server?Next.js serverless functions don’t support WebSocket connections. A custom server allows:
- Long-lived Socket.IO connections
- Shared state between HTTP and WebSocket
- Fine-grained control over server lifecycle
- Custom middleware (rate limiting)
- Cannot deploy to Vercel
- Need to manage server process
- Deployed on platforms like Render, Railway, or AWS
Scalability Considerations
Future Improvements:- Redis Cluster for high availability
- Socket.IO adapter for multi-instance (Redis adapter)
- Load balancer with sticky sessions
- Separate WebSocket and HTTP servers
Next Steps
Backend Deep Dive
Explore server implementation, Socket.IO handlers, and Redis repositories
Frontend Architecture
Learn about React hooks, component structure, and state management
Real-Time Sync
Deep dive into video synchronization mechanism and drift correction
Development Guide
Set up your development environment and start contributing