Skip to content

silent-brad/hyperdream

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hyperdream

Hyperdream

A web framework for building server-driven, real-time applications as MirageOS unikernels using Datastar for hypermedia exchange over SSE.

Philosophy

  • Server-driven: All rendering happens on the server. No client-side JS framework.
  • Real-time: Every view is a live SSE stream. State changes push instantly to all connected clients.
  • Unikernel-ready: Same code runs on Unix (dev) and as a MirageOS unikernel (prod). Zero filesystem access at runtime.
  • Minimal: Simple map-based router, no middleware chains to debug, no magic.

Core Concepts

Two-Route Page Model

Every page registers two HTTP routes:

  • GET /path → serves a lightweight HTML shell (the “shim”) that boots Datastar and opens the SSE connection
  • POST /path → opens a persistent SSE stream that pushes rendered HTML on every state change
let _view = View.define ~datastar_js hub "/" (fun _req ->
  Lwt.return (Template.render_string tpl ~models:[("count", string_of_int !counter)]))

Actions

Actions are POST handlers with content-addressed paths (SHA256 of the name). When an action returns No_content, the framework automatically broadcasts a refresh to all SSE clients.

let inc_path = Action.define hub "counter/inc" (fun _req ->
  incr counter;
  Lwt.return Action.no_content)

Use the path directly in Datastar attributes:

<button data-on:click="@post('{{ inc_path }}')">+1</button>

SSE Broadcast Hub

The hub manages connected clients with Lwt_condition fan-out. Hash deduplication skips sending when rendered HTML hasn’t changed.

Irmin Store

Optional persistent state via Irmin. Connect a store to a hub and state changes automatically trigger SSE broadcasts:

let* store = Store.create () in
Store.connect_hub store hub;
(* Any Store.set call now triggers Sse.notify_all *)

Asset Embedding

Static files are compiled into the binary at build time via Nushell codegen — required for unikernel targets with no filesystem. Assets get content-addressed URLs with immutable cache headers.

Quick Start

nix develop
dune exec examples/counter/main.exe
# Open http://localhost:8080

Examples

ExamplePortDescription
counter8080Minimal: single counter with SSE push
=game-of-life=8081Conway’s GoL with continuous game loop
todo8082CRUD: Irmin-backed todo list
chat8083Multi-user real-time chat room

Dependencies

LibraryRole
h1HTTP/1.1 protocol
pafMirageOS HTTP server (Protocol-Agnostic Fibers)
datastarDatastar SSE SDK (datastar-sdk-ocaml)
jingooJinja2-compatible templating
irminGit-like database for persistent state
lwtCooperative concurrency
digestifSHA256 for content addressing
yojsonJSON serialization

License

MIT

About

MirageOS/Datastar web framework for server-driven real-time applications

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors