Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Metadata } from 'next';
import { NotificationSSEProvider } from '@/app/providers/notification-sse-provider';
import { QueryProvider } from '@/app/providers/query-provider';
import { ThemeProvider } from '@/app/providers/theme-provider';
import { PwaUpdatePrompt } from '@/shared/ui/pwa-update-prompt';
Expand Down Expand Up @@ -48,7 +49,10 @@ export default function RootLayout({
<html lang="ko" suppressHydrationWarning>
<body className="min-h-screen bg-background font-sans antialiased">
<ThemeProvider>
<QueryProvider>{children}</QueryProvider>
<QueryProvider>
<NotificationSSEProvider />
{children}
</QueryProvider>
</ThemeProvider>
<PwaUpdatePrompt />
</body>
Expand Down
13 changes: 13 additions & 0 deletions src/app/providers/notification-sse-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use client';

import { useNotificationSSE } from '@/features/notification';
import { useAuthStore } from '@/shared/model/auth-store';

export function NotificationSSEProvider() {
const isAuthenticated = useAuthStore((state) => state.isAuthenticated);
const userId = useAuthStore((state) => state.userId);

useNotificationSSE({ enabled: isAuthenticated || Boolean(userId) });

return null;
}
1 change: 1 addition & 0 deletions src/features/notification/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export {
useMarkNotificationRead,
useNotifications,
} from './model/use-notifications';
export { useNotificationSSE } from './model/use-notification-sse';
export type { NotificationItem } from './api/notification-api';
89 changes: 89 additions & 0 deletions src/features/notification/model/use-notification-sse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use client';

import { useEffect, useRef } from 'react';
import { useQueryClient } from '@tanstack/react-query';
import { backendProxyEndpoint, endpoints } from '@/lib/endpoint';
import { notificationKeys } from './use-notifications';

type UseNotificationSSEOptions = {
enabled?: boolean;
};

const BASE_RECONNECT_DELAY_MS = 1_000;
const MAX_RECONNECT_DELAY_MS = 30_000;

export function useNotificationSSE({
enabled = true,
}: UseNotificationSSEOptions = {}) {
const queryClient = useQueryClient();
const queryClientRef = useRef(queryClient);

useEffect(() => {
queryClientRef.current = queryClient;
}, [queryClient]);

useEffect(() => {
if (!enabled || typeof EventSource === 'undefined') return;

let es: EventSource | null = null;
let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
let reconnectAttempts = 0;
let closed = false;

const clearReconnectTimer = () => {
if (reconnectTimer) {
clearTimeout(reconnectTimer);
reconnectTimer = null;
}
};

const connect = () => {
clearReconnectTimer();
es?.close();

es = new EventSource(
backendProxyEndpoint(endpoints.notifications.subscribe),
);

es.addEventListener('open', () => {
reconnectAttempts = 0;
});

es.addEventListener('notification', () => {
queryClientRef.current.invalidateQueries({
queryKey: notificationKeys.all,
});
});

es.addEventListener('ping', () => {
// keep-alive 이벤트는 연결 유지용이므로 별도 처리하지 않는다.
});

es.addEventListener('error', () => {
if (
closed ||
reconnectTimer ||
!es ||
es.readyState !== EventSource.CLOSED
) {
return;
}

const delay = Math.min(
BASE_RECONNECT_DELAY_MS * 2 ** reconnectAttempts,
MAX_RECONNECT_DELAY_MS,
);
reconnectAttempts += 1;
reconnectTimer = setTimeout(connect, delay);
});
};

connect();

return () => {
closed = true;
clearReconnectTimer();
es?.close();
};
}, [enabled]);
}
Loading