Reference · Channels

channel.open

signature
channel.open(str, fn, map | null=) -> map

What it does

Opens a channel from a page: in a component, channel.open(name, f) joins the channel for as long as the code that called it stands (the component leaving the page closes it). f gets each message the server sends on the channel, and the map it gives has .send(value, { reliable }), which sends one to the server's channel.on handler and gives null. On the server, while the page is rendered, it opens nothing: the map's send gives false.

Examples

in a program
component Board() {    state others = {}    let game = channel.open("game", (m) => { others = set(others, m.id, m.pos) })    <canvas @pointermove={(e) => game.send({ pos: [e.clientX, e.clientY] })}/>}component Chat() {    state lines = []    let chat = channel.open("chat", (m) => { lines = lines + [m] })    <form @submit={(e) => chat.send({ text: e.form.text }, { reliable: true })}>        <input name="text"/>    </form>}

What the server sees while rendering:

let game = channel.open("game", (m) => print(m))print(game)print(game.send({ pos: [1, 2] }))
output
{ send: <builtin channel.open.send> }false

Edge cases

print(channel.open("room:1.a-b_c", (m) => m).send(null))
output
false

Errors

A bad name is a runtime error, on the server as in the page. There are no error values: a message that cannot be delivered is dropped or waits, as above.

this one fails
print(channel.open("bad name", (m) => m))
output
line 1: `channel.open`: a channel's name is letters, digits, `_`, `-`, `.` and `:`, up to 64, got "bad name"

Chaining

The map is kept in a let and its send called from handlers; the name comes first, so it could be piped, but it is written out.

in a program
component Cursor() {    let c = channel.open("cursors", (m) => draw(m))    <div @pointermove={(e) => c.send([e.clientX, e.clientY])}/>}
print("game" | channel.open((m) => m) | keys)
output
["send"]

See also

channel.on, channel.send, channel.broadcast, listen, live

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

Thread 0 posts

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