Clojure server-sent events Event Source client
EPL-1.0 License
A Clojure server-sent events event stream client. Provides a core.async based interface for consuming events.
cjohansen/clj-event-source {:mvn/version "2019.11.30"}
This library is implemented on top of java.net.http.HttpClient
, which requires
JDK 11.
clj-event-source
allows you to consume events from an event stream over a
core.async
channel. The vals put on the channel will be maps with:
:kind
- a keyword:content
- depends on the :kind
, see below:kind
will take one of four values:
:meta
- Process information from clj-event-source. Basically a log message.:error
- An error occurred. :content
will be an error object:message
- A message without the event
field was received over the event:content
will be an event.:event
- An event was received over the event stream. :content
will be an(let [event-stream (event-source/connect "http://localhost:9119/event-stream")]
(go-loop []
(if-let [msg (<! event-stream)]
(do
(case (:kind msg)
:error (println "Encountered error" (:content msg))
:message (let [{:keys [id data]} (:content msg)]
(println "Message without event" id data))
:event (let [{:keys [id event data]} (:content msg)]
(println "Message with event" id event data)))
(recur))
(println "Event source disconnected")))
(Thread/sleep 60000)
(close! event-stream))
If you want to control the buffer size etc, you can supply your own chan
when
connecting:
(event-source/connect "http://localhost:9119/event-stream" {:chan my-chan})
The event stream event is a map:
:data
- a string:id
- a string:event
- a stringThe map will never be empty, but it may have any combination of 1, 2, or all of these keys.
The library emits meta events instead of logging directly. Meta events always
have an :event
key in :content
:
:retry
means the connection was lost and a retry is about to be attempted.:retry-attempts
, the number of retries tried so:retry-after
, which indicates for how long it will:no-connection
means the connection was lost an retries are exhausted - the:retry-attempts
, the number of retriesclj-event-source.core.async/connect
takes an optional second parameter - a map
of parameters:
:chan
- the channel to use for relaying messages:retry-attempts
- number of times to retry a failed connection. Defaults to:retry-delay
- the number of milliseconds to wait between retries.[1000 2000 3000]
which means wait 1 second before the first retry, 2 seconds before theCopyright © 2019 Christian Johansen
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.