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.
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.
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.
I took a look at your service and tried deploying an Action Hero of my own at: https://actionhero-test.onrender.com/ and was able to have to get it successfully running without the web socket errors.
A few things to try:
Set your build command to: npm install && npm run build
Set your start command to: npm run start
Probably un related but it seems that your Redis instance ended up running on port 10000 instead of 6379. Can you add an environment variable to that service PORT: 6379 which will force Redis to redeploy and use that port.
Thanks for the feedback @Sean_Doughty!
I can see the app building and starting OK in the logs, and I can confirm that it’s properly connected to redis (the app crashes and doesn’y boot otherwise). The REST api parts of the app all work OK (eg Actionhero.js Swagger Documentation).
When checking out your version, I see quite a few problems with the API (nothing loaded on the homepage for instance).
I’m curious if the deployment type matters - I did a web service
This is going to sound nuts - but I’m pretty sure that the new versions of the app I’m deploying are not what’s being served up on https://demo-actionherojs-com.onrender.com/. The logs don’t show any requests, and they should.
Can you correlate a deployment with a URL?
(I’m not allowed to post more comments, so I keep editing this one)
I was able to replicate the error you are having when using the Free plan. We are taking a look to see why this is the case.
Update:
We are looking at why the web sockets are not working with our Free Plan and we will pick up this thread early next week. In the meantime, you could try running your api on the Starter plan.
Yeah I’m also getting this on Phoenix. Works flawlessly with a local release but when deployed I get an error. Everything else on the platform is working well.
I’m using node js web socket and my connection is establish in starting but after few minutes it loss the connection and reconnect
I’m using render free plan how can i make web sockets in node js???