Skip to content

implement chat with rooms and online users#184

Open
sashkatmshchk-pixel wants to merge 3 commits intomate-academy:masterfrom
sashkatmshchk-pixel:develop
Open

implement chat with rooms and online users#184
sashkatmshchk-pixel wants to merge 3 commits intomate-academy:masterfrom
sashkatmshchk-pixel:develop

Conversation

@sashkatmshchk-pixel
Copy link
Copy Markdown

Implemented chat with:

  • rooms
  • message history
  • username storage
  • online users list
  • enter key sending

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

You've built a solid foundation for the chat application, and the features for creating, joining rooms, and handling message history are well-implemented.

However, I am requesting changes because two key requirements from the task description are missing. The server-side implementation in src/index.js needs to include the functionality for both renaming and deleting chat rooms. Please add the necessary event handlers and logic to support these actions to fully complete the task.


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example


app.use(express.static(path.join(__dirname, '../public')));

io.on('connection', (socket) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the task description, you need to implement functionality for creating, renaming, joining, and deleting rooms. While handlers for creating and joining rooms are present, the implementation for renaming and deleting rooms is missing.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

Great job implementing the renameRoom and deleteRoom features! This requires a few changes because there are some critical issues that leave users in a broken state.

  • When a room is renamed or deleted, users inside are stuck because their currentRoom becomes invalid. You'll need to handle these users, for example, by moving them to a default room or notifying their client of the name change.
  • When a user switches rooms, the user lists for the old and new rooms aren't updated. The user should be removed from the old room's list and added to the new one.
  • User list updates are sent globally with io.emit() instead of to specific rooms with io.to(roomName).emit(). This sends incorrect data to users in other rooms and needs to be fixed on lines 40, 83, and 103.

Addressing these points will make the chat application much more stable. You're on the right track!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

src/index.js Outdated

socket.emit('rooms', Object.keys(rooms));
socket.emit('history', rooms[currentRoom].messages);
io.emit('users', rooms[currentRoom].users);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using io.emit() broadcasts the user list to every single client connected to the server, regardless of which room they are in. This means users in one room will incorrectly receive user list updates for another. You should use io.to(roomName).emit(...) to send updates only to clients within a specific room. This issue is present on lines 40, 83, and 103 as well.

Comment on lines +54 to +63
socket.on('renameRoom', ({ oldName, newName }) => {
if (!rooms[oldName] || rooms[newName]) {
return;
}

rooms[newName] = rooms[oldName];
delete rooms[oldName];

io.emit('rooms', Object.keys(rooms));
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming a room while users are inside it leaves them in an unusable state. Their currentRoom variable on the server becomes invalid because it still holds the oldName. Any subsequent action from that user, like sending a message, will cause an error because rooms[oldName] no longer exists. You need to handle these users, for example, by notifying their clients of the change so they can automatically rejoin the room under its new name.

Comment on lines +65 to +74
socket.on('deleteRoom', (roomName) => {
if (roomName === 'general') {
return;
}

if (rooms[roomName]) {
delete rooms[roomName];
io.emit('rooms', Object.keys(rooms));
}
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the renameRoom issue, deleting a room leaves clients inside it in a broken state. Their currentRoom variable will point to a room that no longer exists, causing errors on their next action. You should handle this case by moving the affected users to a default room, like 'general', and notifying them.

Comment on lines +76 to +84
socket.on('joinRoom', (roomName) => {
socket.leave(currentRoom);

currentRoom = roomName;
socket.join(currentRoom);

socket.emit('history', rooms[currentRoom].messages);
io.emit('users', rooms[currentRoom].users);
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a user switches rooms, the server should update the user lists for both the old and the new room. Currently, the user is not removed from the old room's list or added to the new room's list. You'll need to add logic here to:

  1. Remove the user from rooms[currentRoom].users.
  2. Notify users in the old room about the change.
  3. Add the user to rooms[roomName].users.
  4. Notify users in the new room about their arrival.

Copy link
Copy Markdown

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review Summary

I am approving your solution as an educational override to help you progress, as there are a few important state management issues to address. You've done a great job fixing the user list updates when users join or leave a room!

However, there are a couple of key areas to focus on:

  1. When a user changes their name, the old username is not removed from the room's user list, creating duplicates.
  2. When a room is renamed or deleted, other users in that room are not notified of the change. This leaves their clients in a broken state, still pointing to the old room. You'll need to emit an event to all affected clients to tell them to update their state.

Fixing these state consistency bugs will make your application much more robust. You're doing great work!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

Comment on lines +34 to +38
username = name;

if (!rooms[currentRoom].users.includes(username)) {
rooms[currentRoom].users.push(username);
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a user changes their name, the new name is added to the user list, but the old one isn't removed. You should remove the previous username from the users array before adding the new one.

Comment on lines +65 to +67
if (s === socket) {
currentRoom = newName;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic updates currentRoom only for the user who initiated the rename. Other users in the room are moved to the new socket.io room, but their currentRoom variable on the server still points to the old room name. This leaves their client in a broken state. You need to notify all clients in the affected room about the change so they can update their state accordingly.

Comment on lines +78 to +83
io.sockets.sockets.forEach((s) => {
if (s.rooms.has(roomName)) {
s.leave(roomName);
s.join('general');
}
});
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop correctly moves users to the 'general' socket.io room, but there are two issues here:

  1. The users from the deleted room are not added to the rooms.general.users array.
  2. The currentRoom variable for the moved users is not updated, which leaves their clients in an invalid state.

You'll need to update the 'general' room's user list and also notify the affected clients that they have been moved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants