3. Someone is typing

Events: the backend pushes a moment to chosen devices.

3 MIN READView Markdown ↗

"Lin is typing…" is not data you store. It is a moment: it matters now, to the people who are looking, and then it is gone. For that, ArkAPI has events.

LiveValueEvent
Whatstate, the result of a querya moment
Who gets itevery screen showing the querythe device ids you choose
Storedin your databasenever; offline devices miss it
Examplethe message listsomeone is typing

The backend#

Add event to the import, and this below countOnline:

TypeScript
import { ApiError, caller, event, online } from 'arkapi/server'
TypeScript
export interface Typing {
  name: string
}

export const typing = event<Typing>()

export function setTyping(): void {
  const device = me()
  typing.send(online().filter((other) => other !== device), { name: nameOf(device)! })
}

event<Typing>() declares an event with its payload type. Export it, or the app cannot listen to it. send takes a list of device ids and the payload. Here it goes to every device that is online, except the one typing.

The screen#

Save, and the generated client has a new function, onTyping: on plus the event name, with a typed listener. Add Listening, onTyping, setTyping and Typing to the imports:

ArkTS
import { AsyncValue, Listening, LiveValue } from 'arkapi'
import { countOnline, getMessages, getName, Message, onTyping, sendMessage, setName, setTyping, Typing } from './chat.api'

Add this below the draft state. The listener starts when the screen appears and stops when it goes away:

ArkTS
  @State typist: string = ''
  private typing: Listening | null = null
  private typedAt: number = 0
  private typistAt: number = 0

  aboutToAppear(): void {
    this.typing = onTyping((event: Typing) => {
      this.typist = event.name
      this.typistAt = Date.now()
      setTimeout(() => {
        if (Date.now() - this.typistAt >= 3000) {
          this.typist = ''
        }
      }, 3000)
    })
  }

  aboutToDisappear(): void {
    this.typing?.stop()
  }

The name disappears three seconds after the last event. In the chat builder, show it between the list and the input row:

ArkTS
    .stackFromEnd(true)
    Text(this.typist === '' ? '' : `${this.typist} is typing…`).fontSize(11).fontColor('#8A8A99')

Then tell the others while you type. Add .onChange to the message TextInput, and the typed method above send:

ArkTS
        .backgroundColor('#22222A')
        .onChange(() => this.typed())
        .onSubmit(() => this.send())
ArkTS
  typed(): void {
    if (Date.now() - this.typedAt > 2000) {
      this.typedAt = Date.now()
      setTyping().catch(() => {})
    }
  }

typed sends at most one event every two seconds, and the receiver keeps the name for three, so the indicator stays steady while someone types. catch(() => {}) because a lost typing notice is not worth an error on screen.

Try it#

You need a second listener, so open a socket from the terminal. /live is the same socket the app uses:

Terminal
bun -e "const ws = new WebSocket('ws://localhost:3000/live', { headers: { 'X-ArkAPI-Device': 'laptop' } }); ws.onmessage = (e) => console.log(String(e.data))"

Type in the app, and the terminal prints {"event":"pages.chat.typing","data":{"name":"Ada"}}. The app, the one typing, gets nothing.

Next: photos.

Tab to navigate results · Enter to open · Esc to close