문제 상황

Server-Sent Events(SSE) 연결이 끊어질 때마다 자동으로 재연결을 시도하는 로직으로 인해 다음과 같은 문제가 발생했습니다:

  1. 불필요한 재연결 시도로 서버 부하 증가
  2. 사용자가 의도적으로 연결을 종료했을 때도 재연결 시도
  3. 홈 화면으로 돌아올 때 새로운 연결이 자동으로 생성되므로 재연결 로직이 중복

해결 방안

1. 재연결 로직 제거

2. 사용자 피드백 개선

코드 변경사항

이전 코드의 문제점

typescript
Copy
const connectSSE = (userId: string) => {
  let retryCount = 0;
  const MAX_RETRIES = 3;
  let retryTimeout: NodeJS.Timeout;

// 재연결 시도 로직// 복잡한 재시도 메커니즘
}

개선된 코드

typescript
Copy
const connectSSE = (userId: string) => {
  const { setEventSource } = useSSEStore.getState();

  try {
    const eventSource = new EventSource(url, {
      withCredentials: true,
    });

// 단순화된 에러 처리
    eventSource.onerror = (error) => {
      if (eventSource.readyState === EventSource.CLOSED) {
        eventSource.close();
        setEventSource(null);
        toast.error('실시간 알림 연결이 끊어졌습니다');
      }
    };

    return eventSource;
  } catch (error) {
    console.error('SSE 초기화 실패:', error);
    return null;
  }
}

개선 효과

  1. 코드 복잡도 감소