On a live page, parts of the page update when the data behind them
changes. Live-page support is already built into the app; you add the
part that is specific to yours.
A topic is the name of something that changes: here room/7,
for room 7's messages. Declare it in app.go, with who may follow
it: anyone signed in.
A region is a part of the page tied to a topic. In room.html,
the message list becomes region:messages.
Notify after the write. When postMessageAt saves a message, it
tells the hub that the room's topic moved.
Every browser showing that room then fetches the region again through
the same handler, with the permissions of the person viewing it. Only
the topic name goes down the live stream, never the messages
themselves.
With data-live-form, the form is sent without a page load, and the
regions it names are swapped in from the response. The form is in a
region of its own, so the box is cleared once a message is sent. With
scripts turned off, everything still works as ordinary pages.
Check it worked
With the room open in two windows, a message sent in one appears in the other without a reload, and the box you typed in clears.
🤖 These pages were written by an LLM, on the ideas, instruction, and editing of humans. AI-written text here is always marked, visibly — that's factor X.
app.go Edit
internal/chat/app.go
package chat
import (
"context""log/slog""net/http""time""github.com/go-chi/chi/v5""amadan.net/rastrillo/rastrillo""amadan.net/rastrillo/rastrillo/action""amadan.net/rastrillo/rastrillo/auth""amadan.net/rastrillo/rastrillo/background""amadan.net/rastrillo/rastrillo/db""amadan.net/rastrillo/rastrillo/live""amadan.net/rastrillo/rastrillo/mail""amadan.net/rastrillo/rastrillo/migrate""amadan.net/rastrillo/rastrillo/perf""amadan.net/rastrillo/rastrillo/signin"// Here, not in main, so the app's tests link the packs too. _ "chat/gen/locales")
type App struct {
database *db.DB
logger *slog.Logger
origin string api http.Handler
mcp http.Handler
live *live.Hub
auth *auth.Auth
signins *signin.Signins
}
// New migrates the database and builds the app, as one router inside the// ServeMux that rastrillo.Options.Mux takes.// Stop group before closing the database: sign-in's sweeps run on it.func New(
database *db.DB, origin string, group *background.Group,
mailer mail.Sender, logger *slog.Logger,
) (*http.ServeMux, *live.Hub, error) {
router, hub, err := Router(database, origin, group, mailer, logger)
if err != nil {
returnnil, nil, err
}
mux := http.NewServeMux()
mux.Handle("/", router)
return mux, hub, nil}
// Router is New's router, apart so that the perf test can walk it.func Router(
database *db.DB, origin string, group *background.Group,
mailer mail.Sender, logger *slog.Logger,
) (chi.Router, *live.Hub, error) {
if pagesErr != nil {
returnnil, nil, pagesErr
}
// BootSchema, not Schema: migrations.go says why. migrated, err := migrate.Apply(context.Background(), database, BootSchema)
if err != nil {
returnnil, nil, err
}
logger.Info("migrate", "applied", migrated.Applied,
"skipped", migrated.Skipped, "adopted", migrated.Adopted)
api, mcp, err := newDoors(database, logger)
if err != nil {
returnnil, nil, err
}
app := &App{database: database, logger: logger, origin: origin,
api: api, mcp: mcp}
if err := app.startAuth(group, mailer); err != nil {
returnnil, nil, err
}
app.live, err = live.New(live.Config{
Path: string(livePage),
Topics: map[string]live.Allow{
"room": anyoneSignedIn,
},
})
if err != nil {
returnnil, nil, err
}
router := chi.NewRouter()
// First, so a page never misses a write. router.Use(app.live.Middleware)
Routes(action.NewRoutes(router, app.contextFor), app)
return router, app.live, nil}
// Configure sets the serving options for main.go and the perf test, so// what the perf lane measures is what production serves.func Configure(
options *rastrillo.Options, mux *http.ServeMux, hub *live.Hub,
started time.Time,
) {
options.Mux = mux
options.Live = hub
options.ErrorPage = ErrorPage
var recorder perf.Recorder
options.Wrap = perf.Middleware(&recorder,
perf.Options{Started: started, Logger: options.Logger})
}
live.go New
internal/chat/live.go
package chat
import (
"net/http""strconv""amadan.net/rastrillo/rastrillo/auth")
// anyoneSignedIn may follow any room: every room is open to everyone// who has signed in.func anyoneSignedIn(request *http.Request, _ string) bool {
_, signedIn := auth.From(request)
return signedIn
}
// roomTopic names one room's messages. The page that shows them follows// it, and posting a message moves it.func roomTopic(id int64) string {
return"room/" + strconv.FormatInt(id, 10)
}