Auto Refresh
Open the same article in two sessions of the Journal. Leave a comment in one and watch the other. We’ll set up the subscription that lets the new comment appear in the open page.
Run the same GET again
Start with the GET that drew the article. Its controller already knows the article, the reader and the sort order. After a PostgreSQL commit, Auto Refresh can run the GET again with that session. If the HTML has changed, the browser receives the new rendering over a WebSocket and applies it with morphdom.
Follow one change to the page
- Open an article in the first tab. While handling that GET, its controller declares a filter and renders the page. Bwf keeps a fingerprint of the HTML it sent.
- The tab attaches the subscription at
/_bwf/live. If a watched table changed before the tab attached, Bwf can run a catch-up GET. - After a commit to a watched table, the controller receives the typed changes and decides whether to reread. A rollback does not trigger an update.
- If the new HTML has a different fingerprint, Bwf sends it to the browser. The browser updates the affected DOM.
Choose the tables for the HTML response
Wrap the HTML branch’s read and render in Live.autoRefreshWith. Name every table that can change what the reader sees. Reading a table inside the block does not add it to the filter automatically. In the Journal you can find the other two pieces in the server startup (Server.serve_with_refresh) and the layout (AutoRefresh.assets()).
Check the old and new values
scripts/bwf migrate installs the triggers. An insert records the new row, a delete records the old one and an update records both. After commit, PostgreSQL sends Bwf a reference to the change. When the controller needs fresh HTML, it reads the current data.
Decide which writes require a read
Start with a filter that accepts every change to the tables you listed. Narrow it when many writes concern rows outside the reader’s list.
Db.watch_lists([Db.table.Article{}, Db.table.ArticleComment{}], _ => _ => True{})
Check both sides of any update that can move a row in or out of the list. A changed count or sort key can also be enough to render again.
Use the types of the watched rows
Db.watch_lists passes one list per table in the order you declared them. In the comment list, Refresh.Update{old, new} contains two Db.ArticleComment values. Bend checks that the filter accepts those types.
def has_insert(comments: Db.change_list(Db.table.ArticleComment{})) -> Bool:
match comments:
case Nil{}: False{}
case Refresh.Insert{_} <> _: True{}
case _ <> rest: has_insert(rest)
def should_refresh(articles: Db.change_list(Db.table.Article{}),
comments: Db.change_list(Db.table.ArticleComment{})) -> Bool:
match articles:
case Nil{}: has_insert(comments)
case _: True{}
def filter() -> Refresh.Filter:
Db.watch_lists([Db.table.Article{}, Db.table.ArticleComment{}], should_refresh)
Use Db.watch when you need the order of changes across tables.
Handle a comment that changed article
If a comment moves from article A to article B, both pages change. Article C has nothing new to display. The filter for the article page is in Controller/Articles/Show.bend.
Live.autoRefreshWith(Api.ArticlePageResponse<Accept.Html{}>,
article_filter(address, id), Api.ArticlePageUnchanged{},
load(context, Accept.Html{}, address))
def article_filter(address: String, article: Sql.Id<"articles">) -> Refresh.Filter:
Db.watch_lists([Db.table.Article{}, Db.table.ArticleComment{}, Db.table.ArticleFavorite{}, Db.table.User{}, Db.table.Follow{}],
should_refresh(address, article))
def should_refresh(address: String, article: Sql.Id<"articles">,
articles: Db.change_list(Db.table.Article{}), comments: Db.change_list(Db.table.ArticleComment{}),
favorites: Db.change_list(Db.table.ArticleFavorite{}), users: Db.change_list(Db.table.User{}),
follows: Db.change_list(Db.table.Follow{})) -> Bool:
match favorites users follows:
case Nil{} Nil{} Nil{}:
Db.any_change(~Db.table.Article{}, ~String, ~matches_address, address, articles) ||
Db.any_change(~Db.table.ArticleComment{}, ~Sql.Id<"articles">, ~comment_matches, article, comments)
case _ _ _: True{}
Db.any_change sees both row versions. A matches the old article_id and B matches the new one. On C’s GET, ArticlePageUnchanged finishes the replay before it has to fetch the article details.
Catch up after a missed change
If a change expires or PostgreSQL reports a table-wide operation such as TRUNCATE, Bwf can read the view again. On reconnect, the browser sends its last fingerprint. Route the first GET and its WebSocket to the same process, since that is where the subscription lives.
Watch the browser receive a comment
- Sign into two accounts in separate browser sessions. Open the same article in both.
- Type a reply in the first window and leave it in the form.
- Post in the second window. The first page should show the new comment and keep the draft.
In the first window’s Network panel, inspect the WebSocket to see the rendered response. For regions with a GET of their own, see Auto Refresh for HTMX fragments.