Skip to main content

Live updates in code

A page of Poly Community draws what it shows from rows in its state. When something changes on the server, an event tells it, on the channels of those who may see it. The event carries the changed rows, or says which to read again, and every member page showing them draws them new. Nothing reloads, and no page is fetched.

For the whole path of a part drawn from data, see Building for instant pages and live updates.

Rows​

A row is an object as the browser draws it: its id, its version v, the values its template reads, and in html the keys of the trusted HTML it shows. Rows live in the state by type and id (state.rows.room[12]), in one shape in the page state, a REST answer and an event:

{ "rows": { "room": { "12": { "id": 12, "v": 1795959816000000, "name": "The vault", "html": { "row": "room:12:row:1795959816000000" } } } },
"html": { "room:12:row:1795959816000000": "<article>…</article>" } }

Registering a row type​

function my_rooms_row_types( $live ) {
$live->register_type( 'room', array(
'find' => 'my_rooms_get',
'row' => 'my_rooms_build_row',
'version' => 'my_rooms_version',
'events' => array(
'room.updated' => 'my_rooms_of_event',
),
'exact' => 'my_rooms_is_public',
'named' => 'my_rooms_named',
) );
}
add_action( 'poly_community_register_row_types', 'my_rooms_row_types' );

function my_rooms_of_event( $payload ) {
return (int) ( $payload['id'] ?? 0 );
}

function my_rooms_named( $room ) {
return array( (int) $room->owner_user_id );
}

Each callback is the name of a function, or array( $this, 'method' ) in a class, never an anonymous function: an error names it, and the function that hooks the registration can be taken off again with remove_action().

  • find( $id ) returns the object as it is now, or null.
  • row( $object, $form ) builds the row, puts it with $live->put( 'room', $row ) and returns it, or returns null where the object is not drawn from data. $form says for whom: reader (the reader of this request, what they may do included), shared (everyone an event reaches: nothing of one reader in it) or known (shared, with the real author of an anonymous object).
  • version( $object ) is the row v. A newer version always wins in the browser, and a row of an older one never draws over a newer. A row at the same version as the one a page holds is not taken, so the version must grow with every change. Rows of the core tables are versioned by their rev, which $live->touch( $type, $id ) moves at each change. A row of your own table takes the time of its last change in microseconds, kept when it is saved ((int) floor( microtime( true ) * 1000000 )): a time in seconds gives two changes in one second the same version, and the second is lost.
  • events are the event types about the object, each answering its id, or 0 for an event about something else. Such an event moves the object version and carries its row (below).
  • exact( $object ) says whether the channels of its events are exactly those who may see it. Where they are not, its events go as references, and each page reads the row again for its own reader.
  • named( $object ) returns the accounts the row names. A reader in a block with one of them is never sent it.
  • table names the core table that versions it (items, profiles or spaces) for a row type over one of them.
  • anon( $object ) and known are for a row whose real author only some may know: where anon says so, the shared row is built without them, and those who may know get a second event of the type known names, carrying the known row.

The HTML a row shows is drawn by the server and kept by key: $live->html( 'room', $id, 'row', $v, $html ) returns its key, which the row holds in html. The browser draws it only from the page state, a REST answer or an event the server sent, never from what another browser published.

Events​

Poly_Community()->events->emit( $channels, 'room.updated', array( 'id' => (int) $room->id, 'space' => (int) $room->space_id ) );

Emit an event after the change is saved, with what it is about. Name the object id, as lists read a row again by it. Keep the payload small, its ids and what a list declaration matches on (space above): the row type that names the event adds the row itself.

Channels​

An event goes to channels, and a reader hears these:

ChannelWho hears it
siteeveryone who may see the community
membersevery member logged in
user:{id}one account, on every page they have open
profile:{id}the member of that profile, and those who write as it
followers:{id}, friends:{id}the followers or friends of that profile
modthe administrators, for the moderation queue

Spaces, profiles and posts have the channels of exactly those who may see them, from Poly_Community()->access->space_channels( $space ), profile_channels( $profile ) and item_channels( $item ). Use those for what belongs to one of them, so an event of a hidden space never reaches someone outside it.

What an event carries​

emit() asks the row type naming the event for its row, built as shared, and adds it to the event. An event goes as a reference instead, with the ids alone, when its row type is not exact for that object, or when the event would be over 60 KB.

A page then reads the rows again for its own reader.

Which parts take the rows an event carries: the lists of the core (posts, members, comments, forum topics, questions) each take those of their own types. A list of yours follows its events by its declarations (Lists): the events in its events ask it again, and those in its reads read the row they name again for each reader, in one request a batch. That works whether the event carried the row or came as a reference.

How pages hear them​

The pages of members hear events: a visitor page asks for none, and a visitor sees what changed on the next page they open. A member page hears them over the realtime connection where the site has one (WordPlus Cloud), and otherwise by asking GET /wp-json/poly-community/v1/events?since= every few seconds, less often in a hidden tab (Settings sets how often). Both bring the same events, each once. A page that has not asked for over an hour (a laptop asleep) is told to draw what it shows again from fresh data, as the log keeps an hour.

In a component script​

A component script is a script module at assets/<slug>.js, registered as @poly-community/kit-<slug> by itself, that imports from the core module poly-community. It may be built (the components of Poly Community build theirs from src/index.ts) or written as it is, as the example plugin does:

import { onLive, api, askPart, setPart, partOf, followServer, markPageChanged } from 'poly-community';

// A handler for one type of event, polled or pushed alike.
function roomFull( payload, event ) {
setPart( 'roomSeats', payload.id, { full: true } );
}
const stop = onLive( 'room.full', roomFull );

// A part asked again from its route, its answer written where the page reads it.
askPart( 'roomSeats', 12, 'my-rooms/12/seats' );
  • onLive( type, listener ) adds a handler and returns a function that takes it off. The payload is what the event carries, its rows in data where it carries any.
  • api( path, options ) calls a route under poly-community/v1/ with the reader session (api( 'example-rooms', { method: 'POST', body: { name } } )). Register the routes of your component there, each with its own permission check. Its errors reject, and errorMessage( error ) gives the message to show with toast( message, true ).
  • mergePack( data ), listOf( key ) put rows a route answered into the state and give a list of the page by its key, so the page that made a change draws it at once (step 8 of the guide).
  • setPart(), askPart(), partOf() write, ask again and read the parts of a page (Parts).
  • followServer( name ) keeps a key of the state taken from each page a move brings, whole. Use it for state of your own that differs by page: a move keeps the state of the page before and adds only keys it has not.
  • markPageChanged() says something the page shows changed elsewhere, where no part of it can be drawn again from data: the page shows its pill, which draws it again.

What an event changes is drawn from the state. Never write into the HTML of a page by hand: a move or an event draws over it.

A widget drawn again​

A widget the server draws with no data of its own may name the events that draw it again: data-poly-refresh="room.created room.deleted" on its wrapper, with data-poly-refresh-min (seconds at most between two) and data-poly-refresh-every (again on a timer). It is drawn again by asking the page, so prefer a list or a part that follows its events, as above, for anything new.

What is stable​

poly_community_register_row_types and register_type() are on the stable list (What is stable). The events and their names, Poly_Community()->events->emit(), the channels and the poly-community script module are not on it yet.

See also Lists, Moving between pages and Built on the Interactivity API.