2026-07-16 06:19:58 -04:00
|
|
|
import { NextRequest } from 'next/server';
|
2026-07-24 07:08:29 -04:00
|
|
|
import { getAuthUser } from '@/lib/auth';
|
|
|
|
|
import postgres from 'postgres';
|
2026-07-16 06:19:58 -04:00
|
|
|
|
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
|
export const maxDuration = 300; // 5 minutes
|
|
|
|
|
|
|
|
|
|
const DEFAULT_COLLECTIONS = [
|
|
|
|
|
'tasks',
|
|
|
|
|
'habits',
|
|
|
|
|
'projects',
|
|
|
|
|
'notes',
|
|
|
|
|
'reports',
|
|
|
|
|
'milestones',
|
|
|
|
|
'notifications',
|
|
|
|
|
];
|
|
|
|
|
|
2026-07-24 07:08:29 -04:00
|
|
|
// GET /api/realtime — Multiplexed SSE endpoint backed by PostgreSQL LISTEN/NOTIFY.
|
2026-07-16 06:19:58 -04:00
|
|
|
export async function GET(request: NextRequest) {
|
|
|
|
|
const user = await getAuthUser(request);
|
|
|
|
|
if (!user) {
|
|
|
|
|
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
|
|
|
|
|
status: 401,
|
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse subscription preferences from query params
|
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
|
|
|
const collectionsParam = searchParams.get('collections') || '';
|
|
|
|
|
const collections = collectionsParam
|
|
|
|
|
.split(',')
|
|
|
|
|
.map((c) => c.trim())
|
|
|
|
|
.filter(Boolean);
|
|
|
|
|
|
|
|
|
|
const subscribedCollections =
|
|
|
|
|
collections.length > 0 ? collections : DEFAULT_COLLECTIONS;
|
|
|
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
2026-07-24 07:08:29 -04:00
|
|
|
const listener = postgres(process.env.DATABASE_URL!, { max: 1 });
|
|
|
|
|
let unlisten: (() => Promise<void>) | undefined;
|
|
|
|
|
let keepalive: ReturnType<typeof setInterval> | undefined;
|
2026-07-16 06:19:58 -04:00
|
|
|
|
|
|
|
|
const stream = new ReadableStream({
|
|
|
|
|
async start(controller) {
|
|
|
|
|
// Send connected event
|
|
|
|
|
controller.enqueue(
|
|
|
|
|
encoder.encode(
|
|
|
|
|
`data: ${JSON.stringify({ type: 'connected', collections: subscribedCollections })}\n\n`
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2026-07-24 07:08:29 -04:00
|
|
|
const subscription = await listener.listen('project_e_events', (payload) => {
|
2026-07-16 06:19:58 -04:00
|
|
|
try {
|
2026-07-24 07:08:29 -04:00
|
|
|
const event = JSON.parse(payload) as { collection?: string };
|
|
|
|
|
if (!event.collection || subscribedCollections.includes(event.collection)) {
|
|
|
|
|
controller.enqueue(encoder.encode(`data: ${JSON.stringify(event)}\n\n`));
|
|
|
|
|
}
|
2026-07-16 06:19:58 -04:00
|
|
|
} catch {
|
2026-07-24 07:08:29 -04:00
|
|
|
// Ignore malformed database notifications and closed streams.
|
2026-07-16 06:19:58 -04:00
|
|
|
}
|
2026-07-24 07:08:29 -04:00
|
|
|
});
|
|
|
|
|
unlisten = subscription.unlisten;
|
2026-07-16 06:19:58 -04:00
|
|
|
|
|
|
|
|
// Keepalive ping every 30 seconds
|
2026-07-24 07:08:29 -04:00
|
|
|
keepalive = setInterval(() => {
|
2026-07-16 06:19:58 -04:00
|
|
|
try {
|
|
|
|
|
controller.enqueue(encoder.encode(':ping\n\n'));
|
|
|
|
|
} catch {
|
2026-07-24 07:08:29 -04:00
|
|
|
if (keepalive) clearInterval(keepalive);
|
2026-07-16 06:19:58 -04:00
|
|
|
}
|
|
|
|
|
}, 30000);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async cancel() {
|
2026-07-24 07:08:29 -04:00
|
|
|
if (keepalive) clearInterval(keepalive);
|
|
|
|
|
await unlisten?.();
|
|
|
|
|
await listener.end({ timeout: 5 });
|
2026-07-16 06:19:58 -04:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return new Response(stream, {
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'text/event-stream',
|
|
|
|
|
'Cache-Control': 'no-cache',
|
|
|
|
|
Connection: 'keep-alive',
|
|
|
|
|
'X-Accel-Buffering': 'no',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|