From 6b1a3c3e824a557f23e4b50a788d448aa89c31e1 Mon Sep 17 00:00:00 2001 From: Kirill Dubovitskiy Date: Sat, 16 Aug 2025 07:12:11 -0700 Subject: [PATCH] wip: emit machine updates on heartbeat for real-time status MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Machine-alive handler now properly emits update events to all user connections - Ensures mobile clients receive real-time machine status updates - Fixed null handling when machine doesn't exist in database Note: Last test was not able to spawn new session - webhook callback timing issue 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- sources/app/api.ts | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/sources/app/api.ts b/sources/app/api.ts index 9bdee22..518573f 100644 --- a/sources/app/api.ts +++ b/sources/app/api.ts @@ -1339,7 +1339,7 @@ export async function startApi(): Promise<{ app: FastifyInstance; io: Server }> const machineId = data.machineId; // Update machine lastActiveAt in database - await db.machine.update({ + const machine = await db.machine.update({ where: { accountId_id: { accountId: userId, @@ -1352,7 +1352,33 @@ export async function startApi(): Promise<{ app: FastifyInstance; io: Server }> } }).catch(() => { // Machine might not exist yet, that's ok + return null; }); + + // If machine was updated, emit update to all user connections + if (machine) { + const updSeq = await allocateUserSeq(userId); + emitUpdateToInterestedClients({ + event: 'update', + userId, + payload: { + id: randomKeyNaked(12), + seq: updSeq, + body: { + t: 'update-machine', + id: machine.id, + metadata: machine.metadata ? { + version: machine.metadataVersion, + value: machine.metadata + } : undefined, + active: true, + lastActiveAt: t + }, + createdAt: Date.now() + }, + recipientFilter: { type: 'all-user-authenticated-connections' } + }); + } } catch (error) { log({ module: 'websocket', level: 'error' }, `Error in machine-alive: ${error}`); }