water

water

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

Showing Posts 1 to 5

windexoriginal

windexoriginal

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.

derek-zhou

derek-zhou

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.

water

water OP

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 …)

windexoriginal

windexoriginal

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

water

water OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews