How to start phoenix server without listening at any ip address or port

Hello,

I have an unusual question that is how to start phoenix server without actually listening at any ip address or port.

The reason is that my app will have two major functional blocks, one is as REST and socket api server. Another one is to listen to events from other applications, do some processing and publish the some data to FE socket via phoenix socket channels. I wish to be able to start one node as api server, another node as event listening, processing and publishing server that will join the cluster of api servers but doesn’t listen to any ip address thus not to expose itself.

Is it possible to do so?

Thanks.

1 Like

Hello and welcome.

I don’t think that this is possible because the “phoenix server” is a http server that expects http requeststo reply to.

But you can skip the endpoint from your supervisor children in your application.ex. The endpoint will not be started but that does not prevent you to listen or publish to your pubsub! (I believe so, the pubsub is independant by now, but I remember using the broadcast functions required an endpoint, but now I see it takes the pubsub name so all good I guess).

2 Likes

See the Runtime Configuration section of the Phoenix.Endpoint docs. You can set server: false.

  • :server - when true, starts the web server when the endpoint supervision tree starts. Defaults to false. The mix phx.server task automatically sets this to true
8 Likes

Ah yes, I forgot about it, that’s going to simpler :slight_smile:

1 Like

Thanks. I tested with :server set to false and it worked.

1 Like

BTW it is possible to set this through environment variable, without modifying the runtime config…

export PHX_SERVER=false

…before starting the server, as mentioned at the top of runtime.exs

4 Likes

This doesn’t actually work, does it? The env var check is part of the generated config in the aforementioned runtime.exs, but it only turns the server on, not off.

The server is actually off by default unless you use the special mix phx.server command which enables it. That’s why you have to pass PHX_SERVER=true for a release (where there’s no mix), which the generated scripts will do for you.

Another thing you can do is to simply listen on a UNIX domain socket. This way you can have a uniform starting strategy and a load balancer in front of your service can direct traffic to one of the sockets.

You can modify the endpoint config in application.ex

{MyApp.Endpoint,
         server:
           if is_event_handler do
             false
           else
             true
           end}