Reference · Channels

channel.send

signature
channel.send(str, map | str, any, map | null=) -> bool

What it does

Sends a value to one page in a channel: the channel's name, the peer (the map the channel's function gets, or its id), the value, and options. It gives whether the page was there to take it. The value goes as JSON, up to 1 MiB; { reliable: true } makes sure it arrives in order (over a stream rather than a datagram), and closes a page's connection that cannot keep up rather than drop it. It waits for nothing and needs the net effect. It reaches only a page in a room the code reaches (since 0.7): code run for a page or a request, its tenant's room; a job, a tick and the top level, every tenant's.

Examples

in a program
channel.on("game", (msg, peer) => {    if msg.kind == "hello" {        channel.send("game", peer, { you: peer.id, board: board() }, { reliable: true })    }})

With no page in the channel, the answer is false:

print(channel.send("game", "0123456789ab", { pos: [1, 2] }))print(channel.send("game", { id: "0123456789ab", session: {} }, "hi", { reliable: true }))
output
falsefalse

Edge cases

print(channel.send("game", "0123456789ab", null))print(channel.send("game", "0123456789ab", [1, 2, 3]))print(channel.send("room:1.a-b_c", "0123456789ab", "text"))
output
falsefalsefalse

Errors

A bad channel name, a peer that is neither a peer map nor a hex id, an option that does not exist, and a message over 1 MiB as JSON are runtime errors. A page that is not there is not an error: the result is false.

this one fails
print(channel.send("game", 42, "hi"))
output
line 1: `channel.send` takes a peer (as the channel's function gets it, or its id) second
this one fails
print(channel.send("game", "0123456789ab", "hi", { except: "0123456789ab" }))
output
line 1: `channel.send` has no option `except`

Chaining

The name comes first, so a pipeline passes it; the bool answers whether to try another way.

in a program
channel.on("orders", (msg, peer) => {    if !channel.send("orders", peer, { accepted: msg.id }) {        db.exec("update orders set notify = 1 where id = ?", msg.id)   // tell them next time    }})
print("game" | channel.send("0123456789ab", "hi"))
output
false

See also

channel.broadcast, channel.on, channel.peers, channel.open

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

Thread 0 posts

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