jsJ
Denoβ€’8mo agoβ€’
3 replies
js

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',
            },
        });
    });
Was this page helpful?