'Establish socket.io connection to heroku app

I am hosting my socket.io server on heroku and I am running the client-side of the application on localhost. On the frontend I establish the socket.io connection like this:

const socket = io("https://<my-app-name>.herokuapp.com:5000/");

And the backend looks like this:

const port = 5000;
const io = require("socket.io")(port, { cors: { origin: "*" } });
io.on("connection", (socket) => { console.log(socket.id) }

But the connection won't establish and in the console of my browser I receive this error:

GET https://<my-app-name>.herokuapp.com:5000/socket.io/?EIO=4&transport=polling&t=Nym-K6b net::ERR_CONNECTION_TIMED_OUT

BTW it works just fine when I'm running the server-side on my localhost too.



Solution 1:[1]

https://socket.io/docs/v4/troubleshooting-connection-issues/#the-server-is-not-reachable

Please make sure the Socket.IO server is actually reachable at the given URL. You can test it with: curl "<the server URL>/socket.io/?EIO=4&transport=polling" which should return something like this: 0{"sid":"Lbo5JLzTotvW3g2LAAAA","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":20000}. If that's not the case, please check that the Socket.IO server is running, and that there is nothing in between that prevents the connection.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Maria Ines Parnisari