ream88

ream88

Durable_stash - LiveView state that survives navigation, crashes, and redeploys

What happens when two of the most interesting recent Elixir packages join together? Introducing durable_stash, our vibecoded approach to fixing one of the most annoying Phoenix LiveView problems: your assigns keep dying. Navigate away and they’re gone. And after every deploy, that setting your user carefully configured five minutes ago is back to its default.

The two packages in question:

  • LiveStash (Software Mansion) has a lovely API for stashing and recovering assigns. But the stock ETS adapter only recovers on reconnects, deletes the stash on every fresh mount (by design, their README says to use URL params for navigation), and like everything in BEAM memory it dies on deploy.
  • DurableServer (Chris McCord) gives you durable processes persisted to S3-compatible object storage. Durable Objects, basically, but on the BEAM.

durable_stash is a LiveStash adapter backed by DurableServer. One durable process per browser session, shared by every LiveView of that session:

defmodule MyAppWeb.SomeLive do
  use MyAppWeb, :live_view
  use LiveStash, adapter: DurableStash, stored_keys: [:count, :username]

  def mount(_params, _session, socket) do
    socket = assign(socket, count: 0, username: nil)
    {_status, socket} = LiveStash.recover_state(socket)
    {:ok, socket}
  end
end

A plug drops a random sid into the cookie session, the adapter hashes it into a storage key, and from then on your stored assigns survive live navigation, Wi-Fi hiccups, LiveView crashes, and full redeploys. Cleared cookies or another browser means defaults again. Which is the point. It’s session state, not a database.

Some details I’m happy with:

  • Each stored key declares a recovery scope, because settings and form drafts want different policies:
stored_keys: [
  theme: :session,    # recovers on every mount: navigation, crashes, deploys
  draft: :reconnect   # recovers only on rejoins; clears on fresh navigation
]

A :reconnect draft survives a deploy mid-edit (the browser stays on the page and rejoins), but navigating to the form fresh starts blank. That’s stock LiveStash semantics, per key, with deploy survival on top.

  • Writes are per-key diffs merged by a single actor, so two tabs writing different keys can’t clobber each other. We looked at CRDTs for this and rejected them: one process per session means there’s nothing to merge.
  • Values are JSON-normalized at stash time. What you recover in dev is byte-for-byte what you’d recover after a redeploy in prod, so you don’t get atom keys in dev and string keys in prod.
  • There’s a vsn/migrate option for when your stored shape changes.
  • It ships an in-memory DurableServer.StorageBackend with proper etag CAS, so your tests need neither S3 nor LocalStack.

About “vibecoded”: yes, Claude wrote most of it. The test suite is real though: 40 tests including a deploy simulation (two DurableServer supervisors sharing one backend, stop the first, recover on the second) and actual Phoenix.LiveViewTest lifecycle coverage. We also ran it against a local S3 container and killed the VM between write and read.

It’s a 0.1 and young. On the roadmap: atomic update/3 inside the session actor, PubSub broadcasts for live multi-tab convergence, TTL/idle-stop, and a :permanent scope for user-scoped (not session-scoped) keys.

Hex: durable_stash | Hex
Docs: durable_stash v0.1.1 — Documentation
GitHub:

Feedback very welcome, especially from anyone using LiveStash or DurableServer in anger.

Where Next?

Popular in Announcing Top

OvermindDL1
I created a new library (rather I pulled out a couple files from my big project), it manages an operating system PID file for the BEAM. ...
New
seancribbs
Today I released a new dialyzer Mix task as the dialyzex package! At the time we started writing this task, the existing dialyzer integra...
New
brainlid
LangChain is short for Language Chain. An LLM, or Large Language Model, is the “Language” part. This library makes it easier for Elixir a...
New
mbuhot
Leverage Open Api 3.0 (Swagger) to document, test, validate and explore your Plug and Phoenix APIs. Generate and serve a JSON Open API ...
New
Qqwy
Hello everyone, I wrote a small library today called MapDiff. It returns a map listing the (smallest amount of) changes to get from map...
New
mindok
What is ContEx? A pure Elixir server-side data plotting/charting library outputting SVG. It has nice barcharts in particular and works g...
New
josevalim
Hello everyone, We have just released NimbleCSV which is a small and fast CSV parsing library for Elixir. It allows developers to define...
New
mplatts
With HEEX released we decided to start a components library using Tailwind CSS - check it out here: Petal Components. We also have a boi...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
trisolaran
Hi! :waving_hand: I would like to present LiveSelect, a little library that I wrote to easily add a dynamic selection input to your LV f...
198 10978 107
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement