class
Kemal::EventStream
- Kemal::EventStream
- Reference
- Object
Overview
Helper for Server-Sent Events (SSE) responses.
Sets the required headers and formats events according to the SSE spec.
Defined in:
kemal/event_stream.crConstructors
Class Method Summary
-
.serve(context : HTTP::Server::Context, & : EventStream, HTTP::Server::Context -> )
Runs an SSE handler with headers configured and yields an
EventStream.
Instance Method Summary
- #close : Nil
-
#comment(text : String) : self
Sends a keep-alive comment (ignored by clients, useful during idle periods).
- #flush : Nil
-
#send(data : String, *, event : String | Nil = nil, id : String | Int | Nil = nil, retry : Time::Span | Nil = nil) : self
Sends an SSE event.
Constructor Detail
Class Method Detail
Runs an SSE handler with headers configured and yields an EventStream.
A HEAD request gets the headers of the stream and no stream. The handler is
typically an endless loop that only stops when a write fails because the
client is gone, and on HEAD nothing it writes goes anywhere - the body is
discarded and counted - so the loop would never see the client leave and the
fiber would run for the life of the process.
Instance Method Detail
Sends a keep-alive comment (ignored by clients, useful during idle periods).
Sends an SSE event. Multi-line data is split into separate data: fields.
event and id must not contain CR/LF; newlines there raise ArgumentError
so they cannot inject SSE fields. A negative retry span is omitted from the
output, since clients discard non-digit retry values anyway.