Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix(timeline): fix loading forever and real-time polling issues

louis030195e35029ca1281
diff --git a/screenpipe-app-tauri/components/rewind/timeline.tsx b/screenpipe-app-tauri/components/rewind/timeline.tsx
index 3dfdad894..17a231b90 100644
--- a/screenpipe-app-tauri/components/rewind/timeline.tsx
+++ b/screenpipe-app-tauri/components/rewind/timeline.tsx
@@ -80,7 +80,7 @@ export default function Timeline() {
const { currentDate, setCurrentDate, fetchTimeRange, hasDateBeenFetched, loadingProgress } =
useTimelineStore();
- const { frames, isLoading, error, message, fetchNextDayData } =
+ const { frames, isLoading, error, message, fetchNextDayData, websocket } =
useTimelineData(currentDate, (frame) => {
setCurrentFrame(frame);
});
@@ -117,6 +117,11 @@ export default function Timeline() {
}, []);
useEffect(() => {
+ // Wait for websocket to be ready before fetching
+ if (!websocket || websocket.readyState !== WebSocket.OPEN) {
+ return;
+ }
+
let currentDateEffect = new Date(currentDate);
const checkIfThereAreFrames = async () => {
const checkFramesForDate = await hasFramesForDate(currentDateEffect);
@@ -130,14 +135,16 @@ export default function Timeline() {
const endTime = new Date(currentDateEffect);
if (endTime.getDate() === new Date().getDate()) {
- endTime.setMinutes(endTime.getMinutes() - 5);
+ // For today: use current time so server can poll for real-time frames
+ // Don't subtract 5 minutes - this was breaking live polling
+ // (server checks if now <= end_time, which was always false)
} else {
endTime.setHours(23, 59, 59, 999);
}
fetchTimeRange(startTime, endTime);
}
checkIfThereAreFrames();
- }, [currentDate]);
+ }, [currentDate, websocket]); // Re-run when websocket connects or date changes
useEffect(() => {
if (currentFrame) {
diff --git a/screenpipe-app-tauri/lib/hooks/use-timeline-data.tsx b/screenpipe-app-tauri/lib/hooks/use-timeline-data.tsx
index 1d4ffa070..df644a671 100644
--- a/screenpipe-app-tauri/lib/hooks/use-timeline-data.tsx
+++ b/screenpipe-app-tauri/lib/hooks/use-timeline-data.tsx
@@ -12,7 +12,6 @@ export function useTimelineData(
error,
message,
connectWebSocket,
- fetchTimeRange,
fetchNextDayData,
websocket,
} = useTimelineStore();
@@ -22,27 +21,12 @@ export function useTimelineData(
connectWebSocket();
}, []); // Only connect once when component mounts
+ // Set initial frame when frames arrive and no frame is selected yet
useEffect(() => {
- // Only fetch data when WebSocket is connected
- if (websocket && websocket.readyState === WebSocket.OPEN) {
- const startTime = new Date(currentDate);
- startTime.setHours(0, 0, 0, 0);
-
- const endTime = new Date(currentDate);
- if (endTime.getDate() === new Date().getDate()) {
- endTime.setMinutes(endTime.getMinutes() - 5);
- } else {
- endTime.setHours(23, 59, 59, 999);
- }
-
- fetchTimeRange(startTime, endTime);
-
- // Set initial frame if available
- if (frames.length > 0) {
- setCurFrame(frames[0]);
- }
+ if (frames.length > 0) {
+ setCurFrame(frames[0]);
}
- }, [websocket?.readyState]); // Depend on WebSocket connection state
+ }, [frames.length > 0]); // Only trigger when we go from 0 to some frames
return {
frames,
@@ -50,6 +34,7 @@ export function useTimelineData(
error,
message,
fetchNextDayData,
+ websocket, // Expose websocket so timeline.tsx can depend on it
};
}
diff --git a/screenpipe-app-tauri/lib/hooks/use-timeline-store.tsx b/screenpipe-app-tauri/lib/hooks/use-timeline-store.tsx
index 32152a969..cb033948d 100644
--- a/screenpipe-app-tauri/lib/hooks/use-timeline-store.tsx
+++ b/screenpipe-app-tauri/lib/hooks/use-timeline-store.tsx
@@ -158,6 +158,21 @@ export const useTimelineStore = create<TimelineState>((set, get) => ({
loadingProgress: { loaded: 0, isStreaming: true }
});
console.log("WebSocket connection established");
+
+ // After successful connection/reconnection, trigger a fetch for current date
+ // This ensures data is requested even after reconnection
+ setTimeout(() => {
+ const { currentDate, fetchTimeRange } = get();
+ const startTime = new Date(currentDate);
+ startTime.setHours(0, 0, 0, 0);
+ const endTime = new Date(currentDate);
+ if (endTime.getDate() === new Date().getDate()) {
+ // For today: use current time for real-time polling
+ } else {
+ endTime.setHours(23, 59, 59, 999);
+ }
+ fetchTimeRange(startTime, endTime);
+ }, 100);
};
ws.onmessage = (event) => {
diff --git a/screenpipe-server/src/server.rs b/screenpipe-server/src/server.rs
index 318c9aeef..996ef31d1 100644
--- a/screenpipe-server/src/server.rs
+++ b/screenpipe-server/src/server.rs
@@ -3116,6 +3116,9 @@ async fn handle_stream_frames_socket(socket: WebSocket, state: Arc<AppState>) {
let mut poll_timer = tokio::time::interval(Duration::from_secs(2));
poll_timer.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
+ // Timer for keep-alive messages (every 30 seconds)
+ let mut keepalive_timer = tokio::time::interval(Duration::from_secs(30));
+
let active_request_clone = active_request.clone();
let sent_frame_ids_clone = sent_frame_ids.clone();
let db_clone = db.clone();
@@ -3321,6 +3324,13 @@ async fn handle_stream_frames_socket(socket: WebSocket, state: Arc<AppState>) {
}
}
}
+ // Send keep-alive message to prevent connection timeout
+ _ = keepalive_timer.tick() => {
+ if let Err(e) = sender.send(Message::Text("\"keep-alive-text\"".to_string())).await {
+ error!("failed to send keepalive: {}", e);
+ break;
+ }
+ }
}
}
});