<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="255136" data-post-id="255136">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ndrean" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ndrean/120/36680_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ndrean
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks for enlightening me. I will dig into the <code>PartitionSupervisor</code>. Many tools with OTP! It seems to make powerful and lightweight apps. It is surprising it’s not more widely used, for web apps I mean.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="255136" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/unknown-registry-genserver/48748/22">Post #21</a>
	                </div>
	            </div>
              <div id="likers-container-255136" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="255136"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #21"></div>
  </section>
</div>
    <div class="postbit" id="255484" data-post-id="255484">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ndrean" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ndrean/120/36680_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ndrean
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Progressing slowly with Elixir. If I don’t use state in a module but only use the messaging part, I understand I may not need a Genserver. I used a <a href="https://hexdocs.pm/elixir/1.13/Task.html" rel="noopener nofollow ugc">supervised  Task</a>  and use a  <code>receive do</code> loop in the module. This seems to work (you can subscribe and receive the event/data between distributed nodes). However, this looks like I’m using Genserver with more complications and no benefit, aren’t I?</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># some module broadcasts an event/payload: 
Phoenix.PubSub.broadcast(MyApp.PubSub, "node_up", {:new data})

# the "listener" module is started in the "children" array of the app supervisor
children = [..., {MyApp.Listener, opts},...]

defmodule MyApp.Listener
  # no need "child_spec"
  use Task, restart: : permanent

  def start_link(opts) do
    task = Task.start_link(__MODULE__, :start, [opts])
    IO.inspect(task, label: "start_link")
  end

  def start(opts) do
     do_something_with(opts)
      Phoenix.PubSub.subscribe(MyApp.PubSub, "node_up")
      monitor()
  end

  def monitor() do
    receive do
      {:new, payload} -&gt;
        IO.inspect(payload, label: "Node UP -&gt; action with: ")
        monitor()
    end
  end
end
</code></pre>
<p>Side note: I can’t reach for  <code>Process.whereis(MyApp.Listener) == nil</code>, however the process appears by pid under the supervision tree.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="255484" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/unknown-registry-genserver/48748/23">Post #22</a>
	                </div>
	            </div>
              <div id="likers-container-255484" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="255484"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #22"></div>
  </section>
</div>
    <div class="postbit" id="255502" data-post-id="255502">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="msimonborg" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/msimonborg/120/26638_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  msimonborg
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="al2o3cr" data-post="12" data-topic="48748">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/al2o3cr/48/3457_2.png" class="avatar"> al2o3cr:</div>
<blockquote>
<p>A general observation: if you’re ignoring the GenServer’s state in every handler, you probably don’t need a GenServer. For instance, <code>save_with_call</code> could be simplified to:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def save_with_call(user) do
  Phoenix.PubSub.broadcast_from(MyApp.PubSub, self(), @topic, {:perform_new, user})
end
</code></pre>
<p>As a bonus, this approach doesn’t have a single process (the GenServer) forcing everything to happen one-at-a-time.</p>
</blockquote>
</aside>
<p>My take on this comment that I think you’re referencing was that it’s guiding you to consider not using a GenServer for bare function calls when the GenServer’s state is being ignored. The original code you posted didn’t have any use for the GenServer state, nor was the GenServer receiving and responding to any pubsub messages, so it could have been written as a module and functions to allow parallel execution from any process, rather than serial execution from a single process.</p>
<p>Now that it’s clear you need a process to listen for and respond to changes in cluster membership, it is appropriate to use a GenServer for that. I think the way you’re using the Task as a replacement is a misunderstanding of the basis for that original comment, and a misuse of Tasks, which are really an abstraction for one-off computations. A GenServer is a general-purpose abstraction for listening to and handling messages in a loop, so it’s the right tool for the job IMO.</p>
<aside class="quote no-group" data-username="ndrean" data-post="23" data-topic="48748">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ndrean/48/36680_2.png" class="avatar"> ndrean:</div>
<blockquote>
<p>Side note: I can’t reach for <code>Process.whereis(MyApp.Listener) == nil</code>, however the process appears by pid under the supervision tree.</p>
</blockquote>
</aside>
<p>AFAIK you can’t register a name for a Task, precisely because they’re not meant to be used as this type of long running “reachable” process</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="255502" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/unknown-registry-genserver/48748/24">Post #23</a>
	                </div>
	            </div>
              <div id="likers-container-255502" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="255502"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #23"></div>
  </section>
</div>
    <div class="postbit" id="255517" data-post-id="255517">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="ndrean" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ndrean/120/36680_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  ndrean
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yes, when I read about the Task, it seemed like an inappropriate tool, that’s why I posted this. It was however instructive! Now it makes sense. Thanks.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="255517" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/unknown-registry-genserver/48748/25">Post #24</a>
	                </div>
	            </div>
              <div id="likers-container-255517" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="255517"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>