Pulastya B commited on
Commit
3c36cfe
·
1 Parent(s): af8cd44

Fix SSE reconnection issue for follow-up requests

Browse files
FRRONTEEEND/components/ChatInterface.tsx CHANGED
@@ -69,9 +69,20 @@ export const ChatInterface: React.FC<{ onBack: () => void }> = ({ onBack }) => {
69
  return;
70
  }
71
 
72
- // Don't reconnect if already connected to this session
 
73
  if (eventSourceRef.current) {
74
- return;
 
 
 
 
 
 
 
 
 
 
75
  }
76
 
77
  // Connect to SSE stream - will receive history + any new events
@@ -115,11 +126,11 @@ export const ChatInterface: React.FC<{ onBack: () => void }> = ({ onBack }) => {
115
  }
116
  };
117
 
118
- // Handle errors
119
  eventSource.onerror = (err) => {
120
- console.error('❌ SSE error:', err);
121
- eventSource.close();
122
- eventSourceRef.current = null;
123
  };
124
 
125
  eventSourceRef.current = eventSource;
 
69
  return;
70
  }
71
 
72
+ // Check if we need a new connection for this session
73
+ // Close old connection if it exists and belongs to a different session or is closed
74
  if (eventSourceRef.current) {
75
+ const currentSource = eventSourceRef.current;
76
+ // If readyState is CLOSED (2), we need a new connection
77
+ // If it's CONNECTING (0) or OPEN (1) for the same session, reuse it
78
+ if (currentSource.readyState === 2) {
79
+ console.log('🔄 Existing connection closed, creating new one');
80
+ currentSource.close();
81
+ eventSourceRef.current = null;
82
+ } else {
83
+ console.log('♻️ Reusing existing SSE connection');
84
+ return;
85
+ }
86
  }
87
 
88
  // Connect to SSE stream - will receive history + any new events
 
126
  }
127
  };
128
 
129
+ // Handle errors - DON'T immediately close, just log
130
  eventSource.onerror = (err) => {
131
+ console.error('❌ SSE connection error/closed:', err);
132
+ // Don't close here - let it reconnect naturally on next request
133
+ // The readyState check above will handle creating a new connection if needed
134
  };
135
 
136
  eventSourceRef.current = eventSource;