Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed a little bug in useListenMessages.js hook #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
fixed a little bug in useListenMessages.js hook
I checked that if a person A is online(i.e. his socket.io id is online), and he has opened the chat with person B, then at that time if any person C sends him a message then it gets rendered in the person A and B chat conversation. Because useListenMessages.js hook listens to all the messages sent to person A's socket.io id...
So to prevent this while we have a selected conversation, we should add a if statement to check if the newMessage sent to our socket.io id is sent by the person we are chatting with right now, so others message will not be rendered during your ongoing chat with a person.
  • Loading branch information
deepak-kr-patra committed May 7, 2024
commit c48784271ea85ee068217d73b096c044bcb9c9ca
13 changes: 8 additions & 5 deletions frontend/src/hooks/useListenMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import notificationSound from "../assets/sounds/notification.mp3";

const useListenMessages = () => {
const { socket } = useSocketContext();
const { messages, setMessages } = useConversation();
const { messages, setMessages, selectedConversation } = useConversation();

useEffect(() => {
socket?.on("newMessage", (newMessage) => {
newMessage.shouldShake = true;
const sound = new Audio(notificationSound);
sound.play();
setMessages([...messages, newMessage]);
// add the newMessage to the messages array only if it is sent by the selectedConversation user id
if (selectedConversation?._id === newMessage.senderId) {
newMessage.shouldShake = true;
const sound = new Audio(notificationSound);
sound.play();
setMessages([...messages, newMessage]);
}
});

return () => socket?.off("newMessage");
Expand Down