The symptom
On the call screen there is a timer that counts down from five minutes. It was started when the session was created — that is, when the two people were matched — and it ran through everything that happened afterwards: reading the pre‑call screen, granting the camera, waiting for the partner to press the button, and waiting for a partner who never did.
So the number on the screen and the number in the database were the same wrong number. A learner who talked for forty seconds could see the timer reach zero, and the minutes they earned for the conversation were taken from the time they spent waiting for the other person to arrive.
Who decides when a call starts?
Not the client, and not the moment of the match. The answer that works for every platform is the one the server already has: a call starts when both people have said they are ready. Only the server knows whether both have said it.
So the server records it, in one statement, so that there is no window in which two sides disagree:
-- One atomic statement: the clock starts only when both sides are ready.
UPDATE sessions SET
user1_ready_at = CASE WHEN user1_id = ? THEN datetime('now') ELSE user1_ready_at END,
user2_ready_at = CASE WHEN user2_id = ? THEN datetime('now') ELSE user2_ready_at END,
call_started_at = CASE
WHEN call_started_at IS NOT NULL THEN call_started_at -- never moved
WHEN user1_ready_at IS NOT NULL AND user2_ready_at IS NOT NULL
THEN datetime('now')
ELSE NULL
END
WHERE id = ? AND (user1_id = ? OR user2_id = ?);
The client asks the server every two seconds until the server says the call has started, and the readiness also travels over the signalling socket, so the common case is instant and the poll is the safety net.
The duration of the call is then measured from that column, with the old connected_at as the fallback for the rows that predate it:
export function callDuration(sess: any, now: number = Date.now()): number {
const raw = sess?.call_started_at || sess?.connected_at || null;
const ms = parseDbTimestamp(raw);
if (isNaN(ms)) return 0;
return Math.max(0, Math.floor((now - ms) / 1000));
}
A session that never reached that point has no call time at all. The waiting belongs to neither side.
Two clocks, two phones
The screen has to show the same second on both phones, and phone clocks are not clocks — they drift, and they are set by hand. The server therefore answers with its own time next to the start of the call, and the phone corrects itself:
start(serverTimestamp, serverTimeMs, clientNow) {
const now = typeof clientNow === 'number' ? clientNow : Date.now();
if (typeof serverTimeMs === 'number') this.skewMs = serverTimeMs - now;
const ms = parseServerTimestamp(serverTimestamp);
if (isNaN(ms)) return false;
// The start of the call is written in the clock of the server, so
// it has to be carried over into the clock of this phone.
this.startMs = ms - this.skewMs;
return true;
}
The first version of this code had the sign the other way round, and it was wrong in a way that no amount of staring would have found: the phone that was ahead of the server counted four seconds too fast, and the two sides of a call counted down to two different moments.
The clock was moved out of the screen and into a module that can be run without a browser (lib/call-clock.mjs), so it could be tested like any other code. The test wrote down a phone four seconds ahead and a phone seven seconds behind, and asked them to show the same second at the same moment of the server’s clock. They did not. That test is in the repository and runs with every change.
The proof against the live server
Two clients, using the real module against the real server, one phone four seconds ahead and the other seven seconds behind:
| Step | Result |
|---|---|
| The first side says it is ready | started: false — the clock does not start |
| The second side says it is ready | started: true, with one start time |
| The first side asks again | the same start time, to the second |
| Two seconds later | both phones show 297 seconds — the same number, eleven seconds of clock difference between them |
And in the database, the same call:
created_at 2026-11-16 13:13:06
user1_ready_at 2026-11-16 13:13:07
user2_ready_at 2026-11-16 13:13:08
call_started_at 2026-11-16 13:13:08
ended_at 2026-11-16 13:13:13
duration 5 ← not 7
Two seconds of waiting are not two seconds of conversation. Before the change the row said seven.
What the user sees
The screen says what is true at every moment. Before the call exists, the badge says Waiting for your partner to join… and the timer stands still at 5:00 with a line under it: The timer starts when you are both ready. When the server starts the clock, the badge changes to Connecting… and the seconds begin to run.
The thirty‑second connection deadline runs from the start of the call too, not from entering the room, and a partner who never comes gets forty‑five seconds and the reason peer_not_joined — the subject of the previous note.