feat: ditch triplit, move to postgresql + drizzle orm
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { userEvents } from "~~/server/utils/events";
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await protectRoute(event);
|
||||
|
||||
const userId = event.context.user.id;
|
||||
|
||||
let controller: ReadableStreamDefaultController;
|
||||
let pingInterval: NodeJS.Timeout;
|
||||
|
||||
const stream = new ReadableStream({
|
||||
start(c) {
|
||||
controller = c;
|
||||
|
||||
// Send initial connection message
|
||||
try {
|
||||
controller.enqueue(':connected\n\n');
|
||||
} catch (e) {
|
||||
console.error('Failed to send initial connection:', e);
|
||||
return;
|
||||
}
|
||||
|
||||
userEvents.subscribe(userId, controller);
|
||||
|
||||
pingInterval = setInterval(() => {
|
||||
try {
|
||||
controller.enqueue(':heartbeat\n\n');
|
||||
} catch (e) {
|
||||
clearInterval(pingInterval);
|
||||
}
|
||||
}, 15000);
|
||||
},
|
||||
cancel() {
|
||||
console.log(`User ${userId} event stream cancelled`);
|
||||
clearInterval(pingInterval);
|
||||
userEvents.unsubscribe(userId, controller!);
|
||||
}
|
||||
});
|
||||
|
||||
setHeaders(event, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Connection': 'keep-alive',
|
||||
});
|
||||
|
||||
return sendStream(event, stream);
|
||||
});
|
||||
Reference in New Issue
Block a user