-
-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to send server side events (on Mongodb watch() for example) #84
Comments
Set the Also, this isn't really an issue so I guess it can be closed. |
Hello, just coming back to that issue. Sending the event data with I'm not expecting any fix, just trying to clarify the issue for the ones who'll try to use polka with SSE. |
The last message is correct and is not Polka specific. The header is necessary for SSE in general and response.write is a native Nodejs API |
@lukeed it is indeed not polka specific but given it uses Node.js http server, shouldn't it be working with these headers and writing to the response with For example: import { createServer } from "node:http";
const srv = createServer((req, res) => {
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"access-control-allow-origin": "*",
Connection: "keep-alive",
});
setInterval(() => {
res.write(`data: ${Date.now()}\n\n`);
}, 1000);
req.on("close", () => {
res.end();
});
});
srv.listen(4000, () => {}); This works, but if I now switch createServer for polka, this does not seem to write to the event stream anymore. Again I'm not asking for any fix (even though I could send a PR if this is an issue), just trying to clarify the situation for people (including me) using polka and trying to setup SSE. |
You're absolutely right @antoine-coulon, I probably confused It's been almost 5 years since I last used Polka (or any Node server framework/lib), but I remember having to call Discussion (relevant comment): https://stackoverflow.com/questions/60372306/how-to-flush-data-to-client-just-after-res-write-is-written-before-res-end#comment111542785_60384570 |
@guilhermeoc97 actually the example I pasted just above works with Node.js |
Can you post the code using Polka? |
Actually it works I don't know what dummy things I did to not make it work (I might have mixed up APIs while testing with So to close that discussion, I'm happily posting an example of SSE working very simply and as expected with our dear import polka from "polka";
const app = polka();
app.get("/subscribe", (request, response) => {
response.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
Connection: "keep-alive",
// Might not be needed if running the front and backend one the same domain
"access-control-allow-origin": "*",
});
const intervalId = setInterval(() => {
response.write(`data: ${new Date().toLocaleString()}\n\n`);
}, 1000);
request.on("close", () => {
clearInterval(intervalId);
});
});
app.listen(4000, () => {
console.log("Server is listening on port 4000");
}); By the way @lukeed do you think it could be useful to add that in the |
Great! And sure, go for it! |
Just went for it, opened there #204 |
I think I found what led me thinking that polka was not working with SSE in my previous setup, this was due to compression middleware I'm currently using in skott. According
Again this is not related to polka but to the middleware I was using, so be careful if using it, you'll need to manually call |
That makes sense! |
No description provided.
The text was updated successfully, but these errors were encountered: