Reference · Channels

channel.broadcast

signature
channel.broadcast(str, any, map | null=) -> int

What it does

Sends a value to every page in a channel and gives how many took it. The value goes as JSON, up to 1 MiB. The options are except (a peer, or its id, that is left out: usually the one whose message is being passed on) and reliable (in order, over a stream; a page that cannot keep up is disconnected rather than miss it). It waits for nothing, needs the net effect, and reaches the pages connected to this server (and the copies of it that quillo run main.qwe --instances 4 starts; see the options). Each tenant has its own room (since 0.7): code run for a page or a request reaches its tenant's room (a page with no tenant, the room of no tenant), and a job, a tick and the top level reach every tenant's. with_tenant(name, f) reaches one tenant's.

Examples

in a program
channel.on("game", (msg, peer) => {    channel.broadcast("game", { id: peer.id, pos: msg.pos }, { except: peer })})schedule("*/10 * * * * *", () => channel.broadcast("ticks", { at: now() }))

With no page in the channel, nothing is sent and the count is 0:

print(channel.broadcast("game", { pos: [1, 2] }))print(channel.broadcast("game", "text", { reliable: true, except: "0123456789ab" }))
output
00

Edge cases

print(channel.broadcast("nobody-here", null))print(channel.broadcast("room:1.a-b_c", [1, 2, 3]))
output
00

Errors

A bad name, an option that does not exist, options that are not a map and a message over 1 MiB as JSON are runtime errors, not error values.

this one fails
print(channel.broadcast("game", 1, { foo: 1 }))
output
line 1: `channel.broadcast` has no option `foo`
this one fails
print(channel.broadcast("game", repeat("x", 1100000)))
output
line 1: `channel.broadcast`: the message is 1100015 bytes as JSON; at most 1048576 go to a page

Chaining

The name comes first in a pipeline. The count can be logged or compared with channel.peers.

in a program
route POST /notice { form: { text: str } } {    let n = channel.broadcast("notices", { text }, { reliable: true })   // this tenant's pages    `sent to ${n} pages`}schedule("0 9 * * *", () => {    channel.broadcast("notices", { text: "good morning" })                // every tenant's    with_tenant("acme", () => channel.broadcast("notices", { text: "acme only" }))})
print("game" | channel.broadcast({ tick: 1 }))
output
0

See also

channel.send, channel.peers, channel.on, channel.open, schedule, with_tenant

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

Thread 0 posts

On channel.broadcast(): 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