C
C#11mo 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.
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; }
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
4 Replies
The Fog from Human Resources
I'm not too sure of what you mean with server sent events but do you perhaps mean websockets?
Vortac
VortacOP11mo ago
Server-sent events
Server-Sent Events (SSE) is a server push technology enabling a client to receive automatic updates from a server via an HTTP connection, and describes how servers can initiate data transmission towards clients once an initial client connection has been established. They are commonly used to send message updates or continuous data streams to a b...
lycian
lycian11mo ago
In the wikipedia it looks like SignalR would be SSE compliant , or you could use https://docs.servicestack.net/server-events if you want to only use HttpClient, I'm guessing you'd have to end up doing a GetStreamAsync and handle that input stream
Vortac
VortacOP11mo ago
Ideally using only httpclient as I’m writing a library and would like to avoid external dependencies if possible
Want results from more Discord servers?
Add your server