'my websocket closes before the end of a session

I'm making collaborative editor and a I do want to use websockets. But my websocket get closed right after the opening while the client didn't even close the session. Can you help me fix that issue ?

This is my code the connection I made

function connectToWebSocket(){
    websocket = new WebSocket(endpoint);
    console.log("Connecting to : "+endpoint);
    websocket.onopen = () => {
        console.log("opened");  
        websocket.send("hello");
    }
    
    websocket.onmessage = (e) => {
        console.log("Message "+e.data);
        return false;
    }
    
    websocket.onclose = () => {
        console.log("closed");
    }
    
    websocket.onerror = () => {
        console.log("error");
    }
}


/**
* add the event listener while loading the page
 */
 window.addEventListener("load", init);
 window.addEventListener("load", connectToWebSocket);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source