"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.
LiveValue | Event | |
|---|---|---|
| What | state, the result of a query | a moment |
| Who gets it | every screen showing the query | the device ids you choose |
| Stored | in your database | never; offline devices miss it |
| Example | the message list | someone is typing |
The backend#
Add event to the import, and this below countOnline:
import { ApiError, caller, event, online } from 'arkapi/server'
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:
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:
@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:
.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:
.backgroundColor('#22222A')
.onChange(() => this.typed())
.onSubmit(() => this.send())
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:
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.