Skip to content

Instantly share code, notes, and snippets.

@arnaudgeiser
Last active January 27, 2023 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnaudgeiser/aec1efea5440160e61320e2aa9069b79 to your computer and use it in GitHub Desktop.
Save arnaudgeiser/aec1efea5440160e61320e2aa9069b79 to your computer and use it in GitHub Desktop.
Server Sent Events
(defn- streaming-handler
[_]
(let [body (s/stream)]
(future
(dotimes [i 10]
(s/put! body (format "data: %s \n\n" i))
(Thread/sleep 200))
(s/close! body))
{:status 200
:headers {"Content-Type" "text/event-stream"}
:body body}))
(def server (http/start-server #'streaming-handler {:port 9999}))
(let [{:keys [body]} @(http/get "http://localhost:9999")
rdr (clojure.java.io/reader body)]
(doseq [l (line-seq rdr)]
(prn l)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment