diff --git a/communication-service/server.js b/communication-service/server.js index 8719f19752..87ff273882 100644 --- a/communication-service/server.js +++ b/communication-service/server.js @@ -76,6 +76,13 @@ function cleanupRoom(roomId) { */ function onJoinRoom(io, socket, data) { const { roomId, user } = data; + // if there are already two people in this room, reject the request + if (io.sockets.adapter.rooms.get(roomId)?.size >= 2) { + + console.log(`INFO: Room { ${roomId} } is full, rejecting user { ${user.userId} }`); + socket.emit('room-full'); + return + } onSetUsername(io, socket, user); socket.join(roomId); // User joins the specified room diff --git a/peer-prep/src/components/Communication/Text/TextChatWidget.tsx b/peer-prep/src/components/Communication/Text/TextChatWidget.tsx index 3c05bddfea..b84642ea0f 100644 --- a/peer-prep/src/components/Communication/Text/TextChatWidget.tsx +++ b/peer-prep/src/components/Communication/Text/TextChatWidget.tsx @@ -276,6 +276,16 @@ export default function TextChatWidget({ roomId }: TextChatWidgetProps) { setMessages(chatHistory); } + function onRoomFull() { + notifications.show({ + title: "Room is full", + message: "This room is full, please try again later", + color: "red", + }); + + socket.disconnect(); + } + socket.on("connect", onConnected); socket.on("disconnect", onDisconnected); socket.on("message-sent", onMessageSent); // for ack @@ -283,6 +293,7 @@ export default function TextChatWidget({ roomId }: TextChatWidgetProps) { socket.on("user-joined", onUserJoinedChat); // unused socket.on("room-people-update", onRoomPeopleUpdate); socket.on("chat-history", onReceiveChatHistory); + socket.on("room-full", onRoomFull); return () => { socket.disconnect(); @@ -291,6 +302,9 @@ export default function TextChatWidget({ roomId }: TextChatWidgetProps) { socket.off("message-sent", onMessageSent); socket.off("chat-message", onReceivedChatMessage); socket.off("user-joined", onUserJoinedChat); + socket.off("room-people-update", onRoomPeopleUpdate); + socket.off("chat-history", onReceiveChatHistory); + socket.off("room-full", onRoomFull); }; }, [roomId]);