fsprojects / FSharp.Data.GraphQL

FSharp implementation of Facebook GraphQL query language.
http://fsprojects.github.io/FSharp.Data.GraphQL/
MIT License
395 stars 72 forks source link

Support subscriptions using SSE (Server-Sent Events) as alternative to websockets #476

Open cmeeren opened 2 months ago

cmeeren commented 2 months ago

I propose that support is added for the GraphQL over Server-Sent Events Protocol, like HotChocolate has.

This simplifies authentication, since you can use the same authentication as normal HTTP requests.

I haven't looked at the GraphQL-specific parts of it, but SSE it itself technically very simple. Here is a demo:

Get(
    "/sse-test",
    RequestDelegate(fun (ctx: HttpContext) ->
        task {
            ctx.Response.Headers.Add("Content-Type", "text/event-stream")

            while not ctx.RequestAborted.IsCancellationRequested do
                do!
                    ctx.Response.WriteAsync(
                        $"data: {DateTime.Now}"
                    )

                do! ctx.Response.WriteAsync("\n\n")
                do! ctx.Response.Body.FlushAsync()
                do! Async.Sleep(1000)
        }
    )
)

You can test it by simply opening that endpoint in a browser (at least Edge works fine).

Related: #460

xperiandri commented 2 months ago

Good idea, why not!