Skip to content

feat(api-graphql): add WebSocket connection health monitoring#14563

Open
anivar wants to merge 2 commits intoaws-amplify:mainfrom
anivar:feat/websocket-health-monitoring
Open

feat(api-graphql): add WebSocket connection health monitoring#14563
anivar wants to merge 2 commits intoaws-amplify:mainfrom
anivar:feat/websocket-health-monitoring

Conversation

@anivar
Copy link
Copy Markdown
Contributor

@anivar anivar commented Sep 24, 2025

Description of changes

Add read-only WebSocket connection health diagnostics to AWSWebSocketProvider:

  • getConnectionHealth() — returns WebSocketHealthState with:

    • isHealthy: true when connection state is Connected AND last keep-alive was received within 65 seconds
    • connectionState: current ConnectionState enum value
    • lastKeepAliveTime: timestamp of most recent keep-alive
    • timeSinceLastKeepAlive: milliseconds since last keep-alive
  • isConnected() — checks raw WebSocket readyState === OPEN

These methods let consumers detect stale connections and build health indicators without modifying any internal state.

Removed from original implementation:

  • Broken reconnect() method (called close() which destroys subscription state and reconnection monitor, making recovery impossible)
  • Over-engineered persistent storage (module-level require() side effect for AsyncStorage, localStorage I/O on every keep-alive, useless after app restart)
  • Unused WebSocketControl interface (never implemented)
  • getPersistentConnectionHealth() (depended on removed persistence)

Issue #, if available

N/A

Description of how you validated changes

  • 10 unit tests covering all connection states, keep-alive staleness, and WebSocket readyState values
  • Verified 42 existing AWSAppSyncRealTimeProvider tests still pass
  • TypeScript compilation clean (no new errors)

Checklist

  • PR description included
  • yarn test passes
  • Unit Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link
Copy Markdown
Member

@bobbor bobbor left a 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

@anivar anivar force-pushed the feat/websocket-health-monitoring branch 2 times, most recently from d1cf448 to 9c832cd Compare October 31, 2025 13:35
@anivar anivar requested a review from bobbor November 11, 2025 16:51
@bobbor bobbor self-assigned this Dec 3, 2025
@anivar
Copy link
Copy Markdown
Contributor Author

anivar commented Dec 5, 2025

Hi @bobbor - I've addressed all the review feedback and made additional improvements to code quality.

Review feedback addressed:

  • Fixed Promise handling with proper .catch()
  • Using KeyValueStorageInterface instead of any
  • Proper import statements at the top
  • Simplified code where possible
  • Using Date.now() instead of JSON.stringify(new Date())

Additional refactoring:

  • Extracted helper methods (calculateHealthState, getPersistedKeepAliveTimestamp, persistKeepAliveTimestamp)
  • Refactored platform storage initialization into testable function
  • Added concurrency guards and proper error handling to reconnect()
  • Fixed type consistency in WebSocketHealthState (non-optional fields with defaults)

Test coverage:

  • Added 21 unit tests covering all new functionality
  • All 170 tests passing

No breaking changes. Ready for re-review.

@anivar anivar force-pushed the feat/websocket-health-monitoring branch from f801f49 to d96ad88 Compare March 7, 2026 13:09
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 7, 2026

🦋 Changeset detected

Latest commit: f8c906a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@aws-amplify/api-graphql Patch
@aws-amplify/api Patch
@aws-amplify/datastore Patch
aws-amplify Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Add getConnectionHealth() and isConnected() to AWSWebSocketProvider for
real-time connection diagnostics. getConnectionHealth() reports whether the
connection is healthy based on connection state and keep-alive staleness
(65s threshold). isConnected() checks raw WebSocket readyState.

Removed broken reconnect(), over-engineered persistent storage, and unused
WebSocketControl interface from original implementation.
@anivar anivar force-pushed the feat/websocket-health-monitoring branch from d96ad88 to 253b44c Compare March 7, 2026 14:34
@anivar anivar changed the title feat(api-graphql): add WebSocket health monitoring and manual reconnection feat(api-graphql): add WebSocket connection health monitoring Mar 7, 2026
@anivar
Copy link
Copy Markdown
Contributor Author

anivar commented Mar 7, 2026

Hi @bobbor — thanks for the thorough review. I've refactored significantly based on your feedback. Most points were addressed by simplifying the scope:

Your review points → resolution:

  1. Unhandled Promise on AsyncStorage.setItem → Persistent storage removed (unnecessary complexity for this use case)
  2. Use KeyValueStorageInterface type → No longer needed without storage
  3. Use import statement instead of inline require() → Removed along with storage
  4. Unnecessary _ prefix on variables → Removed with persistent health methods
  5. reconnect(): just await this.close() → Removed reconnect() — on closer review, close() tears down subscription state and the reconnection monitor, so reconnect-after-close can't work with the current architecture
  6. Use Date.now() directly → No more persistence code
  7. Unnecessary ternary on keepAliveTimestamp → Fixed, it's always initialized to Date.now()
  8. Simplify isHealthy calculation → Done, inlined directly
  9. lastKeepAliveTime should be 0 not undefined → Simplified

What remains (139 lines, down from 571):

  • getConnectionHealth() — read-only diagnostics
  • isConnected()readyState === OPEN check
  • WebSocketHealthState type
  • 10 unit tests

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.

2 participants