Skip to content

Fix realtime re-subscribing stale data issue #2162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/wicked-ads-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@trigger.dev/react-hooks": patch
"@trigger.dev/core": patch
---

Fixes an issue with realtime when re-subscribing to a run, that would temporarily display stale data and the changes. Now when re-subscribing to a run only the latest changes will be vended
11 changes: 9 additions & 2 deletions packages/core/src/v3/apiClient/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ class ReadableShapeStream<T extends Row<unknown> = Row> {
},
});

let updatedKeys = new Set<string>();

// Create the transformed stream that processes messages and emits complete rows
this.#changeStream = createAsyncIterableStream(source, {
transform: (messages, controller) => {
Expand All @@ -122,7 +124,7 @@ class ReadableShapeStream<T extends Row<unknown> = Row> {
}

try {
const updatedKeys = new Set<string>();
let isUpToDate = false;

for (const message of messages) {
if (isChangeMessage(message)) {
Expand All @@ -147,18 +149,23 @@ class ReadableShapeStream<T extends Row<unknown> = Row> {
if (message.headers.control === "must-refetch") {
this.#currentState.clear();
this.#error = false;
} else if (message.headers.control === "up-to-date") {
isUpToDate = true;
}
}
}

// Now enqueue only one updated row per key, after all messages have been processed.
if (!this.#isStreamClosed) {
// If the stream is not up to date, we don't want to enqueue any rows.
if (!this.#isStreamClosed && isUpToDate) {
for (const key of updatedKeys) {
const finalRow = this.#currentState.get(key);
if (finalRow) {
controller.enqueue(finalRow);
}
}

updatedKeys.clear();
}
} catch (error) {
console.error("Error processing stream messages:", error);
Expand Down
27 changes: 27 additions & 0 deletions references/hello-world/src/trigger/realtime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { logger, runs, task } from "@trigger.dev/sdk";
import { helloWorldTask } from "./example.js";
import { setTimeout } from "timers/promises";

export const realtimeByTagsTask = task({
id: "realtime-by-tags",
Expand Down Expand Up @@ -32,3 +33,29 @@ export const realtimeByTagsTask = task({
};
},
});

export const realtimeUpToDateTask = task({
id: "realtime-up-to-date",
run: async ({ runId }: { runId?: string }) => {
if (!runId) {
const handle = await helloWorldTask.trigger(
{ hello: "world" },
{
tags: ["hello-world", "realtime"],
}
);

runId = handle.id;
}

logger.info("runId", { runId });

for await (const run of runs.subscribeToRun(runId, { stopOnCompletion: true })) {
logger.info("run", { run });
}

return {
message: "Hello, world!",
};
},
});