burakorkmez / mern-chat-app

Real Time Chat App | MERN && Socket.io && JWT
MIT License
623 stars 175 forks source link

In real time chatting #2

Closed dhruvinvaghani001 closed 4 months ago

dhruvinvaghani001 commented 4 months ago

in real time chat we set our message context with updated message and set it . but actually its not tru because if reciver selected conversation is diffrent then also he find your mesdsage but it should not let's take and example if there is thee user jhon , bob and alice now jhon and bob doing chat (online) in this code if jhon send message to bob and if selected conversation of bob is alice then also it shows new upcoming message but bob and alice neve do chat new upcoming message is actully of jhon if we refresh it then it comes propely due to context refresh in this case .

to solve this : we have to check if selected conversation id is same as reciver id then and then update message context otherwise dont update context of message.

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

useEffect(() => { socket?.on("new-message", ( newMessage ) => { const check = newMessage.receiverId == selectedConversation.id console.log(selectedConversation); newMessage.shake = true; const sound = new Audio(notificationSound); sound.play();

  if (check) {
    setMessages([...messages, newMessage]);
  }
});

return () => {
  socket?.off("new-message");
};

}, [socket, messages, setMessages]); };

export default useListenMessages;`

anmolpal2001 commented 4 months ago

in real time chat we set our message context with updated message and set it . but actually its not tru because if reciver selected conversation is diffrent then also he find your mesdsage but it should not let's take and example if there is thee user jhon , bob and alice now jhon and bob doing chat (online) in this code if jhon send message to bob and if selected conversation of bob is alice then also it shows new upcoming message but bob and alice neve do chat new upcoming message is actully of jhon if we refresh it then it comes propely due to context refresh in this case .

to solve this : we have to check if selected conversation id is same as reciver id then and then update message context otherwise dont update context of message.

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

useEffect(() => { socket?.on("new-message", ( newMessage ) => { const check = newMessage.receiverId == selectedConversation.id console.log(selectedConversation); newMessage.shake = true; const sound = new Audio(notificationSound); sound.play();

  if (check) {
    setMessages([...messages, newMessage]);
  }
});

return () => {
  socket?.off("new-message");
};

}, [socket, messages, setMessages]); };

export default useListenMessages;`

When sender sends the message and selectedConversation id is the sender's id in the receiver's window, the messages window is not updating automatically it require reload of the page, can you provide any solution for this ??

dhruvinvaghani001 commented 4 months ago

there should be error in the socket logic so when a new message is sent then on receiver side socket event not recived properly and setMessages not run so updated messages not set directly

johnjehiel commented 4 months ago

you also need to add the selectedConversation in the use effect to update the message when the respective user is selected, ` const useListenMessages = () => { // . . .

useEffect(() => {
    //. . .
}, [socket, setMessages, messages ,selectedConversation]);

}; export default useListenMessages;`

satt-hri commented 4 months ago

[setMessages] is a function and does not need to be passed.