Next.js Discord

Discord Forum

Socket never disconnects

Unanswered
Masai Lion posted this in #help-forum
Open in Discord
Masai LionOP
Hello,
I'm using a Provider for a Socket connection in nextjs 13, but the problem is everytime I close the window, the socket never disconnects. So when I come back in the app, it creates a new connection, indefinitely. The only time the socket instances end, it is when I close the nextjs server.

I don't understand because I disconnect the socket when the Socket Provider is unmounted :
 useEffect(() => {
        const handleBeforeUnload = () => {
            socketRef.current?.disconnect();
        };

        window.addEventListener('beforeunload', handleBeforeUnload);
        window.addEventListener('unload', handleBeforeUnload);
        return () => {
            window.removeEventListener('beforeunload', handleBeforeUnload);
            window.removeEventListener('unload', handleBeforeUnload);
            socketRef.current?.disconnect();
        };
    }, []);

1 Reply

Masai LionOP
No ideas ? 🥲