Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ app.use(
app.use(express.json());

// Rate limiting
// FIX: Increased limit to 2000 to prevent '429 Too Many Requests' when polling for online counts
const limiter = rateLimit({
windowMs: config.rateLimit.windowMs,
max: config.rateLimit.maxRequests,
windowMs: 15 * 60 * 1000, // 15 minutes
max: 20000, // Allow 20000 requests per IP (was likely 100)
message: 'Too many requests from this IP, please try again later.',
});
app.use('/api/', limiter);
Expand Down Expand Up @@ -82,4 +83,4 @@ process.on('SIGINT', () => {
console.log('Server closed');
process.exit(0);
});
});
});
12 changes: 12 additions & 0 deletions backend/src/lib/roomState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// backend/src/lib/roomState.ts

// Stores the live count of users in each room
export const roomCounts = new Map<string, number>();

export const updateRoomCount = (roomId: string, count: number) => {
roomCounts.set(roomId, count);
};

export const getRoomCount = (roomId: string) => {
return roomCounts.get(roomId) || 0;
};
20 changes: 18 additions & 2 deletions backend/src/routes/rooms.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Router } from 'express';
import { supabase } from '../lib/supabase';
import { authenticate, AuthRequest } from '../middleware/auth';
import { getRoomCount } from '../lib/roomState'; // <--- MUST IMPORT THIS

const router = Router();

Expand All @@ -24,13 +25,28 @@ router.get('/', authenticate, async (req: AuthRequest, res) => {
}
});

// Get LIVE activity count (Reads from memory)
router.get('/:roomId/activity', authenticate, async (req: AuthRequest, res) => {
try {
const { roomId } = req.params;

// FIX: Read from shared memory, NOT database
// This gives the INSTANT count of connected sockets
const liveCount = getRoomCount(roomId);

res.json({ count: liveCount });
} catch (error) {
console.error('Error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});

// Get messages for a room
router.get('/:roomId/messages', authenticate, async (req: AuthRequest, res) => {
try {
const { roomId } = req.params;
const limit = parseInt(req.query.limit as string) || 50;

// Fetch messages without joining profiles (will get user data from WebSocket)
const { data, error } = await supabase
.from('messages')
.select('*')
Expand All @@ -50,4 +66,4 @@ router.get('/:roomId/messages', authenticate, async (req: AuthRequest, res) => {
}
});

export default router;
export default router;
Loading