Skip to main content

Chat

Markdown

Conversations between two people and in groups: messages that arrive at once on every device, who is typing, who is here, read ticks and reactions. WordPlus Cloud carries them as they happen, sealed, and your server keeps the conversation in its own database, so it is yours to show again, to search and to moderate.

The chat window of Ben shows “Ann typing” while Ann, in her own browser, writes a reply

In your stack: Next.js · Express · Laravel · PHP · Django · FastAPI · Flask · WordPress

Who does what​

A chat has two halves, and this page and the next one take them in turn:

  • WordPlus Cloud carries it: a room for each conversation, the messages sent with an ack, who is typing and who has read what, who is in it now, and its last events for 5 minutes, so a page that reconnects misses nothing. It keeps no conversation past that.
  • Your server keeps it: the conversations, their members, every message, where each member read up to, and from those the unread counts and the list of conversations. It also sends the push to a phone with nothing open (what your server keeps).

The running example: conversation 7, between Ann (user 34) and Ben (user 42), in the room chat:7. A group is the same with more members.

A room per conversation​

A room for each conversation, named by its number in your database: chat:7. A page joins the room of a conversation to send and receive in it.

Your server decides who may join. A page that joins asks your server first, and a rule answers: a function your server registers once for every room whose name fits a pattern, here chat:{id}. WordPlus Cloud then holds the page to the answer (permissions). This rule says:

  1. The members of the conversation join, as your database lists them, and nobody else.
  2. They see who else is in it now, for a dot beside each name.
  3. The pages may send messages, typing, read marks and reactions, and nothing else.
  4. Your server hears each message and reaction, to keep them.
// lib/realtime.ts, beside createRealtime(). isMember() is your own query of the members table.
realtime.room( 'chat:{id}', ( user, { id } ) => isMember( id, user ), {
presence: true, // 2. who is in it now
emit: [ 'message', 'typing', 'read', 'reaction' ], // 3. what the pages may send
webhook: [ 'message', 'reaction' ], // 4. what your server hears, to keep
history: 50, // the last 50 for 5 minutes, for a page that reconnects
} );
  • One rule for two people and for a hundred: someone added to a group joins as their page next asks, as your rule says yes from then on.
  • Named after the two, with no table: chat:{pair}, such as chat:34:42, lets in only the two people its name holds and needs no rule. It suits a conversation that is only ever those two (two people, or a group). Your server still keeps its messages, under the room name in place of a number.
  • Sealed: what the members write is sealed with a key of the room, which only those your rule lets in get (encryption).

Sending, with an ack​

A message goes with emitWithAck(), which resolves once WordPlus Cloud took it, with the id and the time every page and your webhook see for it. The page that sent it does not hear it back, so it draws it from the ack.

// On the conversation page, after connect(). form and input are your composer.
const chat = rt.join( 'chat:7' );

form.onsubmit = async ( event ) => {
event.preventDefault();
const text = input.value;
const { id, at } = await chat.emitWithAck( 'message', { text } ); // taken, kept for 5 minutes and sent on
draw( { id, from: rt.user, text, at } );
input.value = '';
};

chat.on( 'message', ( data, meta ) => draw( { id: meta.id, from: meta.from, text: data.text, at: meta.at } ) );
  • draw() is yours: it adds the message to the conversation on the screen, once for each id.
  • meta.from is the sender as your server vouched for them: a page cannot write as someone else.
  • A refused message rejects with its code: not_allowed, rate_limited, too_large (errors). Show it on the message, and let the user send it again.
  • Pictures and files go to your own storage, and the message carries where each is (files).

Typing, read and who is here​

These three change all the time and matter only now, so typing and read go volatile: never queued, never kept, and never sent to your webhook.

  1. Typing: { on: true } while the user writes, at most every 2 seconds, and { on: false } as they stop or send. Hide a writer after 4 seconds of nothing.
  2. Read: { id } of the newest message the user has seen, as it shows on their screen. The sender draws their ticks from it.
  3. Who is here: the members of the room now, from its presence, with join and leave as they come and go.
// 1. Typing, at most every 2 seconds. input is your composer.
let typed = 0;
input.oninput = () => {
if ( Date.now() - typed < 2000 ) return;
typed = Date.now();
chat.volatile.emit( 'typing', { on: true } );
};
chat.on( 'typing', ( data, meta ) => showTyping( meta.from, data.on ) );

// 2. Read, for each message that arrives while the page is in view.
chat.on( 'message', ( data, meta ) => {
if ( document.visibilityState === 'visible' ) chat.volatile.emit( 'read', { id: meta.id } );
} );
chat.on( 'read', ( data, meta ) => markRead( meta.from, data.id ) );

// 3. Who is here now.
chat.on( 'joined', () => showHere( [ ...chat.members.keys() ] ) );
chat.on( 'join', ( { id } ) => addHere( id ) );
chat.on( 'leave', ( { id } ) => removeHere( id ) );
  • showTyping(), markRead() and the others are yours: they draw what the event says.
  • Read is for the ticks. It is volatile, so your server never hears it. Where your server counts unread messages, the page also tells your server where the user read up to (the last-read marker).
  • Online anywhere in your app, for a dot on a list of conversations, is rt.online.watch( ids ) (who is online).

Catching up​

A page that opens a conversation shows it from your server, and the room fills in the rest:

  1. As it opens, the page asks your route for the newest 50 messages your server kept, and with ?before=<id> for the 50 before that one as the reader scrolls up (the route).
  2. What your server has not kept yet, sent in the last seconds, comes from the room: chat.history() gives its last events, oldest first.
  3. A reconnect gets what it missed first, in order, with meta.replayed, then the new events. Draw each message once by its id.
  4. Back after more than 5 minutes, or after more came than the room keeps, the page gets what the room still had, then error with { code: 'gap' }, still joined. Ask your route for the newest page again.

Browser

// 1. Your server's newest page, then 2. what the room has beside it.
const { messages } = await fetch( '/api/chat/7/messages' ).then( ( r ) => r.json() );
messages.forEach( draw );
const { events } = await chat.history( { limit: 50 } );
events.filter( ( e ) => e.event === 'message' ).forEach( ( e ) => draw( { id: e.id, from: e.from, text: e.data.text, at: e.at } ) );

// 4. Some events lost to this page: the newest page again.
chat.on( 'error', ( e ) => {
if ( e.code === 'gap' ) reload();
} );
  • draw() keeps each id once, as the same message may come from your route, from the history and live.
  • The ready-made conversation window (<chat-room>) does all four by itself (the ready-made window).

Keeping the conversation​

Your server keeps each message by its event_id, the id the pages saw as meta.id, so a webhook delivered twice keeps it once. The tables it goes in, the route that gives a conversation back and the unread counts are on the next page (what your server keeps).

// Each message, kept once by its event_id. keepMessage() is yours.
realtime.on( 'message', ( { room, from, data, event_id, at } ) => keepMessage( { eventId: event_id, room, from, text: data.text, at } ) );

// Each reaction, by the message and the person: an empty key takes it back.
realtime.on( 'reaction', ( { from, data } ) => db.reactions.set( data.id, from, data.key ) );
  • Check there anything that matters: the events do not go through your server before they are sent on. An app that must check each message before anyone sees it sends it from its own route instead (from your own send route).
  • In a public room (public: true), a visitor who is not signed in sends with no from: keep the messages of signed-in people there, and leave the others out.
  • On localhost no webhook reaches your server, as a webhook needs an address on the internet, so nothing is kept this way while you build there. The page can tell your server of each message it sent instead (without a webhook), or a tunnel gives your computer an address (while you develop).

Telling the other person​

A message shows at once only on a page that has its conversation open. Ben writes to Ann while she is on another page of your app, and she sees nothing there. So your server tells her:

  1. Your server hears the message, from its webhook, as it keeps it.
  2. It sends each other member a notice of their own with toUser(), which reaches every page and app they have open.
  3. Their page shows it: a toast with the name, the text and a link, or one more on the count beside the conversation.
  4. A member with nothing open gets a push to their phone, which your server sends (sending a push).
// After keeping it (above). membersOf() is your own query: the user ids of the conversation.
realtime.on( 'message', async ( { room, from, data, event_id } ) => {
// 1. Heard from the webhook: only the rooms of conversations, such as chat:7.
const [ kind, id ] = room.split( ':' );
if ( kind !== 'chat' ) return;

// 2. A notice for every other member, on every page and app they have open.
const others = ( await membersOf( id ) ).filter( ( user ) => user !== String( from ) );
await realtime.toUser( others ).emit( 'new_message', { room, from, text: data.text.slice( 0, 140 ), id: event_id } );
} );

Then 3. her page shows it: every page of a signed-in user listens, and leaves out the conversation it has open (sending).

// On every page of a signed-in user, after connect(). openRoom is the conversation on this page, if any.
rt.on( 'new_message', ( m ) => {
if ( m.room !== openRoom ) toast( nameOf( m.from ), m.text, `/messages/${ m.room }` );
} );
  • A count of new messages: add one to the conversation in your list for each notice, and clear it as the user opens it. Your server counts the same from where each member read up to, for a page that opens later (counting unread).
  • toUser() reaches the pages and apps open now, and nothing else: for a member with nothing open, your server sends a push or an email itself. The next page puts the notice and the push in one function, tellOthers(), called as each message is kept (sending a push).

Without a webhook, as on localhost​

A webhook needs an address on the internet, so your server on localhost never gets one. While you build, the page can tell your server itself: the ready-made conversation window (<chat-room>) hears each message the room took, with its id, as its sent event, onSent in React.

// On the conversation page. /api/chat/sent is your own route.
document.querySelector( 'chat-room' ).addEventListener( 'sent', ( e ) => fetch( '/api/chat/sent', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify( e.detail ), // { id, room, text, at }
} ) );

Your route /api/chat/sent then does what the webhook does above: it checks the user of the session is a member of m.room, keeps the message by m.id, and tells the others. The webhook stays the one to trust where it reaches you, as WordPlus Cloud signs it, and keeping by the id lets both run without keeping a message twice.

  • Both on at once? Keep the message from either, by its id, but send the notice from one alone: the route of onSent while you build, the webhook once it reaches you. Or let the page show one notice for each id.
  • A page of your own does the same once its emitWithAck() resolves, with the id it answered.
  • In Next.js, <Chat> goes in a client component ('use client') of your own (Next.js).

Files​

A picture, a scan, a contract: the chat takes files once you give it a route for them, and your server keeps them. WordPlus Cloud never has the file. The message carries only where it is, its name, its type and its size, sealed with the room key as the rest of the message is.

  1. The page picks a file, or one is pasted or dropped on the chat, and sends it to your route at once, showing how far it got. Send waits until it is up.
  2. Your route asks the room rule whether this person may be in the conversation (mayJoin), keeps the file, and answers where it opens: { id, url, name, type, size }. Or it refuses with an error status and { error: "…" }, whose words the chat shows on the file.
  3. The message goes with its files, and your webhook keeps them with it. Keep only the files your route took for that room, as kept: anyone may write anything into a message of their own.
  4. Your file route opens a file only for those in its conversation.
// POST /chat/files: a file for a conversation, as a form of the file and the room. upload is your form parser, such as multer.
app.post( '/chat/files', upload.single( 'file' ), async ( req, res ) => {
// The room rule: whoever may be in the conversation may send it a file.
if ( !req.file || !( await realtime.mayJoin( req.body.room, req.user?.id ?? null ) ) ) return res.status( 403 ).json( { error: 'Not for this conversation.' } );
if ( req.file.size > 10_000_000 ) return res.status( 413 ).json( { error: 'Larger than 10 MB.' } );
const file = await db.files.create( { room: req.body.room, owner: req.user.id, name: req.file.originalname, type: req.file.mimetype, data: req.file.buffer } );
res.status( 201 ).json( { id: file.id, url: `/chat/files/${ file.id }`, name: file.name, type: file.type, size: req.file.size } );
} );

The page names the route:

<chat-room room="chat:7" messages="/api/chat/7/messages" upload="/chat/files" accept="image/*,.pdf" max-file-size="10000000"></chat-room>
  • Kept with the message: the webhook data.files, and the files of onSent. Look each up by its id among the files your route kept for that room, and keep those alone, as your route answered them.
  • Your route for the conversation answers each message with its files, and the chat shows them: pictures in a grid that opens them large, a video with its player, any other file as a row with its download.
  • Where files are shown from: your site, and the hosts you name in file-hosts (fileHosts in React), such as your storage. A file anywhere else is not shown, nor a link of any other kind.
  • Serving a file: answer it with its own type, X-Content-Type-Options: nosniff, and anything not a picture as a download, so no file runs as a page of your site. Never take an SVG or an HTML file from a user as a picture.
  • A page of your own posts the file to the same route, then sends the message with files: [ … ] as the route answered them.

The ready-made window​

<chat-room> is the conversation window of Better Messages on one room. It is optional, as every ready-made part, and runs on the same events as the code above, so it and a page of your own read each other (events from the page).

  • What it draws: the messages of one conversation, paged back from your route as the reader scrolls up, the room filling in what came since, the days, the messages of a sender stacked, typing, reactions, the read ticks, files and the composer. With to, the other person on its top bar, online or not, and with token-url or call their call buttons too (a call in a chat).
  • What it leaves to your app: the list of conversations, who belongs to each one, the unread counts, the search, keeping the messages and the pushes. It shows the one conversation in room, and keeps nothing itself (what your server keeps).

rt.callScreen() on the page loads it, or rt.ui() on a page without calls:

<chat-room room="chat:7" to="42" messages="/api/chat/7/messages" style="height: 520px"></chat-room>
<typing-line room="chat:7"></typing-line>
  • messages is your route for the conversation, asked with the page cookies (the route). load replaces it, for a route that needs headers of its own (elements).
  • What it sends: message with an ack, typing and read volatile, and reaction with an ack, as events from the page lists them. Your room rule names them in emit.
  • Its sent event gives each message of this user the room took, for your server to keep where no webhook reaches it (without a webhook).
  • Its read marks stay among the pages, as they go volatile: for the unread counts of your server, the page tells your route as the conversation opens (the last-read marker).
  • <Chat … /> of @wordplus/realtime-ui/react is the same window drawn in your own tree, where your styles reach it, with its chat.css, and useChat() runs it with no look at all (React, your own interface).

Groups​

  • Who is in the group now comes from the presence of its room: chat.members, then join and leave (presence). Who belongs to it is your members table.
  • Someone removed from the group goes at once: take them off the conversation in your database, so your rule says no for them, then realtime.room( 'chat:7' ).remove( 34 ). Their token is refused from then on, a reload asks your rule again, and newer messages are sealed with a key they never get (encryption).
  • Someone added joins when their page next asks: your rule says yes from then on.
  • A reader who may not write, such as a supervisor: the rule answers { readOnly: true } for them (permissions).

Variations​

  • A support chat beside a call: a <call-button> button in the header of the conversation (calls from a button).
  • On phones: the same conversations in iOS and Android apps, on the same rooms (iOS, Android), and in a Capacitor app as on the web (mobile apps).
  • In a WordPress plugin: the same rooms with wordplus_realtime()->room() (WordPress events).
  • A buyer and a seller about a listing (marketplace chat), a support desk with agents (support desk), and an assistant that hands over to a person (AI chat).
  • A whole site, in Next.js: the chat of a dating site, with a video date in it (dating site), the channels of a team intranet (team intranet), channels with photos for feedback (community site), and a buyer and a seller sending photos about an item (marketplace).