0

I have done a little Chat-App -> React JS Chat Client and Node-JS (socket.io / express) Web-Socket server.

After I finished testing I deployed the NodeJS-Web-Socket server on heroku.

The URL I should use in the Client is const ENDPOINT = 'https://jorroch-consulting.herokuapp.com/';

What makes me wondering is that I had to use a port in the endpoint URL, when my server still was started on localhost. The URL looked like this: const ENDPOINT = 'localhost:5000';

That was because my server was listening to port 5000: server.listen(PORT, () => console.log('Server has started on port ${PORT}'));

Now, that the server is deployed on heroku the client does not have the info about the port 5000 and it works nevertheless.

How does my React-Client know which is the proper port? Does heroku routes all requests to port 5000? But how do they know? Do they scan the code?

I am asking, because I want to create a second WebSocket-Express server in the same index.js (is that possible?) file and redeploy that to heroku but think, without the use of different ports, that could not work.

MarkusJackson
  • 225
  • 2
  • 12
  • Why there is no port and how to deploy a websocket + http server simultanously on one app I've explained here: https://stackoverflow.com/questions/61497258/how-to-run-two-servers-in-heroku/61499366#61499366 – Tin Nguyen Apr 30 '20 at 10:48

1 Answers1

0

the application listens on a port that you decide on localhost, on Heroku it is assigned to you dynamically and mapped to a given hostname i.e. jorroch-consulting.herokuapp.com.
Accessing the host (on HTTPS) is re-routed to an internal service on port $PORT.

Heroku web applications have one single port ($PORT) so you cannot have 2 ports within the same Web dyno. You can scale your dyno to have multiple instances, or deploy a different application if the second server provides a different functionality.

Beppe C
  • 11,256
  • 2
  • 19
  • 41