feat: Manage socket connection lifecycle based on authentication status and ensure chat screen joins rooms only when socket is connected.

This commit is contained in:
pradeepkumar
2026-03-15 00:30:12 +05:30
parent fe2bc5bf28
commit 54fdd66270
2 changed files with 25 additions and 2 deletions

View File

@@ -37,8 +37,17 @@ class _ChatScreenState extends ConsumerState<ChatScreen> {
notifier.setActiveConversation(widget.conversationId);
notifier.loadMessages(widget.conversationId);
notifier.markAsRead(widget.conversationId);
// Join socket room for real-time updates
_socket.joinConversation(widget.conversationId);
// Join socket room for real-time updates (socket is connected at auth time)
if (_socket.isConnected) {
_socket.joinConversation(widget.conversationId);
} else {
// Wait for socket connection then join
_socket.onConnectionChange.first.then((connected) {
if (connected && mounted) {
_socket.joinConversation(widget.conversationId);
}
});
}
});
_scrollController.addListener(_onScroll);
_messageController.addListener(_onTextChanged);