Added SSE function and add readAt for notification

This commit is contained in:
2026-02-21 22:34:29 +07:00
parent fa689ea4aa
commit ab745e6a2f
17 changed files with 349 additions and 43 deletions

24
src/hooks/use-sse.ts Normal file
View File

@@ -0,0 +1,24 @@
import { useEffect } from 'react';
export function useSSE(onMessage: (data: any) => void) {
useEffect(() => {
const eventSource = new EventSource('/api/notify');
eventSource.onmessage = (event) => {
try {
const parsed = JSON.parse(event.data);
onMessage(parsed);
} catch (err) {
console.error('Invalid SSE data', err);
}
};
eventSource.onerror = () => {
eventSource.close();
};
return () => {
eventSource.close();
};
}, [onMessage]);
}