feat(dashboard): add online count for events

Show real-time count of users currently in each event chat room.
- Backend: Export getEventsOnlineCounts from socket module
- Dashboard API: Include onlineCount for each active event
- Frontend: Display online count with animated green dot indicator
This commit is contained in:
Radosław Gierwiało
2025-11-21 21:41:16 +01:00
parent c15031db9f
commit 2c0620db6a
4 changed files with 88 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ const express = require('express');
const router = express.Router();
const { authenticate } = require('../middleware/auth');
const { PrismaClient } = require('@prisma/client');
const { getEventsOnlineCounts } = require('../socket');
const prisma = new PrismaClient();
@@ -38,6 +39,15 @@ router.get('/', authenticate, async (req, res, next) => {
},
});
// Get online counts for all events (safely handle if socket not initialized)
let onlineCounts = {};
try {
const eventIds = eventParticipants.map(ep => ep.event.id);
onlineCounts = getEventsOnlineCounts(eventIds);
} catch (_) {
// Socket may not be initialized (e.g., during tests)
}
// Get user's heats for each event
const activeEvents = await Promise.all(
eventParticipants.map(async (ep) => {
@@ -72,6 +82,7 @@ router.get('/', authenticate, async (req, res, next) => {
startDate: ep.event.startDate,
endDate: ep.event.endDate,
participantsCount: ep.event.participantsCount,
onlineCount: onlineCounts[ep.event.id] || 0,
myHeats: heats.map((h) => ({
id: h.id,
competitionType: h.competitionType,

View File

@@ -461,4 +461,21 @@ function initializeSocket(httpServer) {
return io;
}
module.exports = { initializeSocket, getIO };
// Get count of online users for a specific event
function getEventOnlineCount(eventId) {
if (!activeUsers.has(eventId)) {
return 0;
}
return activeUsers.get(eventId).size;
}
// Get online counts for multiple events
function getEventsOnlineCounts(eventIds) {
const counts = {};
for (const eventId of eventIds) {
counts[eventId] = getEventOnlineCount(eventId);
}
return counts;
}
module.exports = { initializeSocket, getIO, getEventOnlineCount, getEventsOnlineCounts };