Skip to main content

Presence in Express

Markdown

Three ways to show people: a presence room lists who is in it to everyone in it (who is in the shop, who is viewing a document). A count says how many, at any size. And who is online is known across your whole app, for a dot on every avatar.

The browsers of Ann, Ben and Cara, each listing the three of them in the shop now, with their roles from the info of each member

A presence room​

One room for the whole shop, named shop, lists who is in it to everyone in it. A page that joins it asks your server first, and a rule answers: a function your server declares once for the room. WordPlus Cloud then holds the page to the answer (permissions). This rule says:

  1. Signed-in users join, and visitors do not.
  2. Each one is shown to the others with their name and their role.
realtime.room( 'shop', async ( user ) => {
// 1. Signed-in users only.
if ( user === null ) return false;
const person = await db.users.find( user );
if ( person === null ) return false;

// 2. What the others see of them.
return { info: { name: person.name, role: person.role } };
}, { presence: true } );
  • info is what the others see of the member: a name, a role, anything small (up to 4 KB). It is sealed with the room key.
  • Up to 1,000 members are listed in full, with join and leave. Past that, the room counts them, and members() pages through them, up to 100,000.
const shop = rt.join( 'shop' );
const draw = () => list( [ ...shop.members ] ); // [ [ id, info ], … ]
shop.on( 'joined', draw ); // the members as the page joins
shop.on( 'join', ( { id, info } ) => draw() );
shop.on( 'leave', ( { id } ) => draw() );

A user is a member once, however many tabs and devices they have in the room, and leaves when the last one does.

Counting​

count: true gives a room a live count, without a list. The page of product 7 joins product:7, a public room: anyone may join it, signed in or not, and no rule runs.

realtime.room( 'product:{id}', { public: true, count: true } );
const product = rt.join( 'product:7' );
product.on( 'count', ( n ) => showLooking( n ) ); // at most once a second

A presence room counts too. product.count holds the latest.

Who is online, across your app​

Every page whose user your server vouched for makes that user online. They go offline 10 seconds after their last page closes, so a reload does not flicker.

rt.online.watch( [ 34, 43 ] ).on( 'change', ( { id, online, at } ) => dot( id, online ) );
rt.online.has( 34 ); // true or false, for a user watched
rt.online.lastSeen( 43 ); // when, in ms, for one offline
rt.online.on( 'count', ( n ) => showOnline( n ) ); // how many are online, live
  • A page watches up to 1,000 users, and gets their status now and every change.
  • Your server asks too: await realtime.online.has( 42 ), .count(), .list().
  • Hiding: a user who chose not to show their status is never online to anyone:
const realtime = createRealtime( {
// …
hidden: async ( user ) => ( await db.users.find( user ) )?.hideStatus === true,
} );

Ready-made: who is online​

Optional, as every ready-made part: the presence room above is all a list needs, drawn your way (your own interface). This is the ready-made one.

The Better Messages list of members, on a presence room, with a call button for each of the others. It is one element:

<online-list room="shop" title="In the shop now" token-url="/call-token" type="video"></online-list>
<online-count room="product:7"></online-count>
await rt.callScreen();   // loads the elements and the call screen, rt.ui() the elements without calls
  • token-url is your call token route, the one of your first call: a press on a row asks it with ?to=34&type=video, and your route checks whether this user may call that one before it answers a token. Someone your route refuses is told so as they press. Without it, the rows have no button.
  • By a rule, call names the call in place of token-url, and a calls() rule decides (by a rule).
  • type is video, audio, or both: type="audio video".
  • Names and pictures come from the member info, then from your directory.
  • Its look is that of Better Messages, in a shadow root: your page styles do not reach in, and its colours are CSS custom properties your page sets.

Use cases​

Each with the rules of your server and the code of the page:

More, with PHP and Python side by side: presence in depth.