const emitter = new EventEmitter();
app.use("/sse/*", async (c, next) => {
c.header("Content-Type", "text/event-stream");
c.header("Cache-Control", "no-cache");
c.header("Connection", "keep-alive");
await next();
});
app.post('/message', async (c) => {
const message = 'test';
emitter.emit('message', message);
return c.json({message});
})
app.get('/sse', async (c) => {
return streamSSE(c, async (stream) => {
const handler = async (message: string) => {
await stream.writeSSE({
data: message,
event: "message"
});
}
stream.onAbort(() => {
console.log('Connection aborted');
})
emitter.on('message', handler);
})
})
const emitter = new EventEmitter();
app.use("/sse/*", async (c, next) => {
c.header("Content-Type", "text/event-stream");
c.header("Cache-Control", "no-cache");
c.header("Connection", "keep-alive");
await next();
});
app.post('/message', async (c) => {
const message = 'test';
emitter.emit('message', message);
return c.json({message});
})
app.get('/sse', async (c) => {
return streamSSE(c, async (stream) => {
const handler = async (message: string) => {
await stream.writeSSE({
data: message,
event: "message"
});
}
stream.onAbort(() => {
console.log('Connection aborted');
})
emitter.on('message', handler);
})
})