| | | 1 | | using System.Threading.Channels; |
| | | 2 | | |
| | | 3 | | namespace Kestrun.Sse; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides a broadcast-style Server-Sent Events (SSE) publisher. |
| | | 7 | | /// </summary> |
| | | 8 | | public interface ISseBroadcaster |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Gets the number of currently connected SSE clients. |
| | | 12 | | /// </summary> |
| | | 13 | | int ConnectedCount { get; } |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Subscribes a client to broadcast SSE events. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="cancellationToken">Cancellation token used to disconnect the client.</param> |
| | | 19 | | /// <returns>A subscription containing the client ID and a channel reader for SSE payloads.</returns> |
| | | 20 | | SseClientSubscription Subscribe(CancellationToken cancellationToken); |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Broadcasts an SSE event to all connected clients. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="eventName">The event name (optional).</param> |
| | | 26 | | /// <param name="data">The event payload.</param> |
| | | 27 | | /// <param name="id">Optional event ID.</param> |
| | | 28 | | /// <param name="retryMs">Optional client reconnect interval in milliseconds.</param> |
| | | 29 | | /// <param name="cancellationToken">Cancellation token.</param> |
| | | 30 | | ValueTask BroadcastAsync(string? eventName, string data, string? id = null, int? retryMs = null, CancellationToken c |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Represents a connected SSE client subscription. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="ClientId">Unique client identifier.</param> |
| | | 37 | | /// <param name="Reader">Channel reader producing formatted SSE payloads.</param> |
| | 9 | 38 | | public readonly record struct SseClientSubscription(string ClientId, ChannelReader<string> Reader); |