Skip to main content

Sending from your server

Markdown

Your server sends events to rooms and to users with its secret, in the same chain in every package. Pages send to rooms too, as a room allows (events from the page). Only your server sends to a user.

To rooms and users​

A room by its name, such as order:42, the room the page of order 42 joins, and a user by their id. Your server sends once its own work is saved:

await realtime.to( 'order:42' ).emit( 'updated', { status: 'paid' } );
await realtime.to( [ 'order:42', 'staff' ] ).except( tab ).emit( 'updated', { id: 42 } ); // not back to the tab that made it
await realtime.toUser( 42 ).emit( 'notice', { text: 'Your order shipped' } );
to( rooms )one room, or up to 1,000
toUser( users ) (Python to_user)one user, or up to 1,000: every tab and device they have open, through a room of their own WordPlus Cloud keeps. It chains with to()
except( tab ) (Python except_tab)a tab (the browser rt.tab, sent with the request that caused it, as X-Realtime-Tab), which does not get it back. The Laravel toOthers() takes it from that header
emit( event, data, { seal } )any name but the reserved names of the room and the connection (joined, join, leave, count, error, kicked, connect, disconnect, connect_error, state, online). Data anything JSON takes, binary too, up to 1 MB. seal: false sends the data of a sealed room plain
  • Sealed by default: the data of a sealed room goes sealed with its key, a copy each, and what goes to a user with their own key. The data of public rooms goes plain, together.
  • Kept: a room whose rule keeps history keeps it, and the answer is the kept event ids, by room.
  • A refusal throws a RealtimeError with the server code and HTTP status (errors). The Laravel broadcasting throws a BroadcastException. Send after your own work is saved: a failed send is a missed live update, not a lost change.
  • A user with nothing open gets nothing: toUser() reaches the connections they have. A push to their phone is your server's own, sent when realtime.online says they are offline (a push for a message).

To everyone​

Every page and app connected now, signed in or not, with no room to join. A notice that you are about to restart, a new version to reload for:

await realtime.toEveryone().emit( 'notice', { text: 'Back at 9' } );

A page hears it as it hears what is sent to its user, with rt.on( 'notice', … ), in React useUserEvent( 'notice', … ).

  • Never sealed: anyone may be connected, so there is no key they all hold. Keep it to what anyone may read.
  • Never kept: someone who connects a moment later gets nothing, so keep what matters in your database too.
  • On its own: it doesn't chain with to() or toUser(). except( tab ) works as above, and a batch takes { everyone: true, event, data }.

Many at once​

Several events in one call: each is sent as an emit of its own, one after the other, sealed as it would be alone, and the answer is the kept ids of each, in their order.

await realtime.batch( [
{ rooms: 'order:42', event: 'updated', data: { status: 'paid' } },
{ users: 42, event: 'notice', data: { text: 'Paid' } },
] );

Asking about a room​

await realtime.room( 'room:7' ).count();
await realtime.room( 'room:7' ).members(); // { count, list: [ { id, info } ], next? }, their info opened
await realtime.room( 'chat:34:42' ).history( { limit: 50 } ); // { events, more }, oldest first, opened
await realtime.room( 'chat:34:42' ).forget( id ); // one kept event, forget() for all
await realtime.room( 'chat:34:42' ).remove( 43 ); // out now
await realtime.room( 'chat:34:42' ).removeEveryone(); // everyone out now
await realtime.room( 'chat:34:42' ).recheck( 43 ); // your rule asked again, in this room alone
await realtime.room( 'chat:34:42' ).recheckEveryone(); // the same for everyone in it

Remove takes the user connections out of the room at once, and refuses the token they had. Their page hears kicked, and may join again straight away, which asks your rule, so to keep them out, your rule says no for them, from your own data such as a ban list: a room has no ban of its own. With the key generations kept (encryption), it also moves the room key on, so a sealed event after it is not theirs to read.

Remove everyone does that to everyone in the room, visitors too. To close a room, have your rule say no first, then remove everyone. A public room takes anyone back, as it always does.

Recheck is for an answer that changed, such as a member made a reader: their pages leave the room and ask your rule again by themselves, in this room alone, and come back with what it says now, or fail with forbidden if it says no. The others see them leave and join again. Their other rooms and calls are left alone, where user( id ).disconnect() takes them out of everything.

The rooms in use​

The rooms someone is in now, by name, with how many are in each. With a prefix, only the rooms whose names start with it:

const { count, rooms, next } = await realtime.listRooms( { prefix: 'order:' } );   // rooms: [ { name, count, connections } ]

A page has 100 rooms, or limit up to 1,000. While there are more, send next back as cursor.

A user​

await realtime.user( 42 ).get();                     // { id, online, at?, connections }
await realtime.user( 42 ).disconnect(); // out of everything at once
const replies = await realtime.user( 42 ).ask( 'cart', { why: 'checkout' }, { timeout: 5000 } ); // [ { data, at } ]
  • get() says whether the user is online, since when they were last seen when they are not, and how many of their connections your server vouched for are open.
  • disconnect() takes the user out of everything at once, on every connection: every room, their identity, their calls ringing, their group calls and the live streams they watch (a host stays). Their pages then ask your routes for new tokens, and the tokens signed for them before are refused, so your rules decide whether they come back. Use it when you end the session of a user, change their password, or block them: they are out within a second, not when their token expires. Their pages hear signed_out. The connection stays open for everything else on it.
  • ask() sends an event to the open pages of the user and waits for their replies: the first one, or with all: true every one within timeout (10 seconds by default, a minute at most). No page answering is []. A page answers with rt.answer():
rt.answer( 'cart', async ( { why } ) => ( { items: cart.count(), total: cart.total() } ) );

The question and the replies are sealed with the key of the user, which WordPlus Cloud does not hold. Answer only what the user may see: your server asks, and the page of that user answers.

What your app uses​

const { connections, credits } = await realtime.usage();
// connections: { now: 312, limit: 1000 } credits: { plan, bought, borrowed, allowance, admit }

connections.limit is the connections of the plan of an app, null on WordPress. The credits are those of your account, a credit a minute: plan left this month, bought, how far into next month it borrowed, the monthly allowance, and admit, whether new viewers and people above the unmetered floor get in (limits).

Webhooks​

WordPlus Cloud tells your server what happens in rooms, at the address set on your app page in your WordPlus Cloud account (https://shop.example.com/api/realtime/webhook), which the package routes answer. A room rule names the events of its pages that come here too (webhook), and realtime.on() gives each event, by its name, a handler of yours. These handlers say:

  1. A message in a chat is kept once, by its event_id, from signed-in people alone.
  2. A user going offline is marked away.
// 1. A message, which the rule of its room names in `webhook`.
realtime.on( 'message', async ( { room, from, data, event_id, at } ) => {
// A guest in a public room sends with no `from`: kept from signed-in people alone.
if ( ! from ) return;
await saveMessage( { id: event_id, room, from, text: data.text, at } );
} );

// 2. A user going offline.
realtime.on( 'user.offline', ( { user } ) => markAway( user ) );
Event
a page event name{ id, at, room, event, data, from, event_id }, for the events a room rule names in webhook. event_id is the id the pages saw (meta.id), what your app keeps it by
room.occupied, room.vacated{ id, at, room }: the first one in, the last one out
member.joined, member.left{ id, at, room, user }, in a presence room
user.online, user.offline{ id, at, user }
stream.live, stream.ended{ id, at, stream, started_at, ended_at }: a live stream going live and ending, the times in milliseconds
call.ended{ id, at, call, call_id, from, to, media, started_at, answered_at, ended_at, seconds, reason }: a call between two people ended, with the seconds they talked, 0 for one never answered (charging)
group.joined, group.left{ id, at, call, user, media, joined_at }, and on leaving left_at and seconds: someone in a group call
stream.joined, stream.left{ id, at, stream, user, role, watch, joined_at }, and on leaving left_at and seconds: a signed-in person watching or on a live stream, while it is live: the time before it goes live and after it ends is not in it. Visitors are not told
webhook.test{ id, at }: sent once when your server asks for it, below
*every one of them
  • Signed with a key of its own (wordplus-realtime webhook v1), and checked before a handler sees it. Sealed data comes opened.
  • Batched, and sent at least once: each event has its id, so a repeat can be dropped. A request that fails is tried again, later each time, for 24 hours.

While you develop​

The webhook address must be public HTTPS on a host name, and localhost is neither, so WordPlus Cloud cannot reach the server on your computer by itself. A tunnel gives it a public address for as long as it runs:

cloudflared tunnel --url http://localhost:3000     # or: ngrok http 3000

Set the address it prints, with the path of your webhook, as the webhook of your app (https://quiet-hill-1234.trycloudflare.com/api/realtime/webhook), then send a test event to see it arrive:

await realtime.webhooks.test();   // { delivered: true, status: 204 }, or what your server answered
  • One webhook address an app. While you build the app, its webhook is your tunnel. When it goes live, set it to your server. A quick tunnel gets a new address each time it starts, so set it again, or use a named tunnel that keeps one.
  • Once the app is live, its webhook stays with your server, and your computer keeps working on the same app. Its addresses hold your website and its subdomains, and beside them the addresses you develop on: http://localhost:3000, one on your own network, or a name ending in .test or .local, up to 20 in all. There, with the same key, secret and local key, everything but the webhook works as it does live. Those pages share the rooms of the live app, so keep your development data apart, such as rooms named with a prefix of their own (dev:order:42).
  • A change to a webhook handler goes to a staging server of yours, on a subdomain of your website such as https://staging.shop.example.com: point the webhook there while you try it, and webhooks.test() checks it end to end, its signature included.
  • Not every feature needs it. A chat page tells your server each message it sent, with no webhook (without a webhook). Live stream, usage and member events come only by webhook.
  • What arrived: realtime.webhooks.deliveries(), below, has each try, with what your server answered or why nothing did.

Deliveries, a resend and a test​

const deliveries = await realtime.webhooks.deliveries();   // [ { id, at, events, types, attempt, status, error?, next_at?, resendable } ]
await realtime.webhooks.resend( deliveries[ 0 ].id ); // { delivered, status }
await realtime.webhooks.test(); // one webhook.test event, now

The deliveries are the last 100 tries of the last three days, the newest first: each batch by its id, the HTTP status your server answered (0 with error when nothing answered: timeout, unreachable, not_public), when it is tried again, and whether it may be sent again now. A batch your server did not take is kept while it is tried again, and three days after. test() checks your webhook works end to end, its signature included, the day you set it up.

Use cases​

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