Reacting to Data in Your Phosphor Doors: A Beginner's Guide to Bindings
November 14, 2026
In the last tutorial we built the NetStats door and made it repaint on a timer: every 30 seconds the door redrawed its rows. That works, and a lot of doors live happily that way.
But a timer is a guess. The door repaints whether anything changed or not, and — worse — it repaints late: the post lands, and up to 30 seconds later you see it.
This tutorial is about the other way to build a live door: bindings. You declare where your data comes from, and the door wakes up exactly when the data changes. No timer, no polling, no guessing.
Phosphor now ships a real, registered binding source you can use today — board_post_count, which counts the posts in a message board and pushes an update every time one of them changes. We’ll build a little “Board Watch” door with it, and by the end you’ll know every moving part.
What is a binding, really?
Three words, three jobs:
bind— “get my data from a named source.” You writebind posts = board_post_count("General"), and from then on the variablepostsholds the number of posts in the General board — now, and every time it changes.computed— “derive something from my data.”computed summary = "posts " + postsis a little formula that is recomputed automatically whenever any binding it mentions changes. You never ask it to update; it just is.on update— “what to do when that happens.” The body ofon updateruns once per change, with fresh values already waiting in every variable. This is the callback you were promised.
If you have done any spreadsheet, this is the mental model: bind is a cell linked to a data feed, computed is a formula row, and on update is the “on change” script that fires when a linked cell moves.
The whole example
Here is a complete door. It renders a small rounded box with a scrolling log line, and every new post in the General board becomes one more line in that log:
door "Watch" {
layout {
region id = "box" {
border = "rounded"
title = "Watch"
list id = "log" {}
}
}
bind posts = board_post_count("General")
computed summary = "posts " + posts
on update { append("log", summary) }
on key 'Q' { back() }
}
Read it top to bottom:
- The layout is the door’s skeleton — one bordered region holding an empty
listwidget. (Lists grow downward as youappendrows.) - The bind names our data source:
poststracksboard_post_count("General"). - The computed derives a display string from that number.
- The
on updatehandler appends one log line — the recomputed summary — to the list. - The
on key 'Q'gives the door an exit.
When someone posts to General, this happens under the hood:
post saved in the DB
→ the board_post_count source notices
→ it re-counts the board's posts
→ it informs the door that "posts" changed
→ the door re-runs the computed ("posts 3")
→ your on update handler runs ONCE with the fresh values
→ the door marks itself for repaint
→ the host paints the new frame
You did none of that. You declared what your data was and what should happen on a change. That’s the whole deal.
The rules of the road
A few laws the implementation follows — knowing them saves you from surprises:
One fire per change — and only on a real change. The source compares the fresh count against the last one it saw. Two events on the same logical update arrive as one callback. And activity in other boards never wakes your door: bind posts = board_post_count("Tech") sleeps while General is on fire.
Fresh environment. When on update runs, every bind carries its new value and every computed is re-evaluated over the new snapshot. You never “fetch” anything in the handler — the variables are already current.
Threads are not posts. board_post_count counts posts: creating an empty thread in the board changes nothing (it fires nothing); the first post inside it does. This is pinned by a test, and it matters when you design a “new activity” door — watch posts, or watch threads, but say which one you mean.
Junk never throws. Bind to a board name that doesn’t exist and you get a quiet 0 and a door that still renders. A source that can’t be reached leaves the door working, just not updating. A bad argument never crashes your door.
The door cleans up after itself. When the door closes, its subscriptions close too — no leaks, no late callbacks arriving at a dead door.
Registering a source (for plugin authors)
board_post_count is a house builtin, registered by the server itself. Your own data sources ride the same wire — a plugin annotates a class that implements ReactiveBinding, and the server registers it at boot:
@PhosphorBinding(
name = "stock_price",
description = "The last trade price for a ticker")
public static class StockPriceBinding implements ReactiveBinding {
private final String symbol;
public StockPriceBinding(String symbol) { this.symbol = symbol; }
public Object currentValue() { return PriceFeed.getPrice(symbol); }
public void addChangeListener(Consumer<Object> listener) { listeners.add(listener); }
public void removeChangeListener(Consumer<Object> listener) { listeners.remove(listener); }
public void close() { PriceFeed.unsubscribe(symbol); }
}
A ReactiveBinding is a small object with three jobs: report its current value (currentValue()), accept listeners (addChangeListener — call each listener with the new value when your data changes), and let go of its resources (close()). The door’s start() sequence finds your source, subscribes, and everything from the “three words” section works unchanged.
Good first sources to consider: watch counts for other parts of the board (mail, files), a clock, chat activity, anything with a natural “it changed” moment.
Cheat sheet
| You want… | You write… |
|---|---|
| Subscribe to a data source | bind name = source(args) |
| Derive a display value | computed name = expression |
| React to a change | on update { … } (runs once per change) |
| Board post activity | bind p = board_post_count("General") |
| New topics (threads) | bind t = board_thread_count("General") |
| Unread threads for this user | bind u = board_unread_posts("General", user_id) |
| Unread mail | bind m = mail_unread_count(user, host) |
| Who’s online | bind n = online_user_count() · the names: bind who = online_usernames() |
| Chat activity in one channel | bind c = chat_message_count("#lobby") |
| Read on a timer (no event) | on every 60s { update("x", "age " + board_last_post_age_minutes("General") + "m") } |
| Other timer-read facts | mail_last_received_age_minutes(user, host), chat_last_message_age_minutes(c), file_count(), news_item_count(), server_uptime_hours() |
| Draw the new value | update("widgetId", text) or append("listId", line) |
| Give up cleanly | on key 'Q' { back() } |
Try it: the Watch door’s source (board_post_count) is live on this board today. Post somewhere you’re watching, and the door repaints the instant it lands.
The laws in this post are pinned by code: the binding’s own test suite (six laws, real database) and an end-to-end law that mounts the Watch door, writes a post, and asserts the fresh value arrives in the frame. If the tutorial and the code ever disagree, code wins — file it in the Dev Log.
Your own status bar: user scripts
Doors run full-screen, but you don’t need Java or a door to put a live fact
on your status bar. The BBS accepts user scripts — a few lines stored with
your account by a sysop, loaded when you log in and unloaded when you leave.
The grammar is the event handler you saw above, prefixed with user_handler:
user_handler on mail_received {
set("mail", "✉ 2")
}
user_handler on login {
set("hi", "hey!")
}
The set(slot, text) builtin writes into your personal status-bar strip:
up to three slots, a few characters each, appended after the screen’s own
status text. Oversized text clips; a fourth slot is politely ignored — the
server caps silently rather than erroring your script. Your slots clear when
you log out. The bar repaints on the screen’s next refresh, so a keystroke
or menu change is usually enough to show your newest value.
The rules of the road apply here too: set is the only render verb, the
servers owns the paint (your script never touches a component), and anything
without a natural “it changed” moment — a clock, an age — stays a job for the
on every side of the house, not a slot you set once.
This section grew into its own post: Your Own Status Bar — user scripts and the set() builtin, with the slot bounds (3 slots × 8 columns in a 24-column zone) verified straight from the source.