Skip to content

Exchange - WebSockets instead of polling (for User and Chat updates) - #1775

Merged
i5hi merged 8 commits into
developfrom
exchange-sockets-chat-user
Jan 21, 2026
Merged

Exchange - WebSockets instead of polling (for User and Chat updates)#1775
i5hi merged 8 commits into
developfrom
exchange-sockets-chat-user

Conversation

@mocodesmo

@mocodesmo mocodesmo commented Jan 12, 2026

Copy link
Copy Markdown
Contributor

🔔 1. Real-Time Notifications via WebSocket

Before: User data (balance, KYC status, groups) was polled every 5 seconds using a timer, causing unnecessary network requests and delayed updates.

After: Implemented WebSocket-based real-time notifications that push updates instantly when changes occur:

  • Balance updates – User sees balance changes immediately
  • KYC status changes – KYC approval/rejection reflected in real-time
  • Group membership updates – Instant notification of user group changes
  • Support chat messages – New messages appear without manual refresh

💬 2. Enhanced Support Chat Experience

Real-time message delivery:

  • New chat messages from support are now pushed instantly via WebSocket
  • No more manual refresh or polling required
  • Messages appear immediately in the chat UI

Improved attachment handling:

  • Cleaner image picker flow with proper permission handling
  • Better error messages for permission issues:
    • "Permission denied." (temporary)
    • "Permission denied. Please grant photo library access in Settings." (permanent)
    • "Failed to pick files. Please try again." (general error)
  • Streamlined file sharing functionality

🔄 3. Smarter Connection Lifecycle Management

Login/Logout awareness:

  • WebSocket automatically connects when user logs in
  • WebSocket automatically disconnects when user logs out
  • No lingering connections or unnecessary reconnects

Network environment switching (Mainnet ↔ Testnet):

  • WebSocket reconnects to the correct server when switching networks
  • Seamless transition without manual intervention

Auto-reconnect on disconnection:

  • If connection drops unexpectedly, auto-reconnects after 5 seconds
  • Manual disconnects (logout) don't trigger auto-reconnect

🏗️ 4. Architecture Improvements (Supporting Better UX)

Component Change
ExchangeListener New widget wrapping the app to manage WebSocket connection lifecycle based on user authentication state
ExchangeCubit Removed polling timer, added WebSocket connection management (connectWebSocket, disconnectWebSocket, reconnectWebSocket)
ExchangeSupportChatCubit Listens to notification stream for instant message updates

📁 New Files Added

File Purpose
exchange_listener.dart Manages WebSocket lifecycle based on login state
exchange_notification_datasource.dart WebSocket connection handling
exchange_notification_usecase.dart Environment-aware notification routing
pick_image_attachments_usecase.dart Clean abstraction for image selection
share_attachment_usecase.dart Clean abstraction for file sharing
permission_datasource.dart Platform-specific permission handling
image_picker_datasource.dart Image picker abstraction
file_share_datasource.dart File sharing abstraction

✨ Key User-Facing Benefits

  1. Faster feedback – Balance and status updates appear instantly instead of up to 5 seconds later
  2. Real-time chat – Support messages appear immediately without refreshing
  3. Less battery drain – No more constant polling; WebSocket is more efficient
  4. Smoother UX – Connection state is managed intelligently based on user actions
  5. Better error handling – Clearer permission error messages guide users on how to fix issues

- Updated pubspec.yaml to include web_socket_channel dependency.
- Enhanced ExchangeLocator to register ExchangeNotificationDatasource, ExchangeNotificationRepository, and ExchangeNotificationUsecase.
- Integrated ExchangeNotificationUsecase into ExchangeCubit and ExchangeSupportChatCubit for handling notifications.
- Implemented WebSocket connection management in ExchangeCubit and ExchangeSupportChatCubit.
- Updated ExchangeSupportChatScreen to utilize the new ExchangeNotificationUsecase.
- Added new datasources for image picking, file sharing, and permissions in ExchangeLocator.
- Integrated PickImageAttachmentsUsecase and ShareAttachmentUsecase into ExchangeSupportChatCubit for improved attachment handling.
- Refactored addAttachment method to utilize the new use cases for better error management and code clarity.
- Updated ExchangeSupportChatScreen to include the new use cases in the cubit initialization.
- Updated ExchangeNotificationDatasource to improve WebSocket connection handling, including a new method for building WebSocket URLs.
- Added connection state management to prevent multiple simultaneous connection attempts.
- Enhanced error handling during connection failures and implemented auto-reconnect logic.
- Modified ExchangeCubit to filter messages for user notifications based on multiple types.
- Integrated ExchangeListener in main.dart to manage WebSocket connections more effectively.
Comment thread lib/core/exchange/data/datasources/image_picker_datasource.dart Outdated
Comment thread lib/core/exchange/data/datasources/file_share_datasource.dart Outdated
Comment thread lib/core/exchange/data/repository/exchange_notification_repository_impl.dart Outdated
Comment thread lib/core/exchange/domain/usecases/pick_image_attachments_usecase.dart Outdated
Comment thread lib/core/exchange/domain/usecases/share_attachment_usecase.dart Outdated
@i5hi

i5hi commented Jan 16, 2026

Copy link
Copy Markdown
Collaborator

Once all these comments are made into separate issues and this PR is tested and working we can merge.

- Removed ExchangeNotificationRepository and ExchangeNotificationUsecase to streamline notification handling.
- Introduced ExchangeNotificationService for improved WebSocket management and message handling.
- Updated ExchangeLocator and related classes to utilize the new service, ensuring proper registration and dependency injection.
- Adjusted ExchangeCubit and ExchangeSupportChatCubit to connect and manage notifications through the new service.
@mocodesmo

Copy link
Copy Markdown
Contributor Author

@kumulynja please re-review this pr.
i have updated according to the comments you have suggested.

@kumulynja
kumulynja self-requested a review January 19, 2026 12:25

@kumulynja kumulynja left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes @mocodesmo 👍

@i5hi
i5hi merged commit 986e5c2 into develop Jan 21, 2026
1 check passed
@thibistaken
thibistaken deleted the exchange-sockets-chat-user branch March 19, 2026 08:52
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.

3 participants