I succesfully implemented a real-time chat in specific 'room' with Socket.io-client.
And now, I want to make every message from every room make an alert from everywhere in my page even though the user doesn't actually enter every room.
So, I wrote codes like this.
const socket = io(server-url);
This Socket is managed with useContext. (The socket is connected in the ContextProvider)
useEffect(() => {
socket.connect();
});
And I made this socket gets global messages in the layout.js
where the <Header/>, <Footer/>, <Outlet/>
is placed. Sadly, it doesn't work.
I don't have an access to the server code, but I know that there's no problem in the server.
const { socket } = useChatData();
useEffect(() => {
socket.emit(`connect`, { socket: socketID });
if (chats)
socket.on("newMsg", data => {
console.log(data);
});
}, [socket]);
This code doesn't make any error, so I couldn't catch what's the problem. How can I solve this? I'll also be very happy for some feedbacks or advices for the concept or method of global messages with socket.io-client that are not related with my codes.