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


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Qqwy" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Qqwy/120/1349_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Qqwy
                    <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 class="user-title">
									<span>TypeCheck Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thank you very much for your help, everyone. <img src="https://forum.elixirforum.com/images/emoji/apple/heart.png?v=15" title=":heart:" class="emoji" alt=":heart:" loading="lazy" width="20" height="20"></p>
<p>To make it more clear what I am struggling with, I have written it down now as a library: <a href="https://github.com/qqwy/elixir_sliding_window" rel="noopener nofollow ugc">SlidingWindow</a>. (Still very work-in-progress; it does not have a lot of documentation yet as things might still change a lot.)</p>
<p>It does what I want to perform in a finite-state-automaton kind of way; that is, SlidingWindows does not care about GenServers or anything at all right now, but only about gathering and updating the information stored in the struct.</p>
<p>An example of a behaviour to calculate averages: (See also the file <a href="https://github.com/Qqwy/elixir_sliding_window/blob/master/test/test_helper.exs" rel="noopener nofollow ugc">test_helper.exs</a>)</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule TestSW do
  @behaviour SlidingWindow.Behaviour

  defmodule Transaction do
    defstruct [:value, :created_at]
    def new(value, created_at) do
      %__MODULE__{value: value, created_at: created_at}
    end
  end

  defstruct count: 0, sum: 0, product: 1

  def empty_aggregate() do
    %__MODULE__{}
  end

  def add_item(agg, %Transaction{value: int}) do
    %__MODULE__{agg |
      count: agg.count + 1,
      sum: agg.sum + int,
      product: agg.product * int
    }
  end

  def remove_item(agg, %Transaction{value: int}) do
    %__MODULE__{agg |
      count: agg.count - 1,
      sum: agg.sum - int,
      product: div(agg.product, int)
    }
  end

  def extract_timestamp(%Transaction{created_at: timestamp}) do
    timestamp
  end

end
</code></pre>
<p>And then you can create it using something like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">result = 
      SlidingWindow.init(TestSW, 10, Timex.Duration.from_seconds(2), initial_data())
      |&gt; SlidingWindow.shift_stale_items(ten_sec_future)
      |&gt; SlidingWindow.add_item(%Transaction.new(10, Timex.now())
      |&gt; SlidingWindow.add_item(%Transaction.new(15, Timex.now())
      # And at some point:
      |&gt; SlidingWindow.get_aggregates()

# Result now contains something like:
result ==            %{0 =&gt; %TestSW{count: 0, product: 1, sum: 0},
                       1 =&gt; %TestSW{count: 0, product: 1, sum: 0},
                       2 =&gt; %TestSW{count: 0, product: 1, sum: 0},
                       3 =&gt; %TestSW{count: 1, product: 1, sum: 1},
                       4 =&gt; %TestSW{count: 2, product: 6, sum: 5},
                       5 =&gt; %TestSW{count: 2, product: 20, sum: 9},
                       6 =&gt; %TestSW{count: 2, product: 42, sum: 13},
                       7 =&gt; %TestSW{count: 2, product: 72, sum: 17},
                       8 =&gt; %TestSW{count: 2, product: 110, sum: 21},
                       9 =&gt; %TestSW{count: 2, product: 156, sum: 25}}
</code></pre>
<hr>
<p>In the end, I think that the smartest decision I can make is to <em>not</em> mess with wrapping this inside of a GenServer-like layer myself, as there are an infinite amount of possible ways how someone might want to manipulate the FSA. Rather, I’ll write some example code on how it could be wrapped in your own GenServer including an <code>send_after</code> message to ensure that the data inside never becomes stale.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="36904" data-batch-url="/posts/batch_likers">
                        3
                      </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/how-to-wrap-phoenix-pubsub/5998/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-36904" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="36904"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></div>
  </section>
</div>
    <div class="postbit" id="76141" data-post-id="76141">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I too am struggling to get PubSub configured w/o phoenix.  So I took the code in this example and wrote it into a small example project.</p>
<p>But something is not configured correctly.  Any and all hints are appreciated.</p>
<p><a href="https://github.com/mwindholtz/pub_sub_spike/blob/master/README.md" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/mwindholtz/pub_sub_spike/blob/master/README.md</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="76141" 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/how-to-wrap-phoenix-pubsub/5998/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-76141" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="76141"
                     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="76144" data-post-id="76144">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Looks like you need to start the pubsub. Probably using a Supervisor.</p>
<p>Here’s an example from one of my projects.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule EventServer.Supervisor do
  @moduledoc false

  use Supervisor

  #
  # client
  #

  def start_link() do
    Supervisor.start_link(__MODULE__, :ok, name: __MODULE__)
  end

  def start_link([]), do: start_link()

  #
  # callbacks
  #

  def init(:ok) do
    Supervisor.init(children(), strategy: :one_for_one)
  end

  #
  # private
  #

  defp children() do
    [
      supervisor(Phoenix.PubSub.PG2, [ EventServer.PubSub, [] ])
    ]
  end
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="76144" data-batch-url="/posts/batch_likers">
                        2
                      </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/how-to-wrap-phoenix-pubsub/5998/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-76144" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="76144"
                     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 #13"></div>
  </section>
</div>
    <div class="postbit" id="76236" data-post-id="76236">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thank You !</p>
<p>So I updated my code and now have a working example if anyone wants to see:<br>
<a href="https://github.com/mwindholtz/phoenix_pubsub_example" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/mwindholtz/phoenix_pubsub_example</a></p>
<p>I deleted the previous broken spike code since, well it didn’t work.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="76236" 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/how-to-wrap-phoenix-pubsub/5998/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-76236" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="76236"
                     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="76237" data-post-id="76237">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>updated code available here:</p>
<p><a href="https://github.com/mwindholtz/phoenix_pubsub_example" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/mwindholtz/phoenix_pubsub_example</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="76237" data-batch-url="/posts/batch_likers">
                        6
                      </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/how-to-wrap-phoenix-pubsub/5998/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-76237" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="76237"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-most-liked cat-most-liked" title="One of the top 3 liked posts in this thread!"></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>