Skip to content

Instantly share code, notes, and snippets.

@ohpauleez
Last active August 29, 2015 14:03
Show Gist options
  • Save ohpauleez/4ac4fb78e6a3f0f255bb to your computer and use it in GitHub Desktop.
Save ohpauleez/4ac4fb78e6a3f0f255bb to your computer and use it in GitHub Desktop.
Pedestal 0.3.0 example with SSE
;; In Pedestal 0.3.1, you won't have to use an atom.
;; You'll pass the channel into the SSE creation function (instead of it passing one back to you)
(def event-ch (atom nil))
(defn home-page
[request]
(ring-resp/response "Hello World!"))
(defroutes routes
[[["/" {:get home-page}
;; Set default route-oriented interceptors
^:interceptors [(body-params/body-params)
bootstrap/html-body
timing-interceptor]
["/about" {:get about-page}]
["/user/:user" {:get greet-user}]
["/echo" {:get echo-page}]
;; If you dig way down in the Pedestal code, you'll find that the new SSE
;; sets the context's body to a channel, which you can then use to control/interact with events
["/events" {:get [::events (sse/start-event-stream (fn [{:keys [body] :as context}]
(reset! event-ch body)))]}]
["/json" ^:interceptors [bootstrap/json-body]
["/hello" {:get [::json-hello (fn [req]
(ring-resp/response {:response "Hello, World"}))]}]]]]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment