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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="CAIOHOBORGHI" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/CAIOHOBORGHI/120/20862_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  CAIOHOBORGHI
                    <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>Pretty Nice, with a BroadcastDispatcher, the selector of the Consumer is now working(and thats great!)</p>
<p>Now I`m kinda confused abot how the handle_demand of producer works.</p>
<p>I needed to send the list(or and update of it) to the consumer from times to time(not always, but not with Process.sleep(), is there a way to only send it through a method call?</p>
<p>Thanks for helping!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="195867" 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/best-way-to-broadcast-data-to-all-workers/35716/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-195867" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="195867"
                     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 #11"></div>
  </section>
</div>
    <div class="postbit" id="195868" data-post-id="195868">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Well, as soon as a message is published by the producer, the consumer will digest it when it is idle. In case you need to add a delay to this consumption you would have to do it on the producer some way.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="195868" 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/best-way-to-broadcast-data-to-all-workers/35716/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-195868" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="195868"
                     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 #12"></div>
  </section>
</div>
    <div class="postbit" id="195870" data-post-id="195870">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>is “Not B” related to be, or does it mean anything besides “A” or “B”? Please explain your problem fully, if you add new concepts on each message we will not be able to help you properly <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>
<blockquote>
<p>Well, I though of sending it separated for performance improvement…</p>
</blockquote>
<p>It may work, but spawning processes for each item will definitely <em>not</em> improve performances to just run “a quick validation” and a print on a list of 1000 items.</p>
<p>For now I would stick to the last proposal I made above, spawning a process for each item as required.</p>
<p>First, a producer that receive the whole list (as in the code I have posted before). You socket receives the whole list, and now you have that in memory. With any solution you will have to copy this data to other processes memory anyway, so you just send it to the producer and you can garbage collect on the socket. If you want to improve, you can stream events from the socket to the producer, but that is optimization, so for a future version, if ever needed.</p>
<p>So, you have this producer that receives the whole list. It uses the default dispatcher (not the broadcast or partition ones).</p>
<p>Then you add a ConsumerSupervisor that will receive the items according to its max_demand, and will spawn a process for each list item. In the handler function, you check the code and call the appropriate validator for this code.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule SocketListUpdater do
  def start_link(producer \\ nil) do
    spawn_link(fn -&gt; init(producer) end)
  end

  defp init(producer) do
    loop(producer)
  end

  defp loop(producer) do
    IO.puts("sending list")
    send(producer, {:new_list, generate_list()})
    # Send a list every minute, here every second
    Process.sleep(1000)
    loop(producer)
  end

  defp generate_list() do
    Stream.cycle(["A", "B", "C"])
    # The length of the list
    |&gt; Enum.take(1000)
    |&gt; Enum.shuffle()
    |&gt; Enum.map(fn code -&gt; %{code: code, infos: generate_infos()} end)
  end

  defp generate_infos do
    [
      %{name: "mary"},
      %{name: "john"},
      %{name: "robert"},
      %{name: "ava"},
      %{name: "simon"},
      %{name: "simon"}
    ]
    |&gt; Enum.take(Enum.random(2..6))
  end
end

defmodule GenstageExample.Producer do
  use GenStage

  def start_link() do
    GenStage.start_link(__MODULE__, [], name: __MODULE__)
  end

  def init(_arg) do
    {:producer, :some_state}
  end

  def handle_info({:new_list, list}, state) do
    {:noreply, list, state}
  end

  def handle_demand(_demand, state) do
    {:noreply, [], state}
  end
end

defmodule GenstageExample.Consumer do
  use ConsumerSupervisor

  # Starts consumer with his code
  def start_link(arg) do
    ConsumerSupervisor.start_link(__MODULE__, arg)
  end

  def init(_arg) do
    # Note: By default the restart for a child is set to :permanent
    # which is not supported in ConsumerSupervisor. You need to explicitly
    # set the :restart option either to :temporary or :transient.
    children = [
      %{
        id: GenstageExample.Validator,
        start: {GenstageExample.Validator, :start_link, []},
        restart: :transient
      }
    ]

    opts = [strategy: :one_for_one, subscribe_to: [{GenstageExample.Producer, max_demand: 50}]]
    ConsumerSupervisor.init(children, opts)
  end
end

defmodule GenstageExample.Validator do
  def start_link(event) do
    Task.start_link(fn -&gt; validate(event) end)
  end

  # Here you can call any validator function depending on the code. In this
  # example I implemented the validators directly in the function body

  defp validate(%{code: "A", infos: infos}) do
    if length(infos) &lt; 3 do
      IO.puts("item with code A is not valid")
    end
  end

  defp validate(%{code: "B", infos: infos}) do
    if length(infos) == 2 do
      IO.puts("item with code B is not valid")
    end
  end

  defp validate(%{code: code}) do
    IO.puts("received unknown code #{code}")
  end
end

GenstageExample.Producer.start_link()
GenstageExample.Consumer.start_link([])
SocketListUpdater.start_link(GenstageExample.Producer)

</code></pre>
<p>So you do not have to start a validator for “B” or “Not B”, you just have a single <code>validate/1</code> function that will check the code and do the appropriate validation. It is simpler.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="195870" 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/best-way-to-broadcast-data-to-all-workers/35716/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-195870" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="195870"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-solved cat-solved" title="Marked as solution"></div>
  </section>
</div>
    <div class="postbit" id="195910" data-post-id="195910">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="CAIOHOBORGHI" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/CAIOHOBORGHI/120/20862_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  CAIOHOBORGHI
                    <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>Hey lud, thanks for the code, I think that this will work with a few adjustments.</p>
<p>The only think is that I cant write different validate functions, because I dont know which code will be on the list(And even if I do, I wouldnt write 800 versions).</p>
<p>But with validate, I can get the code and the infos, and do what I need.</p>
<p>The final ideia is to do something like</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defp validate({%{code: code, infos: infos}) do
    IO.puts("Checking code #{code}...")
    {age, name, nickname} = infos
    if age &gt; 18 do
         IO.puts("#{nickname} is an adult ")
    end

end
</code></pre>
<p>Thats a silly demonstration, but the goal is to do specific commands according to properties inside “infos”.</p>
<p>Thanks for your time and effort</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="195910" 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/best-way-to-broadcast-data-to-all-workers/35716/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-195910" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="195910"
                     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 #14"></div>
  </section>
</div>
    <div class="postbit" id="195942" data-post-id="195942">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Well then you never needed to check the code actually ?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="195942" 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/best-way-to-broadcast-data-to-all-workers/35716/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-195942" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="195942"
                     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>