C#C
C#2y ago
Vortac

How to use Server Sent Events with HttpClient

I'm working with an API framework (https://docs.mistral.ai/api/#operation/createChatCompletion) in which you send a chat request. If you set the stream parameter to true:

tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message. Otherwise, the server will hold the request open until the timeout or until completion, with the response containing the full result as JSON.


To handle this, I created an SssEvent class with the following properties:

public string EventType { get; set; }
public string Data { get; set; }

I have the code working for non-streaming calls, but I'm confused as to how to send my initial message and then stream back the response. My code currently looks like this:

https://paste.mozilla.org/BD3gMX5o
Was this page helpful?