Skip to content

Commit 71e032d

Browse files
committed
Collab: Add RoomCount in Websocket
1 parent f7a73d6 commit 71e032d

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

collab-service/app/server.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const io = new Server(server, {
2121
},
2222
});
2323

24+
// Partially Referenced from: https://github.com/yjs/y-monaco/blob/master/demo/monaco-demo.js
2425
const yjsWs = new WebSocketServer({ noServer: true });
2526
yjsWs.on("connection", (conn, req) => {
2627
setupWSConnection(conn, req, {
@@ -36,18 +37,23 @@ setInterval(() => {
3637
const stats = {
3738
conns,
3839
docs: docs.size,
39-
websocket: `ws://localhost:${PORT}`,
40-
http: `http://localhost:${PORT}`,
4140
};
4241
console.log(`${new Date().toISOString()} Stats: ${JSON.stringify(stats)}`);
4342
}, 10000);
4443

44+
const roomUserCount = new Map();
4545
io.on("connection", (socket) => {
4646
console.log("User connected to Socket.IO");
4747

4848
// Join a room based on roomId
4949
socket.on("joinRoom", (roomId) => {
5050
socket.join(roomId);
51+
if (roomUserCount.has(roomId)) {
52+
roomUserCount.set(roomId, roomUserCount.get(roomId) + 1);
53+
} else {
54+
roomUserCount.set(roomId, 1);
55+
}
56+
io.to(roomId).emit("roomCount", roomUserCount.get(roomId));
5157
console.log(`User joined room: ${roomId}`);
5258
});
5359

@@ -60,6 +66,18 @@ io.on("connection", (socket) => {
6066
io.to(roomId).emit("chatMessage", newMessage);
6167
});
6268

69+
socket.on("disconnecting", () => {
70+
socket.rooms.forEach((roomId) => {
71+
if (roomId !== socket.id) {
72+
if (roomUserCount.has(roomId)) {
73+
const currentCount = roomUserCount.get(roomId);
74+
roomUserCount.set(roomId, currentCount - 1);
75+
io.to(roomId).emit("roomCount", roomUserCount.get(roomId));
76+
}
77+
}
78+
});
79+
});
80+
6381
socket.on("disconnect", () => {
6482
console.log("User disconnected from Socket.IO");
6583
});

frontend/components/collab/chat.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default function Chat({
5555
const [aiMessages, setAiMessages] = useState<Message[]>([]);
5656
const [isConnected, setIsConnected] = useState(false);
5757
const [isLoading, setIsLoading] = useState(true);
58+
const [roomCount, setRoomCount] = useState(0);
5859
const lastMessageRef = useRef<HTMLDivElement | null>(null);
5960

6061
useEffect(() => {
@@ -145,6 +146,14 @@ export default function Chat({
145146
setIsConnected(false);
146147
});
147148

149+
socketInstance.on("roomCount", (roomCount: number) => {
150+
toast({
151+
title: "Room Count",
152+
description: "Current room count: " + roomCount,
153+
});
154+
setRoomCount(roomCount);
155+
});
156+
148157
socketInstance.on("chatMessage", (message: Message) => {
149158
setPartnerMessages((prev) => {
150159
const exists = prev.some(
@@ -171,7 +180,7 @@ export default function Chat({
171180
return () => {
172181
socketInstance.disconnect();
173182
};
174-
}, [roomId, own_user_id, auth?.user?.id]);
183+
}, [roomId, own_user_id, auth?.user?.id, toast]);
175184

176185
useEffect(() => {
177186
const scrollWithDelay = () => {
@@ -282,7 +291,7 @@ export default function Chat({
282291
<Card className="flex flex-col h-full">
283292
<CardHeader>
284293
<CardTitle className="flex justify-between items-center">
285-
Chat
294+
Chat (Users: {roomCount})
286295
<span
287296
className={`h-2 w-2 rounded-full ${isConnected ? "bg-green-500" : "bg-red-500"}`}
288297
/>

0 commit comments

Comments
 (0)