giraffe-fsharp / Giraffe

A native functional ASP.NET Core web framework for F# developers.
https://giraffe.wiki
Apache License 2.0
2.11k stars 267 forks source link

Server-Sent Events in Giraffe #469

Open 2mol opened 3 years ago

2mol commented 3 years ago

Hi!

This is more a question about a feature, not an issue.

Does Giraffe have some helpers for creating Server-Sent event streams? For reference, I found them in Suave, relevant docs here.

I couldn't find a reference anywhere in the Giraffe docs. I think https://giraffe.wiki/docs#streaming doesn't quite describe SSEs

Am I missing something? It took me a while to even find them in Suave, since the name EventSource (as defined in the spec) is a bit generic. The feature not so well-known, especially since it's a one-way stream, so websockets are the usual tool of choice for streaming.

More than happy to attempt to implement them!

https://html.spec.whatwg.org/multipage/server-sent-events.html

dustinmoris commented 3 years ago

Nothing in Giraffe for SSE yet. I always felt that things like SSE deserve a new middleware which can handle text/event-stream requests and then register some handlers/publishers to those open connections which can publish event messages on them. It would be something completely new essentially which wouldn't have much shared code with the rest of Giraffe. What sort of implementation were you thinking of? A PR would be nice if you want to take on this work but let's first flesh out some pseudo code here so I get an understanding of what you were thinking of and how it would fit with the rest of Giraffe!

2mol commented 3 years ago

Hi Dustin, thanks for answering!

Interesting, I had a (fluffy) image of it being a simpler subset of websockets, since it just goes one way. But I'm now realizing that there are no websockets in Giraffe.

I agree with your approach of first sketching out pseudocode.

So, with the context that I'm not a super advanced F# programmer, I see a couple of possibilities:

I like the last option, since that way I won't feel like I wasted your time in case nothing comes of it. What do you think?