How to only have [0,1]] Genserver for liveview

The genserver is only needed when a specific page is opened, and shouldn’t exist when none is on that page.
But adding it to application.ex makes it start even if none is on the page.

    children = [
      # ...
      {MyApp.MyGenserver, [etc_config]}
      MyApp.Endpoint
    ]

    opts = [strategy: :one_for_one, name: MyApp.Supervisor]
    Supervisor.start_link(children, opts)

When I place the call in the Liveview it launches an instance for everyone that is opening the page. And closes one after a page is closed.

  def mount(_params, _session, socket) do
    if connected?(socket) do
      Supervisor.start_link(
        [
          {MyApp.MyGenserver,
           [
             host: @ip,
             port: @port
           ]}
        ],
        strategy: :one_for_one
      )
    end

    { :ok, socket }
  end

I’m not sure what to try next. Could you point me in the right direction?
I know the following things exist, but not sure if they go in the right direction:

  • DynamicSupervisor.start_child/2
  • Register

What you want is start_child/2 and which_children/1. Provide a name for your applications supervisor (a module name style atom is conventional e.g. MyApp.Supervisor) and then when your liveview starts check if there is an existing child process using which_children/1 and start one under your application if none is found using start_child/2 using the name of your application supervisor.

But, I think you need a very good reason not to just go with your first solution and have the genserver start with your application.

1 Like

Yes, you need both the DynamicSupervisor and the Register. Do a Register.lookup/2 first, if none found start the genserver with DynamicSupervisor.start_child/2. Your genserver should auto-terminate if it is idle for some time. This system is not strictly 0 or 1 genserver at all time; there could be more than 1 genserver due to race condition. However, it should heal itself if that happened.

1 Like

I do have a reason, not sure if you would qualify it as very good…

I’m trying to have 2 projects in 1.
2 live dashboards/kiosks, 1 needs a tcp client (genserver) which listens for incoming messages.
But the other live dashboard doesn’t need the genserver. The logs would fill with messages saying the tcp server can’t be found, and keep retrying.
I don’t want to use environment variables to set this on the kiosk. The only difference between the 2 pc’s nixos config is the launch url (chromium …)

Why not start the socket server with the application and use pubsub to send messages to interested liveviews?

1 Like

Might work but slightly different.

Currently the genserver(tcp client) receives the messages, then filters and adds them to the db. The liveview updates based on the db.

I could let the genserver launch from Application.start/2 and be idle.
Then connect to the tcp server when 1 or more liveviews are connected. (pubsub or presence)
I could even close the connection when 0 liveview are connected.