(->sse-response ring-request opts)
Make a Ring like response that will start a SSE stream.
The status code and the the SSE specific headers are sent automatically
before on-open
is called.
Note that the SSE connection stays opened util you close it.
General options:
:status
: status for the HTTP response, defaults to 200.:headers
: ring headers map to add to the response.on-open
: mandatory callback called when the generator is ready to send.on-close
: callback called when the underlying Http-kit AsyncChannel is
closed. It receives a second argument, the :status-code
value we get from
the closing AsyncChannel.on-exception
: callback called when sending a SSE event throws.write-profile
: write profile for the connection.
Defaults to basic-profile
SDK provided write profiles:
You can also take a look at the starfederation.datastar.clojure.adapter.common
namespace if you want to write your own profiles.
Make a Ring like response that will start a SSE stream. The status code and the the SSE specific headers are sent automatically before [[on-open]] is called. Note that the SSE connection stays opened util you close it. General options: - `:status`: status for the HTTP response, defaults to 200. - `:headers`: ring headers map to add to the response. - [[on-open]]: mandatory callback called when the generator is ready to send. - [[on-close]]: callback called when the underlying Http-kit AsyncChannel is closed. It receives a second argument, the `:status-code` value we get from the closing AsyncChannel. - [[on-exception]]: callback called when sending a SSE event throws. - [[write-profile]]: write profile for the connection. Defaults to [[basic-profile]] SDK provided write profiles: - [[basic-profile]] - [[buffered-writer-profile]] - [[gzip-profile]] - [[gzip-buffered-writer-profile]] You can also take a look at the `starfederation.datastar.clojure.adapter.common` namespace if you want to write your own profiles.
Basic write profile using temporary [[StringBuilder]]s, no output stream and no compression.
Basic write profile using temporary [[StringBuilder]]s, no output stream and no compression.
Write profile using a permanent [[BufferedWriter]] and no compression.
Write profile using a permanent [[BufferedWriter]] and no compression.
Default on-exception
callback, it returns true
on [[IOException]] which
closes, the generator. It rethrows the exception wrapped with ex-infor
otherwise.
Default [[on-exception]] callback, it returns `true` on [[IOException]] which closes, the generator. It rethrows the exception wrapped with `ex-infor` otherwise.
Write profile using a permanent [[BufferedWriter]] and gzip compression.
Write profile using a permanent [[BufferedWriter]] and gzip compression.
Write profile using temporary [[StringBuilder]]s and gzip compression.
Write profile using temporary [[StringBuilder]]s and gzip compression.
SSE option key:
Callback (fn [sse-gen & args] ...)
called just after the SSE connection is
closed. More specifically this callback is called when:
When this callback is called it is already not possible to send events anymore. It is called synchronously in the same thread that initiates the closing process.
Args:
sse-gen
: the closed SSE generatorReturn value: The return value isn't used.
Exception behavior:
While closing the sse-gen
several exceptions may be caught:
These exceptions are rethrown wrapped in an ex-info
. To get at these see
[[get-closing-exceptions]].
SSE option key: Callback `(fn [sse-gen & args] ...)` called just after the SSE connection is closed. More specifically this callback is called when: - [[starfederation.datastar.clojure.api/close-sse!]] is called explicitely - the sse-gen detects that the connection is closed by the client. This detection depends on the ring adapter being used. Http-kit AsyncChannel detects a broken connection by itself and calls on-close. With the ring generic adapter, the default behavior is to close the connection when a [[java.io.IOException]] is thrown sending an event. When this callback is called it is already not possible to send events anymore. It is called synchronously in the same thread that initiates the closing process. Args: - `sse-gen`: the closed SSE generator - Depending on the adapter used other arguments may be passed, see doc for the adapter you are using. Return value: The return value isn't used. Exception behavior: While closing the `sse-gen` several exceptions may be caught: - one while closing the underlying output stream - one executing this callback These exceptions are rethrown wrapped in an `ex-info`. To get at these see [[get-closing-exceptions]].
SSE option key:
Callback (fn [sse-gen e ctx] ...)
that will be called when an exception is
thrown sending an event.
Args:
sse-gen
: the SSEGenratore
: the exceptionctx
: context information about the exception, a map whose keys are:
:sse-gen
: the sse generator throwing:event-type
: type of the event that failed:data-lines
: data lines for this event:opts
: options used when sendingReturned value: A truthy value means the sse-gen must close itself.
Exception behavior: Exception thrown here aren't caught.
Defaults to default-on-exception
which will close the sse-gen
on
IOException or rethrow otherwise.
SSE option key: Callback `(fn [sse-gen e ctx] ...)` that will be called when an exception is thrown sending an event. Args: - `sse-gen`: the SSEGenrator - `e`: the exception - `ctx`: context information about the exception, a map whose keys are: - `:sse-gen`: the sse generator throwing - `:event-type`: type of the event that failed - `:data-lines`: data lines for this event - `:opts`: options used when sending Returned value: A truthy value means the sse-gen must close itself. Exception behavior: Exception thrown here aren't caught. Defaults to [[default-on-exception]] which will close the `sse-gen` on IOException or rethrow otherwise.
SSE option key:
Mandatory callback (fn [sse-gen] ...)
called when the SSE connection is
open.
It is called synchronously as soon as the connection is ready. This means that you can block the webserver's thread from here.
Args:
sse-gen
: the SSE generator, ready to send eventsReturn value: The return value isn't used.
Exception behavior: Exception thrown here aren't caught.
SSE option key: Mandatory callback `(fn [sse-gen] ...)` called when the SSE connection is open. It is called synchronously as soon as the connection is ready. This means that you can block the webserver's thread from here. Args: - `sse-gen`: the SSE generator, ready to send events Return value: The return value isn't used. Exception behavior: Exception thrown here aren't caught.
SSE option key:
This option allows a user to control the way SSE events are assembled and sent. It take a write profile which is a map whose keys are:
SSE option key: This option allows a user to control the way SSE events are assembled and sent. It take a write profile which is a map whose keys are: - [[wrap-output-stream]] - [[write!]] - [[content-encoding]] (optional if you don't use compression)
cljdoc builds & hosts documentation for Clojure/Script libraries
Ctrl+k | Jump to recent docs |
← | Move to previous article |
→ | Move to next article |
Ctrl+/ | Jump to the search field |