-
Hello, Though I was not able to figure out how to implement SSE. What I need is to get a response stream and keep the connection open while the client is connected so that I can keep posting data to that open stream. I think returning Stream as the response won't do it as peeking to your implementation it only reads all content while the stream Read function returns any data and then it stops, so I does not wait for the stream to be completed - or does not expect the stream producer to be async. Could you please advise me on how to implement this, if at all possible? Is there a way to get better control over the actual response and get handle of the output stream that I can keep alive and regularly post data into? (For reference here is SSE spec: https://html.spec.whatwg.org/multipage/server-sent-events.html ; note if you don't have any familiarity with it, it is a one-way stream from server to client, not to be confused with websockets) Thank you in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 5 replies
-
Ok after a day of struggling I think I have figured out the missing piece. I implemented this response content: public class PushStreamContent : IResponseContent
{
private readonly Func<Stream, uint, Task> _writer;
public PushStreamContent(Func<Stream, uint, Task> writer)
{
_writer = writer;
}
public ulong? Length => null;
public ValueTask<ulong?> CalculateChecksumAsync() => ValueTask.FromResult<ulong?>(null);
public async ValueTask WriteAsync(Stream target, uint bufferSize)
{
await _writer(target, bufferSize);
}
} Example usage: [ResourceMethod(path: "test")]
public IResponseBuilder Test(IRequest request)
{
return request
.Respond()
.Type(new FlexibleContentType("text/event-stream"))
.Content(new PushStreamContent(async (stream, bufferSize) =>
{
using var writer = new StreamWriter(stream, System.Text.Encoding.UTF8, (int)bufferSize);
var i = 0;
while (true)
{
await writer.WriteLineAsync("data: " + (++i));
await writer.WriteLineAsync();
await writer.FlushAsync();
await Task.Delay(1000);
}
}));
} Still let me know if this is a proper way. Thanks. 😄 |
Beta Was this translation helpful? Give feedback.
-
Sorry for the late reply - we had our daughter in May, so mail notifications like this can get lost in the shuffle. Thanks for the praise too, I'm always happy when someone appreciates the approach. Your implementation is perfectly fine. Within a
If you would like to add your functionality (e.g. to the |
Beta Was this translation helpful? Give feedback.
-
I know it's a old thead but the stream is currently closed by an IO exception. This async task doesn't automatically close when you start adding more logic. (eg. only write when there is data) |
Beta Was this translation helpful? Give feedback.
-
@Matasx, @Fiahblade: Support for Server Sent Events will be available in GenHTTP 9, see https://github.com/Kaliumhexacyanoferrat/GenHTTP.Website/blob/master/content/documentation/content/handlers/server-sent-events.md |
Beta Was this translation helpful? Give feedback.
Ok after a day of struggling I think I have figured out the missing piece.
I implemented this response content:
Example usage: