tide icon indicating copy to clipboard operation
tide copied to clipboard

Conditional Server Sent Events

Open gregcline opened this issue 4 years ago • 1 comments

Question

Is it possible to maintain some state in an SseEndpoint and use it to conditionally send events?

Additional context

In a side project I need to watch a file on the server and send an update to the client when the file changes.

Ideally the server would check the file periodically and only send an event if the file changed, but I can't figure out how to keep track of the previous state of the file in the SSE endpoint. I tried reading the implementation of the SseEndpoint but wasn't able to understand it well enough to figure out if what I'd like to do is possible. As a result, right now the client is receiving events and checking if a change occurred.

If it is possible I would be happy to contribute some documentation describing that use case.

gregcline avatar Aug 11 '20 15:08 gregcline

I think you'd probably want to spawn a task with async_std::task::spawn that watches the file, put a cloneable channel of some sort into your app state (maybe a broadcaster, depending on your fan-out needs), move a clone of that into your sse handler, and communicate that way. It's not particularly ergonomic currently, but the longer-term goal is tide channels

jbr avatar Aug 11 '20 16:08 jbr