Skip to content

Conversation

@rahul-lohra
Copy link
Contributor

@rahul-lohra rahul-lohra commented Dec 1, 2025

Goal

Improve coordinator socket reconnection behavior after the JWT token expires.

Implementation

We need to solve 2 problems

  1. Renewing the JWT token reliably
  2. Ensuring a single source of truth for JWT storage and retrieval across the SDK

1. Renewing the JWT token reliably

  1. We are renewing the token in CoordinatorSocket.onVideoNetworkError
  2. Due to the multi-threaded nature of socket event handling, token renewal could previously behave inconsistently.
    To fix this, CoordinatorSocketConnection will now process its events on a single dedicated thread, ensuring deterministic execution and avoiding race conditions during JWT refresh.

2. Single Source of Truth for JWT

To ensure that every component consistently uses the latest token:

  1. Introduce a new TokenRepository, responsible for storing and providing the most up-to-date JWT.

  2. Remove all logic where classes previously kept their own copy of the token string and replace it with calls to TokenRepository.

  3. Add two new TokenProvider implementations:

    • PersistingTokenProvider
      Similar to CacheableTokenProvider, but also persists the freshly fetched token into TokenRepository.
    • RepositoryTokenProvider
      A lightweight provider that simply returns the token stored in TokenRepository.
  4. Replace CacheableTokenProvider usage with PersistingTokenProvider where token updates must be tracked.

  5. Replace ConstantTokenProvider usage with RepositoryTokenProvider where consumers only need to read the token.

🎨 UI Changes

None

Testing

  1. Reduce the JWT expiry time to 30 seconds using StreamService.TOKEN_EXPIRY_TIME to trigger rapid token expiration.
  2. Start a new call.
  3. Open the logs.
  4. You should observe a JWTAuth error, indicating that the JWT token has expired.
  5. The built-in retry mechanism should automatically attempt reconnection with a new token.
  6. Send a reaction.
  7. You should now see custom events from the coordinator socket, confirming that:
    • The JWT was successfully refreshed, and
    • The coordinator connection is receiving events again.

rahul-lohra and others added 30 commits November 7, 2025 14:26
# Conflicts:
#	stream-video-android-ui-compose/build.gradle.kts
…tion' into featture/rahullohra/video-moderation
…tion' into featture/rahullohra/video-moderation
…wt-token-repository

# Conflicts:
#	stream-video-android-core/api/stream-video-android-core.api
#	stream-video-android-core/src/main/kotlin/io/getstream/android/video/generated/models/CallStatsLocation.kt
@github-actions
Copy link
Contributor

github-actions bot commented Dec 8, 2025

PR checklist ✅

All required conditions are satisfied:

  • Title length is OK (or ignored by label).
  • At least one pr: label exists.
  • Sections ### Goal, ### Implementation, and ### Testing are filled.

🎉 Great job! This PR is ready for review.

@rahul-lohra rahul-lohra self-assigned this Dec 8, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 8, 2025

SDK Size Comparison 📏

SDK Before After Difference Status
stream-video-android-core 11.94 MB 11.94 MB 0.00 MB 🟢
stream-video-android-ui-xml 5.68 MB 5.68 MB 0.00 MB 🟢
stream-video-android-ui-compose 6.27 MB 6.27 MB 0.00 MB 🟢

@rahul-lohra rahul-lohra added the pr:improvement Enhances an existing feature or code label Dec 8, 2025
The log message for socket disconnection now includes the specific error for better debugging.

Additionally, a TODO comment was added to the `sendData` method in `SocketActions.kt` to refactor it to return a boolean in the future.
@rahul-lohra rahul-lohra marked this pull request as ready for review December 9, 2025 12:58
@rahul-lohra rahul-lohra requested a review from a team as a code owner December 9, 2025 12:58
@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
57.7% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@rahul-lohra rahul-lohra changed the title Improve Coordinator Socket reconnection with TokenRepository [AND-909] Improve Coordinator Socket reconnection with TokenRepository Dec 11, 2025
@rahul-lohra rahul-lohra changed the title [AND-909] Improve Coordinator Socket reconnection with TokenRepository Improve Coordinator Socket reconnection with TokenRepository Dec 11, 2025
@aleksandar-apostolov aleksandar-apostolov merged commit 8619650 into develop Dec 11, 2025
13 of 14 checks passed
@aleksandar-apostolov aleksandar-apostolov deleted the bugfix/rahullohra/coordinator-reconnect-jwt-token-repository branch December 11, 2025 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr:improvement Enhances an existing feature or code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants