feat: add Socket.IO heats_updated broadcast event
- Export getIO function from socket module - Broadcast heats_updated event when user updates their heats - Event includes userId, username, and updated heats array - Non-blocking broadcast (won't fail request if socket fails)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const { prisma } = require('../utils/db');
|
const { prisma } = require('../utils/db');
|
||||||
const { authenticate } = require('../middleware/auth');
|
const { authenticate } = require('../middleware/auth');
|
||||||
|
const { getIO } = require('../socket');
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
@@ -566,6 +567,28 @@ router.post('/:slug/heats', authenticate, async (req, res, next) => {
|
|||||||
return userHeats;
|
return userHeats;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Broadcast heats update to all users in event room
|
||||||
|
try {
|
||||||
|
const io = getIO();
|
||||||
|
const roomName = `event_${event.id}`;
|
||||||
|
io.to(roomName).emit('heats_updated', {
|
||||||
|
userId: req.user.id,
|
||||||
|
username: req.user.username,
|
||||||
|
heats: result.map(heat => ({
|
||||||
|
id: heat.id,
|
||||||
|
divisionId: heat.divisionId,
|
||||||
|
division: heat.division,
|
||||||
|
competitionTypeId: heat.competitionTypeId,
|
||||||
|
competitionType: heat.competitionType,
|
||||||
|
heatNumber: heat.heatNumber,
|
||||||
|
role: heat.role,
|
||||||
|
})),
|
||||||
|
});
|
||||||
|
} catch (socketError) {
|
||||||
|
// Don't fail the request if socket broadcast fails
|
||||||
|
console.error('Failed to broadcast heats update:', socketError);
|
||||||
|
}
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
count: result.length,
|
count: result.length,
|
||||||
|
|||||||
@@ -5,8 +5,18 @@ const { prisma } = require('../utils/db');
|
|||||||
// Track active users in each event room
|
// Track active users in each event room
|
||||||
const activeUsers = new Map(); // eventId -> Set of { socketId, userId, username, avatar }
|
const activeUsers = new Map(); // eventId -> Set of { socketId, userId, username, avatar }
|
||||||
|
|
||||||
|
// Global Socket.IO instance
|
||||||
|
let io;
|
||||||
|
|
||||||
|
function getIO() {
|
||||||
|
if (!io) {
|
||||||
|
throw new Error('Socket.IO not initialized');
|
||||||
|
}
|
||||||
|
return io;
|
||||||
|
}
|
||||||
|
|
||||||
function initializeSocket(httpServer) {
|
function initializeSocket(httpServer) {
|
||||||
const io = new Server(httpServer, {
|
io = new Server(httpServer, {
|
||||||
cors: {
|
cors: {
|
||||||
origin: process.env.CORS_ORIGIN || 'http://localhost:8080',
|
origin: process.env.CORS_ORIGIN || 'http://localhost:8080',
|
||||||
credentials: true,
|
credentials: true,
|
||||||
@@ -348,4 +358,4 @@ function initializeSocket(httpServer) {
|
|||||||
return io;
|
return io;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { initializeSocket };
|
module.exports = { initializeSocket, getIO };
|
||||||
|
|||||||
Reference in New Issue
Block a user