js
js•3w ago

ReadableStream doesn't work in production

I'm following the Deno SSE example to set up a GET route for SSE payloads. Exact I got from the SSE examples works ONLY in local development. In production, the requests are stuck in "pending" forever. Is there something different about localhost and production environment that I have to change?
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',
},
});
});
2 Replies
Doctor 🤖
Doctor 🤖•3w ago
What does the client code look like?
lcasdev
lcasdev•3w ago
What does your production look like? Like where are you hosting, and do you have any load balancers in front of the application?

Did you find this page helpful?