feat(api-graphql): add WebSocket health monitoring and manual reconnection #14563
+164
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 trackinggetPersistentConnectionHealth()
- Returns health state with cross-session persistence via AsyncStorage/localStorageisConnected()
- Checks if WebSocket.readyState === OPEN for immediate connection statusreconnect()/disconnect()
- Manual connection control for user-triggered reconnection scenariosTechnical 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.