-
Notifications
You must be signed in to change notification settings - Fork 218
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
feat: add utilities for server sent events #586
Conversation
add SseSession class export ./sse from utils/index.ts add unit tests for new sse utilities
add SSE notes to README.md
One thing that I'm kind of iffy on is whether it's better to explicitly start sending the stream to the client with Right now I think being more explicit is better, but I'm open to differing opinions on this. |
just my personal thoughts.
and these control flows should inside Apps not transports. how about provide a utility controlling state? const bSSE = buffered(createEventStream(event))
setInterval(() => bSSE.push({}), 100)
let started = true
setInterval(() => {
(started = !started) ? (bSSE.start(), bSSE.flush()) : bSSE.stop()
}, 1000)
// transmit every other second |
@arily thanks for the input. It's made me reevaluate how this API should be structured. Right now However this all has made me think that naming the method Although this has made me realize that it may be even better to make a separate utility for returning the We could also add logic allowing devs to simply return an So basically the adjusted API would look like this: // using the send utilty
eventHandler((event) => {
const eventStream = createEventStream(event);
setInterval(() => {
eventStream.push("hello world");
})
sendEventStream(event, eventStream);
}
// using a simple return
eventHandler((event) => {
const eventStream = createEventStream(event)
setInterval(() => {
eventStream.push("hello world")
}, 1000)
return eventStream;
}) |
I do like the idea of |
add pause, resume, and flush methods update tests and examples to work with new changes
Ah sorry my wording might not be accurate, by "control flow" I mean control the start/ stop of the flow of transmitting event. |
Yes I think I was following you. It was my own usage of We need to return the eventStream to the client (which is what By making a separate |
I went ahead and implemented the new API. It looks like this: eventHandler((event) => {
const eventStream = createEventStream(event)
setInterval(async () => {
await eventStream.push("Hello world")
}, 1000)
return eventStream
}) I think it feels more clear this way. I also implemented the Screencast.from.12-30-2023.12.42.06.AM.webm |
Very nice! Just another thought, I think for GC to properly recycle any streams we are done for, or to terminate the connection before client does, we may need a |
Yeah so I've already implemented an |
- more explicit cache-control headers to prevent browsers from ever caching the response - add x-accel-buffering header to prevent nginx from ever caching the response
β¦nection is closed - when calling "close()" only close the connection if the stream was actually sent to the client - cleanup the event trigger names to make things more clear
Added some changes to make cleanup easier and the event callbacks less confusing:
|
omit non-http2 support headers from sse response in http1 requests
I've published the changes I've made here as a standalone npm package in case anyone wants to make use of the changes now I'll be maintaining this repo until native SSE support comes to H3. (Either by this PR getting merged or some other change) |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #586 +/- ##
==========================================
- Coverage 77.83% 77.62% -0.21%
==========================================
Files 47 52 +5
Lines 4286 4836 +550
Branches 611 635 +24
==========================================
+ Hits 3336 3754 +418
- Misses 933 1063 +130
- Partials 17 19 +2 β View full report in Codecov by Sentry. |
Hi dear @joshmossas thanks so much for putting all efforts on this! I quickly checked implementation it is flawless and nice! I have made few refactors out of my instincts in order to move this forward as fast as possible.
Thinking to merge and try on nightly but please consider my changes blind changes and feel free to suggest any amends or make subsequent PRs. |
Awesome thank you so much @pi0 . I was honestly thinking of enabling autoclose by default as well, so I'm glad you thought the same. I agree I suppose the solution is to make the internals for |
Or maybe we just make it so Here me out. The default is now the writer will automatically close when the request closes. So if someone chooses to opt out of That also means the Making autoclose default actually simplified the whole thought process around cleanup and hooks now that I think about it. |
π Linked issue
#477
β Type of change
π Description
This PR adds utilities for server sent events. This enables developers to easily create endpoints that return an event stream as outlined here
The new utilities can be used like so:
Other Changes
Unit tests have been added to tests these new features
The README.md has been updated to include the
createEventStream
utility in its utility list.π Checklist