cmorten / opine

Minimalist web framework for Deno ported from ExpressJS.
https://github.com/cmorten/opine/blob/main/.github/API/api.md
MIT License
854 stars 43 forks source link

server-sent-events with opine #174

Closed AlayKharadi closed 1 year ago

AlayKharadi commented 1 year ago

Issue

Setup:

Details

I was trying to write a simple server sent events example with opine. Ideally the TCP connection for the HTTP request should be open and server should be able to write data chunk to the response stream. Once the whole data is sent or client is disconnected, connection should be closed.

app.get("/server-sent-events", (req: OpineRequest, res: OpineResponse) => {
res.setHeader("Content-Type", "text/event-stream");
res.setHeader('Cache-control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
res.setHeader("Transfer-Encoding", "chunked");
res.send(`data: ${currentData}\n\n`);
});

res.send will close the connection after this data is sent to the user. I wanted to use res.write, but It isn't present in opine. How do I implement this with opine? Below is a similar example for express.

app.get("/server-sent-events", (req, res) => {
res.header("Content-Type", "text/event-stream");
res.header('Cache-control', 'no-cache');
res.header('Connection', 'keep-alive');
res.header("Transfer-Encoding", "chunked");
res.write(`data: ${currentData}\n\n`);
});
cmorten commented 1 year ago

Hi @AlayKharadi

Since the launch of NPM support in Deno this lib has gone into to “maintenance mode” where I’m not looking to evolve the API any further. I would suggest looking to use Express itself over this library in future.

You’re correct in that res.write() is not available - this is actually an API on the Node response object rather than something Express introduces, and isn’t something that was implemented for Opine.

If you’re open to submitting a PR then happy to review.

res.send() does support a Deno.Reader or ReadableStream, not sure if you might be able to workaround using these?

https://cmorten.github.io/opine/modules/_types_.html#denoresponsebody