I am developing a chat app with socket.IO (frontend: React Native, backend: NodeJS). Everything works fine but sometimes messages are not sent and I discovered the problem is due to socket listeners, which sometimes went lost without an apparent reason. Infact, when mounting the component, i call useEffect to subscribe listeners like this:
useEffect(() => {
socket.on("receiveMessage", callback);
socket.on("receiveAck, callback);
... and others
},[])
if i console.log(socket._callbacks), the result is an array with the names of listeners and their relative callbacks
for example: console.log(socket._callbacks) => [ ["receiveMessage", callback function], ["receiveAck", callback function], ...]
when messages begin to not work properly, i console.log(socket._callbacks) and the result is an EMPTY ARRAY, so it's like all the listeners went lost. And the strange thing is that it happens even when the socket is correctly connected to the server (console.log(socket.connected) => true).
why this happens?