Err_connection_timed_out

Hello,
On my project https://glitch.com/edit/#!/scarlet-geese-1, I have a websocket server and a script to connect to it:

const socket = new WebSocket("wss://scarlet-geese-1.glitch.me:40510/")
socket.onopen = e => console.log(e)
socket.onclose = socket.onerror = socket.onmessage = e => console.log(e)

But after a long time, I’m getting the follow error: WebSocket connection to 'ws://scarlet-geese-1.glitch.me:40510/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

It successfully connects locally, but not on glitch.

Could anyone help me? Thanks

Hi @demostanis, I think you’re trying to open a websocket server and trying to connect from a client application; if that’s not correct let me know.

If it is correct, Glitch only opens one port to the outside world. In Node that can be referred to with process.env.PORT, which allows Glitch to manage the PORT for things like the Debugger. Then when you connect you’ll just use something like const socket = new WebSocket("wss://scarlet-geese-1.glitch.me").

Hope this helps!

1 Like

Hi, thanks for the answer. It worked for a day, but then, when I came back to the project this morning, it kept throwing 502 errors… Could you help me? Thanks

Hy @demostanis I’m not sure what you mean when you say it worked for a day. Looking at your project’s history I see that your WebSocket server was briefly associated with the correct port but that it was later changed back - your WebSocket server would only have accepted connections when it was set to use the correct port. The 502 status code is one of the error’s you might receive if you attempt to connect over an incorrect port. You should be able to resolve this by changing 8000 at https://glitch.com/edit/#!/scarlet-geese-1?path=server.js:20:41 to prrocess.env.PORT.

Hello,
Sorry but I’m into a new project… https://glitch.com/edit/#!/fragrant-cake
In it it has process.env.PORT for the websocket server, but it sends 502 errors…