TodoMVC with Datastar
The video is out of date
The video was recorded with Datastar v1.0.0-RC.5. The tutorial and its code examples below work with Datastar v1.0.5.
This tutorial demonstrates the hypermedia/immediate mode approach to web development: backend-first development where the server drives UI updates by streaming HTML fragments to the client. We'll use Datastar for reactive frontend bindings.

- Live demo: try it live, a historical snapshot using Datastar v1.0.0-RC.5.
- Original guide: Datastar's TodoMVC example, the original implementation this is based on.
- Source code: view on GitHub, the complete working example.
What You'll Learn
- Event sourcing with xs streams
- Server-driven UI updates via Server-Sent Events streaming HTML fragments, no client-side state management
- Datastar reactive bindings
- Hypermedia principles: server owns all state, UI is pure projection
Prerequisites
- xs running
- minijinja-cli for templating
- http-nu for serving
Add a Todo
What may not be intuitive about event sourcing - and by extension, Datastar's hypermedia approach - is when you have an idea, you don't start with the UI. You start with the event stream. I like to call this "stream driven development". So, we're making a todo app? Let's start by adding a todo event to the stream and build from there:
. todos --meta
───────┬─────────────────────────────────────────────────────
topic │ todos
id │ 03elvd78yq84vp6botmro8jyt
hash │
meta │ ────────┬──────────────────────
│ action │ add
│ text │ Learn event sourcing
│ ────────┴──────────────────────
ttl │ forever
───────┴─────────────────────────────────────────────────────
See the Event
Check what's in the store:
.cat | where topic ==
─#─┬─topic─┬────────────id─────────────┬─hash─┬──────────────meta───────────────
0 │ todos │ 03elvd78yq84vp6botmro8jyt │ │ ────────┬──────────────────────
│ │ │ │ action │ add
│ │ │ │ text │ Learn event sourcing
│ │ │ │ ────────┴──────────────────────
───┴───────┴───────────────────────────┴──────┴─────────────────────────────────
Add Another Todo
Now let's add a second todo and capture the returned frame:
let todo = . todos --meta
$todo
───────┬─────────────────────────────────────────────────────
topic │ todos
id │ 03elvd8txt25bf13qyggp0cdp
hash │
meta │ ────────┬──────────────────────
│ action │ add
│ text │ Build something cool
│ ────────┴──────────────────────
ttl │ forever
───────┴─────────────────────────────────────────────────────
Completing a Todo
OK, now let's mark that second todo as complete. With event streams, you express updates by appending more events. We'll make use of metadata to point to the frame we are modifying:
. todos --meta
───────┬─────────────────────────────────────────────────────
topic │ todos
id │ 03elvdlutpeeusqvf1xfdf7ig
hash │
meta │ ────────┬───────────────────────────
│ action │ toggle
│ id │ 03elvd8txt25bf13qyggp0cdp
│ ────────┴───────────────────────────
ttl │ forever
───────┴─────────────────────────────────────────────────────
Check the stream:
.cat | where topic ==
─#─┬─topic─┬────────────id─────────────┬─hash─┬──────────────meta───────────────
0 │ todos │ 03elvd78yq84vp6botmro8jyt │ │ ────────┬──────────────────────
│ │ │ │ action │ add
│ │ │ │ text │ Learn event sourcing
│ │ │ │ ────────┴──────────────────────
1 │ todos │ 03elvd8txt25bf13qyggp0cdp │ │ ────────┬──────────────────────
│ │ │ │ action │ add
│ │ │ │ text │ Build something cool
│ │ │ │ ────────┴──────────────────────
2 │ todos │ 03elvdlutpeeusqvf1xfdf7ig │ │ ────────┬───────────────────────────
│ │ │ │ action │ toggle
│ │ │ │ id │ 03elvd8txt25bf13qyggp0cdp
│ │ │ │ ────────┴───────────────────────────
───┴───────┴───────────────────────────┴──────┴─────────────────────────────────
You now have three events: two "add" events and one "toggle" event.
Aggregated View
Raw events are awkward for presenting a summary of the current state of things. We want to create a projection of the stream. We can use Nushell's generate command to aggregate events into state:
.cat | where topic == | |
─#─┬────────────id─────────────┬─────────text─────────┬─completed─
0 │ 03elvd78yq84vp6botmro8jyt │ Learn event sourcing │ false
1 │ 03elvd8txt25bf13qyggp0cdp │ Build something cool │ true
───┴───────────────────────────┴──────────────────────┴───────────
The generate command processes each event to build current state:
- Start with empty todos list -
todos = [] - Process each frame - for every event in the stream
- "add" events - append new todo with
completed: false - "toggle" events - find matching ID and flip completion status
- Output current state -
{out: $in next: $in}emits the updated list - Get final result -
| lastgives us the current aggregated state
This shows the current aggregated state: a list of todos with their completion status.
Real-Time Aggregation
Let's see the aggregated projection update in real-time. In a second terminal window, run the same generate command with live streaming:
.cat -f | where topic == | |
─#─┬────────────id─────────────┬─────────text─────────┬─completed─
0 │ 03elvd78yq84vp6botmro8jyt │ Learn event sourcing │ false
───┴───────────────────────────┴──────────────────────┴───────────
─#─┬────────────id─────────────┬─────────text─────────┬─completed─
0 │ 03elvd78yq84vp6botmro8jyt │ Learn event sourcing │ false
1 │ 03elvd8txt25bf13qyggp0cdp │ Build something cool │ false
───┴───────────────────────────┴──────────────────────┴───────────
─#─┬────────────id─────────────┬─────────text─────────┬─completed─
0 │ 03elvd78yq84vp6botmro8jyt │ Learn event sourcing │ false
1 │ 03elvd8txt25bf13qyggp0cdp │ Build something cool │ true
───┴───────────────────────────┴──────────────────────┴───────────
You see three outputs - one for each event processed so far. Now toggle that same todo back to incomplete:
. todos --meta
In your watching terminal, you immediately see the new aggregated state:
─#─┬────────────id─────────────┬─────────text─────────┬─completed─
0 │ 03elvd78yq84vp6botmro8jyt │ Learn event sourcing │ false
1 │ 03elvd8txt25bf13qyggp0cdp │ Build something cool │ false
───┴───────────────────────────┴──────────────────────┴───────────
Notice both todos now show as incomplete. The aggregated projection automatically updated in real-time when the new event arrived - this is how reactive systems work with event streams.
HTML Visualization
Now let's visualize this projection as HTML fragments. First, save a Jinja2 template:
|
minijinja-cli
minijinja-cli is a fantastic tool that converts JSON to HTML using Jinja2 templates at the command line. In our example:
todo_template.html- the template file-f json- expect JSON input format-- read the data that will be fed to the template from stdin
{
} | | |
<ul id="todos">
<li data-completed="false">
<label>
<input type="checkbox" disabled />
Test todo
</label>
</li>
<li data-completed="true">
<label>
<input type="checkbox" checked disabled />
Done todo
</label>
</li>
</ul>
Now run the live stream with HTML rendering:
.cat -f | where topic == | |
<ul>
<li data-completed="false">
<label>
<input type="checkbox" />
Learn event sourcing
</label>
</li>
</ul>
<ul>
<li data-completed="false">
<label>
<input type="checkbox" />
Learn event sourcing
</label>
</li>
<li data-completed="false">
<label>
<input type="checkbox" />
Build something cool
</label>
</li>
</ul>
...
Now toggle the todo several more times and watch the HTML fragments update in real-time:
. todos --meta
. todos --meta
. todos --meta
Each append immediately triggers a new HTML fragment in your watching terminal, showing the updated todo states.
Server-Sent Events Endpoint
Now let's make our todo list available over HTTP. Save this closure to a file called serve.nu:
# Point this to wherever you've installed the xs.nu module
use ~/./nushell/modules/xs.nu *
Run the server:
|
Test the SSE endpoint with curl:
You'll see HTML fragments streaming in Datastar's SSE format as events flow through the todos stream.
Finally, a little Datastar

Create an index.html that uses Datastar for reactive updates:
TodoMVC with Datastar
TodoMVC with Datastar
Loading todos...
When the #todos element loads, data-init executes the @get action to connect to our /updates Server-Sent Events endpoint. Datastar receives the streamed HTML fragments and replaces the #todos element whenever the todo state changes.
Visit http://localhost:3001 and then in your terminal try appending some more events:
Watch as each event immediately updates the web interface - this is hypermedia-driven reactive programming in action!
Complete the UI Loop
Let's add an input field so users can add todos directly from the web interface. First, update the index.html to include an input field:
TodoMVC with Datastar
Loading todos...
The input uses data-signals:input to create a $input signal, data-bind:input to sync the field value with the signal, and data-on:keydown to POST the todo text to our /add endpoint when Enter is pressed, then clear the input field.
Now add the /add endpoint to your serve.nu by adding this case to the match block (you'll need to restart http-nu after updating the file):
=>
This endpoint parses the JSON body to extract the input field (Datastar sends {"input": "todo text"}), appends a new todo event to the stream, and returns status 204.
Now the reactive loop is complete. When users type a todo and press Enter:
- Browser → POST to
/addendpoint with the todo text - Server →
.append todos --meta {action: "add" text: $text}creates a new event in the stream - SSE endpoint →
/updatesdetects the new event via.cat -f | where topic == "todos" - Stream processing → Generate command aggregates the new state and renders HTML
- Browser update → Datastar receives the SSE event and updates the
#todoselement
All connected browsers update simultaneously because they're all listening to the same /updates SSE stream.