WebSockets issue connecting to Node Express App

I have a Node Express App which I’ve deployed according to the docs. The server has a WebSocket endpoint and everything works fine locally. When we deploy the app it is hit and miss whether we can connect to the server via our one-page app (also deployed to Render). The error we see in the browser console is:

WebSocket connection to 'wss://chat-server-a1mm.onrender.com/chat' failed: WebSocket is closed before the connection is established

When we run our one-page app locally and connect to the server we see the same error. Deploying the Express App to a Droplet and connecting to it everything works fine. We deployed the app as a Docker container to see if it behaves any differently but we got the same result.

Are there any other settings we need to enable to get WebSockets to work more reliably? Any suggestions on how to debug this further? At the moment it feels like the issue is between our deployed Express App and the load balancer (or proxy) in front of it.

Hi @croman,

Sorry we missed your question earlier. Are you still running into issues?

Yeah, still having issues with it. You can see if it happening if you go to https://lodestar.cliche.cloud/ and open the console. After a minute or two you’ll see the error message. Still stumped as to why it is happening.

Hi @croman,

I looked into your issue. I was able to verify that the error is showing up in the console when I open the link you posted. There’s no settings needed to make WebSockets work for apps deployed on Render.

According to this StackOverflow post I found, can you take a look at how you’re establishing the WebSocket connection? Potentially, close() might be called before the connection is established. One experiment you could do is move the close() call to a different location or initially omit it altogether to see if the same error is occurring.

I’ll have a look at it and see if makes things better. The strange problem is that the exact same stack deployed locally or to a droplet on DigitalOcean all works fine. Makes me think there might be something happening at the proxy level directing traffic to our app.

That’s definitely strange that it works fine deployed to a droplet on DigitalOcean.

If the issue doesn’t go away, would you mind creating a minimal repro that we can use to investigate? An express app with a single websocket endpoint and a one-page app that only tries to connect to that endpoint would be ideal.