<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title><![CDATA[Krasen Penchev]]></title>
<description><![CDATA[Krasen Penchev]]></description>
<link>https://krasen.be</link>
<atom:link href="https://krasen.be/rss.xml" rel="self" type="application/rss+xml" />
<lastBuildDate>Wed, 02 Dec 2026 00:00:00 +0000</lastBuildDate>
<item>
  <title><![CDATA[Really Stream Into Your LiveView Streams]]></title>
  <description><![CDATA[
<p>
Almost every Elixir project I've worked on used Phoenix LiveView
streams to display an infinite list of data. Usually, the LiveView
process queries the database directly and applies pagination. Other
times, some internal interface calls the database, but, again, the
LiveView or, God forbid, a live component is responsible for passing
the correct pagination options.
</p>
<div id="outline-container-org0000001" class="outline-2">
<h2 id="org0000001">Streaming interface</h2>
<div class="outline-text-2" id="text-org0000001">
<p>
At the business-logic boundary, we can imagine the following function
for streaming blog posts.
</p>

<div class="org-src-container">
<pre class="src src-elixir"><code><span class="org-keyword">import</span> <span class="org-type">Ecto.Query</span>

<span class="org-keyword">def</span> <span class="org-function-name">stream_posts</span>(batch_size) <span class="org-keyword">do</span>
  <span class="org-type">Stream</span>.resource(
    <span class="org-keyword">fn</span> <span class="org-keyword">-&gt;</span> %{<span class="org-elixir-atom">page:</span> <span class="org-elixir-number">0</span>, <span class="org-elixir-atom">size:</span> batch_size} <span class="org-keyword">end</span>,
    <span class="org-keyword">fn</span> state <span class="org-keyword">-&gt;</span>
      <span class="org-variable-name">posts</span> =
        list_posts_query()
        <span class="org-keyword">|&gt;</span> offset(state.page * state.size)
        <span class="org-keyword">|&gt;</span> limit(state.size)
        <span class="org-keyword">|&gt;</span> <span class="org-type">Repo</span>.all()

      <span class="org-keyword">case</span> posts <span class="org-keyword">do</span>
        [] <span class="org-keyword">-&gt;</span> {<span class="org-elixir-atom">:halt</span>, state}
        <span class="org-constant">_</span> <span class="org-keyword">-&gt;</span> {posts, %{state | <span class="org-elixir-atom">page:</span> state.page + <span class="org-elixir-number">1</span>}}
      <span class="org-keyword">end</span>
    <span class="org-keyword">end</span>,
    <span class="org-keyword">fn</span> <span class="org-constant">_</span> <span class="org-keyword">-&gt;</span> <span class="org-elixir-atom">:ok</span> <span class="org-keyword">end</span>
  )
<span class="org-keyword">end</span>
</code></pre>
</div>

<p>
The <code>stream_posts/1</code> function is intentionally simplistic. In a
production codebase you'd most probably want to use the <a href="https://use-the-index-luke.com/sql/partial-results/fetch-next-page">seek method</a>
for pagination because it's much faster for use cases like this. The
data might not come from your application's database at all, or only
partially. Calls to external services impose more elaborate error
handling. Enough excuses already! The user interface is the
interesting part anyway.
</p>
</div>
</div>
<div id="outline-container-org0000002" class="outline-2">
<h2 id="org0000002">Long live the caller</h2>
<div class="outline-text-2" id="text-org0000002">
<p>
For our purposes, the caller of our streaming interface is a LiveView
process. In the <code>mount/3</code> function we need to call <code>stream_posts/1</code>,
take the first batch of posts, and stream them to the client.
</p>

<div class="org-src-container">
<pre class="src src-elixir"><code><span class="org-elixir-attribute">@query_size</span> <span class="org-elixir-number">10</span>
<span class="org-elixir-attribute">@client_size</span> <span class="org-elixir-number">10</span>

<span class="org-keyword">def</span> <span class="org-function-name">mount</span>(<span class="org-constant">_</span>, <span class="org-constant">_</span>, socket) <span class="org-keyword">do</span>
  <span class="org-comment-delimiter"># </span><span class="org-comment">`posts_list` is a nested list and needs unwrapping later.
</span>  {posts_list, posts_stream} =
    stream_posts(<span class="org-elixir-attribute">@query_size</span>)
    <span class="org-keyword">|&gt;</span> <span class="org-type">Stream</span>.chunk_every(<span class="org-elixir-attribute">@client_size</span>)
    <span class="org-keyword">|&gt;</span> <span class="org-type">StreamSplit</span>.take_and_drop(<span class="org-elixir-number">1</span>)

  <span class="org-variable-name">socket</span> =
    socket
    <span class="org-keyword">|&gt;</span> assign(<span class="org-elixir-atom">:posts_stream</span>, posts_stream)
    <span class="org-keyword">|&gt;</span> stream(<span class="org-elixir-atom">:posts</span>, <span class="org-type">List</span>.first(posts_list, []))

  {<span class="org-elixir-atom">:ok</span>, socket}
<span class="org-keyword">end</span>
</code></pre>
</div>

<p>
The query size and the client size are equal, but they don't have to
be. A query size that is double the client size means that every
second batch is prefetched and already in memory when needed.
</p>

<p>
You might be wondering about <a href="https://stream-split.hexdocs.pm/StreamSplit.html#take_and_drop/2"><code>StreamSplit.take_and_drop/2</code></a>. It enables
us to take the first <i>n</i> elements of the stream <i>while</i> also returning
the rest of the stream. When the stream runs out, the taken list is
empty and streaming an empty list is a no-op.
</p>

<p>
Rather than tying the batch request event name to a particular
LiveView version or a client hook, let's assume the process receives a
<code>"load-more"</code> event. Processing such an event is trivial now. It
consists of taking the next batch off the stream, streaming it to the
client, and storing the rest.
</p>

<div class="org-src-container">
<pre class="src src-elixir"><code><span class="org-keyword">def</span> <span class="org-function-name">handle_event</span>(<span class="org-string">"load-more"</span>, <span class="org-constant">_</span>, socket) <span class="org-keyword">do</span>
  {posts_list, posts_stream} =
    <span class="org-type">StreamSplit</span>.take_and_drop(socket.assigns.posts_stream, <span class="org-elixir-number">1</span>)

  <span class="org-variable-name">socket</span> =
    socket
    <span class="org-keyword">|&gt;</span> assign(<span class="org-elixir-atom">:posts_stream</span>, posts_stream)
    <span class="org-keyword">|&gt;</span> stream(<span class="org-elixir-atom">:posts</span>, <span class="org-type">List</span>.first(posts_list, []))

  {<span class="org-elixir-atom">:noreply</span>, socket}
<span class="org-keyword">end</span>
</code></pre>
</div>

<p>
As you can see, there is no tracking of the last loaded batch, the
query size, or the client size. The stream is handled exactly as it is
in <code>mount/3</code>.
</p>
</div>
</div>
<div id="outline-container-org0000003" class="outline-2">
<h2 id="org0000003">Parting thoughts</h2>
<div class="outline-text-2" id="text-org0000003">
<p>
This technique for managing client-facing streams of data completely
decouples the data querying from the presentation logic. The LiveView
process shouldn't be, and isn't, concerned with any Ecto repository,
the pagination method, or whether the data comes from an external
service. The technique is also very much to my taste because it's
functional. The stream returned by the business logic is treated as a
value, a purpose-built data structure, not scattered values assembled
as arguments when needed.
</p>
</div>
</div>
<div class="taglist"><a href="https://krasen.be/tags.html"><span class="tag-label">Tags</span></a><span class="tag-separator">: </span><span class="taglist__tags"><a href="https://krasen.be/tag-elixir.html" class="tag" data-tag="elixir" data-index="0">elixir</a> </span></div>]]></description>
  <category><![CDATA[elixir]]></category>
  <link>https://krasen.be/really-stream-into-your-live-view-streams.html</link>
  <guid>https://krasen.be/really-stream-into-your-live-view-streams.html</guid>
  <pubDate>Wed, 02 Dec 2026 00:00:00 +0000</pubDate>
</item>
</channel>
</rss>
