A TypeScript-based WebSocket server for real-time synchronization of music playback in the Jukebox Duo application.
- 🎵 Real-time audio synchronization
- 👥 Room-based user management
- 📝 Queue management with real-time updates
- 🔄 Video/song change synchronization
- 🛡️ Type-safe Socket.IO events
- ⚡ High-performance WebSocket connections
- TypeScript - Type-safe development
- Socket.IO - Real-time bidirectional communication
- Node.js - Server runtime
- CORS - Cross-origin resource sharing
websocket-server/
├── src/
│ ├── config/
│ │ └── index.ts # Configuration management
│ ├── services/
│ │ └── socketService.ts # Socket.IO event handlers
│ ├── types/
│ │ └── index.ts # TypeScript type definitions
│ └── index.ts # Main server entry point
├── package.json
├── tsconfig.json
├── env.example
└── README.md
-
Install dependencies:
npm install
-
Set up environment variables:
cp env.example .env # Edit .env with your configuration -
Build the project:
npm run build
Start development server:
npm run devThis will start the server with hot reload using ts-node-dev.
Build and start production server:
npm run build
npm start| Variable | Description | Default |
|---|---|---|
PORT |
Server port | 3001 |
NODE_ENV |
Environment mode | development |
FRONTEND_URL |
Frontend URL for CORS | http://localhost:3000 |
join-room- Join a music roomsync-ping- Synchronization pingsync-command- Play/pause commandschange-video- Change current video/songqueue-updated- Add item to queuequeue-removed- Remove item from queue
sync-pong- Synchronization responsesync-command- Broadcast play/pause commandsvideo-changed- Notify video changequeue-updated- Notify queue updatesqueue-removed- Notify queue removals
- Connect your repository to Railway
- Set environment variables in Railway dashboard
- Deploy automatically
- Create a new Web Service
- Connect your repository
- Set build command:
npm run build - Set start command:
npm start - Configure environment variables
- Create a new app
- Connect your repository
- Set build command:
npm run build - Set run command:
npm start - Configure environment variables
class SocketService {
// Get room statistics
getRoomStats(roomId: string): { roomSize: number; connectedClients: number }
// Broadcast to all clients
broadcastToAll(event: keyof ServerToClientEvents, data: any): void
// Broadcast to specific room
broadcastToRoom(roomId: string, event: keyof ServerToClientEvents, data: any): void
}The server includes comprehensive error handling:
- Graceful shutdown on SIGTERM/SIGINT
- Uncaught exception handling
- Unhandled rejection handling
- Connection error logging
The server provides detailed logging for:
- Client connections/disconnections
- Room join/leave events
- Command synchronization
- Queue updates
- Error conditions
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
MIT License