HADotNet
HADotNet copied to clipboard
Read /api/stream to receive server events
New Feature Description
It would be great to be able to receive server sent events form the endpoint /api/stream and call a callback function when a event is received.
Code Example
var ServerEventClient = ClientFactory.GetClient<ServerEventClient>();
ServerEventClient.Listen(ServerEventCallback());
HA API Docs or Sample
https://developers.home-assistant.io/docs/external_api_server_sent_events
This would be really cool, but my understanding is that this can only be implemented through websockets, not through the RestSharp
client
Sort of, @Devqon... my understanding is that Home Assistant also supports Server-sent Events (SSE), and so we would need to either build out a custom HTTP client implementation that could listen for SSEs, or find a C# library specifically designed for them.
One example: https://makolyte.com/event-driven-dotnet-how-to-consume-an-sse-endpoint-with-httpclient/
Great care must be taken for this implementation, because you are effectively opening a connection and then keeping it open, which means handling opens/closes/reconnects as well as some possible threading issues. Definitely not a trivial task.
Can this be worked around if implemented as a Asp component? This is not what was asked, but some event is better than no event 🤔