HADotNet icon indicating copy to clipboard operation
HADotNet copied to clipboard

Read /api/stream to receive server events

Open fabiolima1 opened this issue 4 years ago • 3 comments

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

fabiolima1 avatar Apr 14 '20 15:04 fabiolima1

This would be really cool, but my understanding is that this can only be implemented through websockets, not through the RestSharp client

Devqon avatar Feb 02 '21 09:02 Devqon

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.

qJake avatar Feb 02 '21 16:02 qJake

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 🤔

RafaelEstevamReis avatar Apr 15 '22 14:04 RafaelEstevamReis