Sidebar

Displays the mobile sidebar.

Search the docs

Search the docs

charter
charter
What charter is
See it running
Where to go
What an app made with charter looks like
Getting started
Getting started
Working on it
Before a push
Shipping it
Tools
Every command
The commands, and what each one proves
The commands, and what each one proves
What is checked, and when you find out
What no compiler checks
create
dev
add
check
test
deploy
pack
run
The rest
An app's layout
An app's layout
What the Worker is bound to
main.go
What is generated
What is never in git
Pages
Pages
With the commands
The tree
A page declares what it needs
A region that changes by itself
The page being shown
A fragment opened as a page
What a page calls
Links
A link shows the next page with no page load
Which pages search engines get
A page that is the same for every reader
The head
The API
The API
With the commands
An operation
Who may call
Limits
SDKs
MCP
The live feed
The live feed
With the commands
Writing
Three ways out, one feed
A page's stream holds no Go
Resuming
The database
The database
With the commands
Changing it
Queries
Every statement is checked
One definition
Seeding
A deployed database
Reads near the reader
By hand
Signing in, and who may do what
Signing in, and who may do what
With the commands
One caller
Saying what is needed
Roles
The first admin, and inviting people
Signing in
Another device
An address to get back in by
Testing it
The clients
What is left to you
Languages and search engines
Languages and search engines
With the commands
Languages
Search engines
Agents
Screens and themes
Screens and themes
With the commands
The interface is gsxui's
Screens
The layout
What a page is made of
Themes
The editor, in your app
A style
While developing
Light and dark
What is checked, and what is not
Docs
Docs
With the commands
Groups, and the order
What you write, and what charter writes
Links are checked
Showing them
What comes with them
What the pages have
Code taken from a real file
Tests
Tests
A page, as plain Go
The whole app, on a real Worker
A page, in a real browser
What to test, and what not to
The API, through its own SDK
Where a test runs
A test leaves the Worker as it found it
Secrets a test needs
What else charter test does
Deploying
Deploying
Two Workers
With the commands
The plan
Nothing of your account in the repository
Credentials
Secrets
Resources
Taking an app away
Your own domain
Email
After a deploy
Environments
Everything Cloudflare gives a Worker
The admin screen
The admin screen
The pages
The app's own facts
From a program
What it does not do
Watching a deployed Worker
Watching a deployed Worker
Is it well?
Three numbers
Cloudflare's limits
Under load
A first hit
Every data centre, by country
What happened?
A failure has a reference
Memory
What charter leaves to Cloudflare
Upgrading
Upgrading
0.82
0.81
0.80
0.79
0.78
0.77
0.76
0.75
0.74
0.73
0.72
0.71
0.69
0.65
0.64
A binding, a variable, a secret's name
A scope
Screens
0.63
When upgrade stops
For your coding agent
For your coding agent
The plugins
In an app made with charter
Anywhere else
One set of commands
What an agent can ask
What a language model reads
What the Worker is bound to
What the Worker is bound to

The live feed

A feed is declared once and serves everything that shows it.

example/api/handlers.go
// Notes is the feed of notes, declared once: the table is the log, the Durable Object HUB wakes whoever follows.
// The page's live list, the API's stream and its WebSocket channel are all this.
var Notes = live.Feed[Note]{Binding: env.HUB, Name: "notes", Log: func() (live.Log[Note], error) { return open() }}

The log, a D1 table, is the truth. The hub, a Durable Object, only wakes whoever follows. So no item is lost or sent twice, across reconnects and deploys alike.

A change that is no new item is said too, so that every open page shows it:

if err := store.Remove(ctx, id); err != nil {
	return err
}
Notes.Changed(ctx)

With the commands

WhenCommandWhat it does for a feed
You declare a feeddev, runningit compiles: a live.Feed[Note] is of one type, for the page, the API and the SDK
Before a pushtesta note written in one browser tab appears in another, and one taken away goes from both
Before a releaserun load --deployedmany readers' streams held open at once, and whether Cloudflare ended any request

Checked before the app runs: the item's type everywhere the feed goes; the hub it wakes (env.HUB, a Durable Object namespace: any other kind of binding does not compile).

Not checked until it runs: that an item arrives, once, in order, across a reconnect and a deploy. That is the two-tab test and the stream's tests. What many open pages cost in memory shows only on Cloudflare: run load.

Writing

example/api/handlers.go
// Post writes a note to the store, as whoever is asking, and wakes every follower: what the API's createNote and
// the page's form both do.
func Post(ctx context.Context, store Store, by charter.Caller, body string) (Note, error) {
	note, err := store.Create(ctx, body, by.Subject, by.Name())
	if err != nil {
		return Note{}, err
	}
	Notes.Publish(ctx, note)
	if OnPost != nil {
		OnPost(ctx, note)
	}
	return note, nil
}

Three ways out, one feed

ForCallWhat it sends
An open pageNotes.Stream() and live.Follows()"what you show has changed": the page asks for that part of itself again, as that reader sees it
An SDK clientNotes.Events(ctx, setHeader, w, positions, seconds, end)each item as JSON over server-sent events
A WebSocketNotes.Lines(ctx, setHeader, w, positions)each item as a line of JSON

Write a note on the example's home page in one tab and it appears in another; take it away and it goes from both. Create one through the SDK and it appears there too.

A page's stream holds no Go

A Go runtime takes 13 MB or more of a Worker's 128 MB for as long as it serves a request, and a stream is a request that lasts minutes. Served by Go, a handful of open pages took a Worker over its memory, and Cloudflare ended it with every request it was answering (a 503 for each; exceededMemory in its analytics).

So a page's stream is the hub's, a small Durable Object in JavaScript, and it says one thing: what you show has changed. The part of the page that shows the feed then asks for itself again, and what comes takes the place of what it had.

<div { api.Notes.Stream()... }>
	<ul
		id={Home.List |> id}
		hx-get={Home{} |> url}
		hx-target={Home.List |> target}
		{ live.Follows()... }
	>
		<p.List notes={props.Notes} reader={props.Caller}/>
	</ul>
</div>

Stream is the stream, on an element around the list. Follows is everything that makes the list keep itself current. What is left in the markup is what is the page's own, from the tree as every other id and address is: the list's id, the page it asks for, and itself as the target.

There is no route for it: the list is a component of its page, so the request is for the page with the list as its target, and structpages renders the list alone (target.Is(p.List) in the page's Props). It is a short, ordinary request: Go renders the rows for that reader, in their language, with the buttons they may use.

Why it is told that something changed, and never what:

  • Added, taken away or altered, it is one thing. The page shows what the log has, so nothing is shown twice or left behind.
  • Changes that come together end right. The last request is the one that counts: however many arrive at once, the page ends as the log is.
  • What the reader was doing stays, when the page says so. What comes is the list as the Worker has it, which knows nothing of what a reader did to a row since: a row they opened would be shut by it. So a row of a list that is asked for again is marked to be kept (hx-preserve, which the example's List gives each Item): htmx leaves the row the page has as it is, and puts in only the rows that are new. A row asked for by itself, by its own button, is not marked: it is the answer.
  • It reads what is newest. The request says so (the header X-D1-Newest), so it is answered by the database's primary: a copy that is behind would show the page as it was before the change it was just told of (reads near the reader).

The stream is on an element of its own, around the list: htmx opens a stream on that element's trigger, and what is replaced is inside it, so the stream stays open across every change.

Nothing is lost when a stream breaks, which every one does when the Worker is deployed: the hub restarts and holds nothing. A page says when it was rendered, and a stream that connects again says when it was last told; the hub answers "changed" at once when something changed since, or when it started since and cannot know.

The API's streams hold no Go either. Events (an SDK client's stream) and Lines (a WebSocket channel) write what the log has after the position their caller gave, say where they stopped, and are done; the hub writes the rest, as Go would have: an event for each item and the terminator at the end, or a line for each. Your handler gives them the answer's SetHeader, which is how they say it:

Notes.Events(hc.Context(), hc.SetHeader, hc.BodyWriter(), []string{in.After, in.LastEventID}, int(in.Seconds), END)

Twenty SDK clients watching for a quarter of a minute hold no Go runtime between them.

Resuming

An API stream's events each have the item's position in the log as their id: an SDK client passes after, or its last event's id, and is sent what came after. A page's stream has the time as its id: a browser sends the last one when it connects again, and is told at once if anything changed since. The log is the truth, so nothing in between is lost.