Files
spotlightcam/backend/src/server.js
Radosław Gierwiało 75cb4b16e7 feat: implement real-time chat with Socket.IO
Implemented WebSocket-based real-time messaging for both event rooms and private match chats using Socket.IO with comprehensive test coverage.

Backend changes:
- Installed socket.io@4.8.1 for WebSocket server
- Created Socket.IO server with JWT authentication middleware
- Implemented event room management (join/leave/messages)
- Added active users tracking with real-time updates
- Implemented private match room messaging
- Integrated Socket.IO with Express HTTP server
- Messages are persisted to PostgreSQL via Prisma
- Added 12 comprehensive unit tests (89.13% coverage)

Frontend changes:
- Installed socket.io-client for WebSocket connections
- Created socket service layer for connection management
- Updated EventChatPage with real-time messaging
- Updated MatchChatPage with real-time private chat
- Added connection status indicators (● Connected/Disconnected)
- Disabled message input when not connected

Infrastructure:
- Updated nginx config to proxy WebSocket connections at /socket.io
- Added Upgrade and Connection headers for WebSocket support
- Set long timeouts (7d) for persistent WebSocket connections

Key features:
- JWT-authenticated socket connections
- Room-based architecture for events and matches
- Real-time message broadcasting
- Active users list with automatic updates
- Automatic cleanup on disconnect
- Message persistence in database

Test coverage:
- 12 tests passing (authentication, event rooms, match rooms, disconnect, errors)
- Socket.IO module: 89.13% statements, 81.81% branches, 91.66% functions
- Overall coverage: 81.19%

Phase 1, Step 4 completed. Ready for Phase 2 (Core Features).
2025-11-12 22:42:15 +01:00

49 lines
1.3 KiB
JavaScript

require('dotenv').config();
const http = require('http');
const app = require('./app');
const { testConnection, disconnect } = require('./utils/db');
const { initializeSocket } = require('./socket');
const PORT = process.env.PORT || 3000;
async function startServer() {
// Test database connection
await testConnection();
// Create HTTP server
const server = http.createServer(app);
// Initialize Socket.IO
initializeSocket(server);
server.listen(PORT, '0.0.0.0', () => {
console.log('=================================');
console.log('🚀 spotlight.cam Backend Started');
console.log('=================================');
console.log(`Environment: ${process.env.NODE_ENV || 'development'}`);
console.log(`Server running on port: ${PORT}`);
console.log(`Health check: http://localhost:${PORT}/api/health`);
console.log('=================================');
});
return server;
}
startServer().catch((err) => {
console.error('Failed to start server:', err);
process.exit(1);
});
// Graceful shutdown
process.on('SIGTERM', async () => {
console.log('SIGTERM received, shutting down gracefully...');
await disconnect();
process.exit(0);
});
process.on('SIGINT', async () => {
console.log('SIGINT received, shutting down gracefully...');
await disconnect();
process.exit(0);
});