water

water

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

Marked As Solved

windexoriginal

windexoriginal

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

Also Liked

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.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement