Sanford Staab
Sanford Staab

Reputation: 289

How to handle random heroku ports

It appears that when I start my heroku server remotely, it chooses a random value for the port. This is handled fine on the server by using the process.env.PORT value but how does a hard-coded client know what port to use to connect to the heroku server? There seems to be no way to force the heroku port value - this seems to be for cool server restart ability and container issues and to prevent port collisions. That's cool but how can I use a server who's port changes from time to time?

Upvotes: 2

Views: 540

Answers (1)

Damien MATHIEU
Damien MATHIEU

Reputation: 32629

While your app needs to listen on a random port, from an outsider point of view, you will always open a connection on port 80 or 443.

Heroku has a router through every connection goes first.

Whenever a request goes to appname.herokuapp.com or a custom domain you have configured, it is sent to that router.
The router knows about all your running dynos (and the port the app runs on), and will pick one randomly to send the connection to.

Upvotes: 1

Related Questions