Mobile: App-state aware WebSocket reconnect on foreground #33

Closed
opened 2026-03-20 22:50:40 +00:00 by replit · 1 comment
Owner

Problem

When the app is backgrounded on iOS or Android, the WebSocket connection to the API server is silently dropped by the OS. When the user returns to the app, the feed shows stale data and the ConnectionBadge shows "connected" incorrectly, because the WS disconnect event may have been swallowed.

What to build

In TimmyContext (or wherever the WebSocket is managed), add a react-native AppState listener:

  • On change → active: check if the WS is still open. If not (or if readyState !== OPEN), close the old socket and reconnect with exponential backoff (start at 1s, cap at 30s).
  • On change → background: optionally close the WS proactively to avoid the OS killing it mid-frame (saves battery).

Update ConnectionBadge to accurately reflect the reconnecting state (amber / pulsing) so the user knows the app is re-establishing the connection.

Acceptance criteria

  • After backgrounding for 30+ seconds and returning, the feed reconnects and shows live events within 5s
  • ConnectionBadge shows "reconnecting" state while the socket is down
  • No duplicate WS connections are created (old socket is fully closed before new one opens)
  • Exponential backoff does not hammer the server on sustained network loss
  • Works on both iOS and Android
## Problem When the app is backgrounded on iOS or Android, the WebSocket connection to the API server is silently dropped by the OS. When the user returns to the app, the feed shows stale data and the ConnectionBadge shows "connected" incorrectly, because the WS disconnect event may have been swallowed. ## What to build In `TimmyContext` (or wherever the WebSocket is managed), add a `react-native` `AppState` listener: - On `change → active`: check if the WS is still open. If not (or if `readyState !== OPEN`), close the old socket and reconnect with exponential backoff (start at 1s, cap at 30s). - On `change → background`: optionally close the WS proactively to avoid the OS killing it mid-frame (saves battery). Update `ConnectionBadge` to accurately reflect the reconnecting state (amber / pulsing) so the user knows the app is re-establishing the connection. ## Acceptance criteria - After backgrounding for 30+ seconds and returning, the feed reconnects and shows live events within 5s - ConnectionBadge shows "reconnecting" state while the socket is down - No duplicate WS connections are created (old socket is fully closed before new one opens) - Exponential backoff does not hammer the server on sustained network loss - Works on both iOS and Android
replit added the frontendmobile labels 2026-03-20 22:50:40 +00:00
claude was assigned by Rockachopa 2026-03-22 23:37:33 +00:00
Collaborator

PR created: http://143.198.27.163:3000/replit/timmy-tower/pulls/77

Added AppState-aware WebSocket reconnect in TimmyProvider. On foreground return, the app checks if the WS is still open and reconnects immediately if not. On background, the WS is proactively closed to save battery. A new "reconnecting" status with amber pulsing badge was added to ConnectionBadge. All acceptance criteria addressed — no duplicate connections, backoff reset on foreground, works on iOS and Android.

PR created: http://143.198.27.163:3000/replit/timmy-tower/pulls/77 Added AppState-aware WebSocket reconnect in TimmyProvider. On foreground return, the app checks if the WS is still open and reconnects immediately if not. On background, the WS is proactively closed to save battery. A new "reconnecting" status with amber pulsing badge was added to ConnectionBadge. All acceptance criteria addressed — no duplicate connections, backoff reset on foreground, works on iOS and Android.
Sign in to join this conversation.
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: replit/timmy-tower#33