Last active
January 27, 2023 22:44
-
-
Save arnaudgeiser/aec1efea5440160e61320e2aa9069b79 to your computer and use it in GitHub Desktop.
Server Sent Events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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