Reference · Jobs and services

publish

signature
publish(any...) -> null

What it does

Announces that something changed, by topic name, so that every page whose live data watched the topic loads it again. Tables and global variables announce themselves when written (db:notes, var:todos); publish is for everything else: a thread's replies, a file that was rewritten, data another process changed. Several topics can be given at once; each is written as text. It gives null. See Live data.

Examples

in a program
server fn load_posts(thread: int) {    watch(`thread:${thread}`)    unwatched(() => db.query("select * from posts where thread = ?", thread))}server fn reply_to(thread: int, text: str) {    db.exec("insert into posts (thread, text) values (?, ?)", thread, text)    publish(`thread:${thread}`)}
print(publish("thread:7"))print(publish("thread:7", "thread:8"))
output
nullnull

Edge cases

print(publish(), publish(7), publish("a", "b", "c"), publish("search:a,b"))
output
null null null null

Errors

Any number of topics, of any type, is taken, each as its text. An empty topic, or one past 256 bytes, is a runtime error, not an error value.

print(publish(null, true, { thread: 7 }))
output
null
this one fails
print(publish(""))
output
line 1: `publish`: a topic is a name, and this one is empty
this one fails
print(publish(repeat("x", 300)))
output
line 1: `publish`: a topic is at most 256 bytes, and this one is 300

Chaining

publish goes after the write, in the same function, so that a page is woken only when there is something new to load; it gives null, so it cannot be the last line of a function whose value matters.

in a program
server fn rename(id: int, name: str) {    let row = db.one("update projects set name = ? where id = ? returning *", name, id)    publish(`project:${id}`)    row}

See also

watch, unwatched, live, db.transaction

Thread
8Useful? Like it, or add to its thread below.

Thread 0 posts

On publish(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo