-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat(api-graphql): add WebSocket health monitoring and manual reconnection #14563
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for opening the PR.
Please take a look at the comments
packages/api-graphql/src/Providers/AWSWebSocketProvider/index.ts
Outdated
Show resolved
Hide resolved
| import { awsRealTimeHeaderBasedAuth } from './authHeaders'; | ||
|
|
||
| // Platform-safe AsyncStorage import | ||
| let AsyncStorage: any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have the KeyValueStorageInterface.
It can be (partially) used as a type here. and would be better than any
packages/api-graphql/src/Providers/AWSWebSocketProvider/index.ts
Outdated
Show resolved
Hide resolved
packages/api-graphql/src/Providers/AWSWebSocketProvider/index.ts
Outdated
Show resolved
Hide resolved
packages/api-graphql/src/Providers/AWSWebSocketProvider/index.ts
Outdated
Show resolved
Hide resolved
packages/api-graphql/src/Providers/AWSWebSocketProvider/index.ts
Outdated
Show resolved
Hide resolved
packages/api-graphql/src/Providers/AWSWebSocketProvider/index.ts
Outdated
Show resolved
Hide resolved
packages/api-graphql/src/Providers/AWSWebSocketProvider/index.ts
Outdated
Show resolved
Hide resolved
| this.connectionState === ConnectionState.Connected && | ||
| this.keepAliveTimestamp && | ||
| timeSinceLastKeepAlive !== undefined && | ||
| timeSinceLastKeepAlive < 65000; // 65 second threshold |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be shortened:
const isHealthy =
this.connectionState === ConnectionState.Connected &&
timeSinceLastKeepAlive < 65000; // 65 second threshold| // Use the more recent timestamp (in-memory vs persistent) | ||
| const lastKeepAliveTime = | ||
| Math.max(this.keepAliveTimestamp || 0, persistentKeepAliveTime || 0) || | ||
| undefined; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the undefined?
why not have lastKeepAliveTime be 0?
this simplifies timeSinceLastKeepAlive and isHealthy, too
1eb4414 to
044fc3c
Compare
…ction - Add getConnectionHealth() method for current connection state - Add getPersistentConnectionHealth() for cross-session health tracking - Add isConnected() for quick connection status check - Add reconnect() for manual WebSocket reconnection - Implement persistent keep-alive tracking using AsyncStorage/localStorage - Use 65-second threshold for health monitoring - Track keep-alive timestamps for connection health assessment Fixes aws-amplify#9749, aws-amplify#4459, aws-amplify#5403, aws-amplify#7057
d1cf448 to
9c832cd
Compare
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.