1

I am currently looking at GRPC for my real time needs.

I noticed in the examples that we are explicitly required to bind to an hardcoded port in the Server.

I hope to deploy the Server on a Stack like Heroku.

Imagine I set the port to 9090 and that port is currently in use by another service won't that cause issues?

I was expecting a dynamic port allocation as encouraged by process.env.PORT

Any insights would be greatly appreciated.

ololo
  • 1,326
  • 2
  • 14
  • 47

1 Answers1

0

You are not explicitly required to bind to a hardcoded port. The example shows how to use the Server API, including binding to a port. You can still modify the code to address your own use case. If you want to, you can use the value of process.env.PORT instead of hardcoding a port number. Or you even use the common convention of using process.env.PORT || 9090 as the port number to have a default.

Alternatively, you can use a port number of 0, which will instruct the operating system to select an unused port for you. The chosen port number will be the return value if you are using bind, or the second argument to the callback if you are using bindAsync.

murgatroid99
  • 19,007
  • 10
  • 60
  • 95
  • 1
    Now, imagine I use `process.env.PORT` how will the client know the real port to connect to. I am talking of an Android Client. Thanks for your input – ololo May 03 '21 at 17:58
  • How do people usually know what port to connect to with Heroku? Do the same thing here. – murgatroid99 May 03 '21 at 18:01
  • 1
    @ murgatroid99 if you are conversant with REST, you know the client doesn't need to specify the port. GRPC requires that the Client Binds also provides the PORT in addition to the HOST. This is what I'm still confused about. – ololo May 03 '21 at 18:04
  • If a REST client doesn't specify a port, it uses the default port for the protocol, because that's how Internet protocols work. The same is true of gRPC. According to the Heroku documentation, "when browsing to your application on Heroku, you still use port 80, (`[your-application].herokuapp.com`)", so I suggest doing that. – murgatroid99 May 03 '21 at 18:08
  • About using bind's return value, will this value be available to the client somehow? I searched this `bind` API among base class ClientChannel and couldn't find any. I'm confused. I'm on grpc-dart and python. – kakyo Jun 04 '21 at 02:20
  • `bind` is a server API, for choosing the port that the server serves from. In general, the client will not be running in the same process or even on the same machine. You will usually need some out-of-band way for the client to know what that port number is. – murgatroid99 Jun 04 '21 at 03:16