Next.js Discord

Discord Forum

How to implement server-sent events in route handlers?

Unanswered
Orinoco Crocodile posted this in #help-forum
Open in Discord
Orinoco CrocodileOP
Hi I want to do this but in the api/api routes.
res.writeHead(200, {
    Connection: 'keep-alive',
    'Content-Encoding': 'none',
    'Cache-Control': 'no-cache, no-transform',
    'Content-Type': 'text/event-stream',
  });

    res.write(
      `data: ${JSON.stringify({
        message: 'hello',
        value: 123,
      })}\n\n`
    );

How can I get the res and the res.writeHead function.

I basically want to implement this code https://github.com/vercel/next.js/discussions/48427#discussioncomment-6230366 with the new routes api.

Thank you for your help !

4 Replies

Orinoco CrocodileOP
Nobody knows ?
Orinoco CrocodileOP
Not sure its the same thing 🙂
Basically I don't find how to do a res.write and a res.writeHead with the new NextResponse