Skip to content

Conversation

anivar
Copy link
Contributor

@anivar anivar commented Sep 24, 2025

This PR addresses several long-standing user requests for better WebSocket connection control in the GraphQL API client. Users have reported issues with WebSocket connections not automatically reconnecting after network interruptions and lacking visibility into connection health status ( Fixes #9749, #4459, #5403, #7057).

The implementation enhances the existing AWSWebSocketProvider class with four new methods that leverage the existing ConnectionStateMonitor infrastructure and keep-alive tracking. These methods provide both real-time health monitoring and manual connection control that users have been requesting.

Enhanced WebSocket Provider Methods

  • getConnectionHealth() - Returns current health state using in-memory keep-alive tracking
  • getPersistentConnectionHealth() - Returns health state with cross-session persistence via AsyncStorage/localStorage
  • isConnected() - Checks if WebSocket.readyState === OPEN for immediate connection status
  • reconnect()/disconnect() - Manual connection control for user-triggered reconnection scenarios

Technical Implementation

The health monitoring uses the existing 65-second keep-alive threshold (DEFAULT_KEEP_ALIVE_ALERT_TIMEOUT) already established in the AppSync WebSocket protocol. A WebSocket is considered healthy when three conditions are met: connection state is Connected, a keep-alive was received within 65 seconds, and the underlying WebSocket readyState is OPEN.

For persistence, the implementation uses a platform-safe approach with AsyncStorage for React Native and localStorage fallback for web platforms. Keep-alive timestamps are stored at 'AWS_AMPLIFY_LAST_KEEP_ALIVE' to avoid key collisions. When storage is unavailable, the feature gracefully degrades to in-memory tracking only.

All changes are backward compatible with no breaking changes to existing functionality.

- Add ConnectionHealthMonitor class to track keep-alive messages
- Record connection establishment and keep-alive timestamps
- Provide health check API with configurable thresholds
- Dispatch Hub events for monitoring integration
- Add comprehensive test coverage

This addresses the need for WebSocket health monitoring without
performance-impacting workarounds like AsyncStorage writes.
… patterns

- Use Observable pattern matching ConnectionStateMonitor
- Replace Hub events with ConsoleLogger for consistency
- Follow Amplify naming conventions (ConnectionHealthState)
- Use proper lifecycle with close() method
- Integrate with existing monitoring patterns
- Add automatic health check timer
- Provide comprehensive health state tracking
- Replace manual observer pattern with RxJS BehaviorSubject
- Add lifecycle management to prevent infinite timer loops
- Implement proper error handling with try-catch blocks
- Add input validation for constructor parameters
- Fix memory leak from improper Observable usage
- Cap consecutive missed keep-alives at reasonable limit
- Ensure timers stop when monitor closes
- Add JSDoc comments for public methods
- Complete observables properly on close

BREAKING CHANGE: getHealthStateObservable() now returns Observable<ConnectionHealthState> directly instead of Optional
The existing ConnectionStateMonitor already provides keep-alive tracking:
- Tracks keepAliveState (healthy/unhealthy)
- Handles KEEP_ALIVE and KEEP_ALIVE_MISSED events
- Maps to ConnectedPendingKeepAlive state when unhealthy

Removed duplicate ConnectionHealthMonitor class to reduce code duplication.
…ction

- Add getConnectionHealth(), isConnected(), reconnect(), disconnect() methods
- Fixes aws-amplify#9749 aws-amplify#4459 aws-amplify#5403 aws-amplify#7057
- Backward compatible, no breaking changes
@anivar anivar requested a review from a team as a code owner September 24, 2025 20:42
…orage

- Add getConnectionHealth() and getPersistentConnectionHealth() methods
- Add isConnected(), reconnect(), and disconnect() controls
- Implement cross-platform persistent storage (AsyncStorage/localStorage)
- Track keep-alive messages with 65-second health threshold
- Fixes aws-amplify#9749, aws-amplify#4459, aws-amplify#5403, aws-amplify#7057
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unable to reconnect AppSync after connection interruption (web) or app going into background (ios/ android)
1 participant