app.get('/test-sse', (c: Context) => {
let timerId: number | undefined;
const body = new ReadableStream({
start(controller) {
const encoder = new TextEncoder();
timerId = setInterval(() => {
const streamMessage = JSON.stringify({
message: `Hello world ${new Date().toISOString()}`,
});
controller.enqueue(new TextEncoder().encode(`data: ${streamMessage}\r\n\r\n`));
}, 1000);
},
cancel() {
if (typeof timerId === 'number') {
clearInterval(timerId);
}
},
});
return new Response(body, {
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
},
});
});
app.get('/test-sse', (c: Context) => {
let timerId: number | undefined;
const body = new ReadableStream({
start(controller) {
const encoder = new TextEncoder();
timerId = setInterval(() => {
const streamMessage = JSON.stringify({
message: `Hello world ${new Date().toISOString()}`,
});
controller.enqueue(new TextEncoder().encode(`data: ${streamMessage}\r\n\r\n`));
}, 1000);
},
cancel() {
if (typeof timerId === 'number') {
clearInterval(timerId);
}
},
});
return new Response(body, {
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
},
});
});