Skip to main content

JavaScript reference

Markdown

Three scripts, each loaded only by the pages that need it:

GlobalScriptLoaded by
wordplusRealtimeClientthe page script, which every WordPlus plugin shareswordplus_realtime()->script()
wordplusRealtimeCallscalls, group calls and live streamswordplus_realtime()->calls_script()
wordplusRealtimeUiThe Better Messages screens: the call screen, who is in a room, the chatwordplus_realtime()->call_screen() or ui()

The page settings are window.wordplusRealtimeConfig, unset on a site without its credentials. The live stream elements, <live-stream> and <stream-studio>, come with wordplus_realtime()->stream_screen() and need no code (live streams).

wordplusRealtimeClient.page() → Rooms​

The page rooms, made once from wordplusRealtimeConfig and shared by the code of every plugin and the screens. null on a site without credentials. wordplusRealtimeClient.rooms( config ) makes rooms of your own, which page() is the one to share.

Member
join( name )→ Room, the same one again for the same name (rooms)
room( name ), leave( name )a room this tab joined, or undefined. Leaving it (the other tabs of the browser keep theirs)
onlinewho is online across the site (presence)
users( ids )→ Promise<{ [id]: { user_id, name, avatar, url, … } }>: the site users from its directory, opened. Rejects with no_identity on a page whose user the site does not know (encryption)
userthe user the site vouched for on this page, or null
on( event, fn ), once, off( event?, fn? )what your server sends this user (to_user()->emit()), fn( data, { at } ), and those of the connection itself: connected, disconnected, moved (to another server without a drop), error ({ code }: the connection refused), signed_out (your server took the user out of everything, for_user()->disconnect())
answer( event, fn )what the pages of this user reply when your server asks them (for_user()->ask()): fn( data, { at } ) returns the reply, or a promise of it. null in place of fn stops answering (sending)
tabthis tab name, for the except of an emit (send it as X-Realtime-Tab)
connected
close()leaves every room. The other tabs of the browser keep the connection

Room​

Member
name, state, errorjoining, joined, failed, left. Why it failed (errors)
readya promise, resolved once joined
on( event, fn ), once, off, onAny( fn ), offAnyan event, fn( data, meta ) with meta { from, id, at, replayed? }, or those of the room itself: joined (the members), join ({ id, info }), leave ({ id }), count (n), error ({ code }), kicked
emit( event, data )to the others in the room, as its rule allows (events)
emitWithAck( event, data )→ Promise<{ id, at }> once WordPlus Cloud took it
volatile, timeout( ms )an emitter that drops rather than queues, or whose acks wait at most ms
history( { before, after, limit } )→ Promise<{ events, more }>, oldest first. 50 by default, 100 at most
members, countthe members of a presence room by id, their info. How many are in a room that counts
membersPage( cursor?, limit? )→ Promise<{ list, next }>: the members past the first 1,000 listed
leave()

Online​

Member
watch( ids ), unwatch( ids )up to 1,000 users a connection
has( id ), lastSeen( id )the status of a watched user. When they were last seen, in ms, or null while online
counthow many are online across the site, once something listens to count
on( 'change' | 'count', fn ), off{ id, online, at? } for a watched user. The count
list( { cursor, limit } )→ Promise<{ ids, next }>: a page of who is online, next '0' after the last

wordplusRealtimeClient.keyring() → Keyring​

The page keys, shared by every plugin on it (encryption). The rooms open what they carry with it by themselves.

Member
open( value )→ the text a sealed value holds, null when no key held opens it. Anything not sealed as it is
openJson( value )→ its data parsed. undefined when it does not open
seal( text, label ), sealJson( data, label )→ sealed with a key held, or null
missing( value )→ the labels of the sealed values in a payload whose keys the page lacks
ensure( labels )→ Promise: asks the site for those keys, once for those asked together
has( label ), newest( prefix ), dayLabel()a key held. The newest held of one thing (ch:order:42:), the key of today
onAdded( fn )fn( entries, source ) for each key the page gets. Returns a function that stops it

wordplusRealtimeClient.labelOf( value ) names the key a sealed value needs.

wordplusRealtimeCalls​

Member
page()→ { rt, calls }: the page rooms and calls, made once from wordplusRealtimeConfig. null without it
calls( rt, options )→ Calls on rooms of your own. page() is the one to share
supportsP2P()whether this browser can hold a direct call

Calls​

Member
start( name, { to, type } )→ Promise<Call>: rings to. Rejects with code
group( name, { type } )→ Promise<GroupCall>: joins a group call. Rejects with code
stream( name, { video }? )→ Promise<Stream>: joins a live stream as your rule says, or rejects with code (live streams, Stream below)
people( name )→ Promise<{ id, name, avatar }[]>: whom the user may add to calls named name, as your plugin people lists them
may( [ { call, to }, … ] )→ Promise<( boolean | null )[]>: whether the user may call each person about each call, as your rule answers for a caller, in the order asked. null where the site gave no answer
incoming()→ Incoming[]: the calls ringing for the page user now
stopRinging()this page holds the user line no more
on( event, fn ), off( event, fn? )incoming (Incoming: a call rings for the page user), call (Call: one this page started or answered), group (GroupCall: one it joined, a ring into one it answered, or a call of its that became one), error ({ code, what } for the line of the page, { code: 'room_failed', room } for a group call that did not connect). call and group hear the calls of the call screen too

Options (in wordplusRealtimeConfig, or to calls()): calls (the calls route, { endpoint, headers }), rooms (the script of the media client), roomsWorker (its encryption worker, beside it by default), streams (the script of live streams, beside the rooms script by default), api (the server for the leave of a closing page), ring (false: hold no line).

Incoming​

Member
id, call, from, info, typethe attempt, the call name, the caller id, what the site said about them, video or audio
groupfor a ring into a group call, the call id. null for a call between two people
state, reasonringing, answered, declined or over, and why it is over
accept()→ Promise<Call>, or Promise<GroupCall> for a ring into a group call
decline( reason? )declined (default) or busy
on( 'over', fn )it stopped ringing: fn( reason )

Call​

Member
id, name, type, role, peerthe attempt, the call name, video or audio, caller or callee, { user, info }
stateringing, connecting, connected, reconnecting, ended
pathdirect, relay, room, or null before it connects
remote{ camera, microphone, screen, screenAudio, media, quality }
endReasonwhy it ended (calls)
setTrack( source, track )camera, microphone, screen_share, screen_share_audio. null stops
add( user )→ Promise<Invite>: rings user in, and the call becomes a group call (adding people)
end()hangs up, or cancelled while ringing
on( event, fn ), off( event, fn? )state, path, remote, waiting, reacquire, autoplay_blocked, group (GroupCall: it goes on as one), ended

GroupCall​

Member
stateconnecting, waiting, connected, reconnecting, ended
people()→ Person[]: everyone else
self→ Person: the page user as the others see them, null until connected
can{ publish, subscribe, admin }: what this page may do now, as your rule said and PHP changed since. admin is true for the host (the host)
moderate( act, user )→ Promise<boolean>: what the host does to someone else, mute (their microphone), listen (a listener now), speak (let speak) or remove, through the calls route, which asks your rule again. Whether it changed anything. Rejects with forbidden for a page that is not the host, not_moderated in the group call of a call
setTrack( source, track )→ Promise: what this page sends
leave()
encryptedwhether its media is encrypted end to end
groupfor the group call of a call, the call id. null for one your plugin names
add( user )→ Promise<Invite>: rings user into the group call of a call. not_a_call in any other
invites()→ Invite[]: the rings into it this page heard of
livekitthe room of the media client itself
on( event, fn ), off( event, fn? )state, waiting ({ until, reason }), person (someone arrived, or what they send or may send changed), person_left, speakers (Person[]), invite (Invite), permissions ({ publish, subscribe }: PHP changed what this page may do), error ({ code }: publish_failed, encryption_failed), ended (left, removed, closed, or disconnected: and the reason of the media server)

A Person is { identity, user, info, camera, microphone, screen, screenAudio, speaking, publish, cameraPaused, screenPaused }: publish whether they may send, and cameraPaused and screenPaused whether their picture holds its last frame for this page until its connection carries it again.

Invite​

A ring into a group call.

Member
id, group, from, tothe ring, the call id, who rang and whom
stateringing, answered, rejected, busy, no_answer or cancelled
cancel()→ Promise: calls it off while it rings, from the page that rang
on( 'state', fn ), off( 'state', fn? )each change: fn( state )

Remote tracks​

Every track another person sends has mediaStreamTrack, attach( element ), which plays it in an <audio> or <video> element, and detach( element? ).

Stream​

A live stream, from calls.stream( name ) (live streams). The same object for every role: what a role may not do rejects with its code (refusals).

Member
name, role, statethe stream name, the role your rule gave (host or viewer: a viewer on stage stays one, with onStage), and idle, live or ended
viewers, tiles, messages, hands, onStagehow many watch, everyone on the stage with their camera, screen and audio, the last messages of the chat, the hands raised ({ user, name, at }, for its hosts), whether this page is on stage
watch( video )shows the main picture in a <video>, and plays the sound. Answers a function that stops it
startAudio()→ Promise: the sound, where the browser held it back until a click
qualities(), setQuality( quality )the layers a viewer may choose, each high, medium or low with its size. One of them, or auto, the default
stats()how it plays in this browser, measured once a second while the picture comes, sent nowhere
snapshot(), subscribe( fn )everything the screen of the stream draws, and every change of it
react( emoji )a reaction everyone watching sees float up
chat.send( text )→ Promise: a message
chat.delete( id ), chat.pin( id ), chat.unpin(), chat.timeout( user, seconds ), chat.slow( seconds ), chat.clear()→ Promise: keeping the chat, for the host and its mods
kick( user ), ban( user )→ Promise: out now, or out and kept out, for the host and its mods
hand( up ), joinStage(), leaveStage()→ Promise: a viewer asks to come on stage, comes on when invited, goes back to watching
preview( { camera, microphone }? ), setCamera( on ), setMicrophone( on ), setScreenShare( on )→ Promise: what the host or a guest sends
goLive(), end()→ Promise: the host
invite( user ), unstage( user ), layout( mode, user? )→ Promise: the host brings someone on stage or takes them off, and shows the stage as grid, speaker, or spotlight with a user
ingest( renew? )→ Promise<{ url, key }>: where OBS streams to, over WHIP. true makes a new key and cuts off the old one
leave()leaves the stream, its picture and its chat
on( event, fn ), off( event?, fn? )state, viewers (n), tiles, message (each new one), hands, invited (the host asks this user on stage), stage (whether this page is on stage), waiting ({ until }), error ({ code }), change (any of it)

wordplusRealtimeUi​

Member
place( call, to, type, { name, avatar } )what <call-button> does
join( name, type, title )what <room-button> does
start( { lang, strings, add } )→ boolean: starts the screens now, which they do by themselves once the page has loaded, with the settings call_screen() gave. false on a page without calls
own( pattern, ( owned ) => … )a screen of your plugin for the calls whose names match pattern (support:{a}:{b}): their rings (owned.kind ring, owned.incoming), the calls <call-button> places (call) and the group calls <room-button> joins (group) come to you instead of the shared screen (your own screen). Answers a function that hands them back
setViews( { CallView, components, OnlineList, Chat, … } )the screens drawn with your own views and pieces (an elements script of your own, customizing the screens)