Reference · Media

media.on

signature
media.on(str, map | null=) -> null

What it does

Lets pages send and watch a stream: after media.on(name, { publish, watch }), a page's <quillo-media publish="name"> sends its camera and microphone (or another MediaStream as its source) to the server, which passes the frames on to every <quillo-media stream="name">, decoding nothing. A name ending in * takes every stream it starts ("call:*"). publish(peer) says which pages may send, and watch(peer) which may watch, as a channel's join does: with the page's session in peer.session, anything but null, false or an error says yes. Without publish, no page sends; without watch, no page watches (since 0.7: any page could). Each tenant has its own streams of a name; tenants: "shared" makes one for every tenant. It is written at the top of a program, once for a name, and the program then serves.

Examples

in a program
media.on("call:*", {    publish: (peer) => peer.session.user != null,    watch: (peer) => peer.session.user != null,})component Call(room: str, me: str, others: list) {    <div>        <quillo-media publish={`call:${room}:${me}`}/>        {for p in others => <quillo-media key={p} stream={`call:${room}:${p}`}/>}    </div>}

Edge cases

in a program
media.on("lobby")                                           // no page may send or watch "lobby"media.on("desk:*", {    publish: (peer) => peer.session.role == "agent",    watch: (peer) => peer.session.user != null,})media.on("town-hall", {                                     // one stream for every tenant    publish: (peer) => peer.session.role == "host",    watch: (peer) => peer.session.user != null,    tenants: "shared",})

Errors

A name that is not a string or not a stream name (letters, digits, _, -, . and :, with * at the end allowed), a publish or watch that is not a function, a tenants other than "shared" or "own", an option that does not exist and a name taken twice are runtime errors when the program starts.

this one fails
media.on("call:*", { publish: true })
output
line 1: `media.on`: `publish` is a function of the page that wants to send
this one fails
media.on("call:*", { watch: true })
output
line 1: `media.on`: `watch` is a function of the page that wants to watch
this one fails
media.on("call:*", { join: (peer) => true })
output
line 1: `media.on` has no option `join` (it takes publish, watch and tenants)
this one fails
media.on("call:*", { tenants: "all" })
output
line 1: `media.on`: `tenants` is "shared" (one room for every tenant) or "own", got "all"
this one fails
media.on("x")media.on("x")
output
line 2: `media.on`: the program already takes stream "x" from pages
this one fails
media.on("bad name*")
output
line 1: `media.on` takes a stream's name first (or one ending in `*`), got "bad name*"

Chaining

It gives null and is not piped. With media.rtp for the camera's side and media.on for the pages', one page shows both.

in a program
media.rtp("hall", { video: 5004, audio: 5006 })media.on("hall", { watch: (peer) => peer.session.ticket != null })media.on("questions:*", {    publish: (peer) => peer.session.ticket != null,    watch: (peer) => peer.session.ticket != null,})

See also

media.rtp, media.streams, media.close, channel.on, session

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

Thread 0 posts

On media.on(): 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