Skip to content

Conversation

jarppe
Copy link

@jarppe jarppe commented Sep 23, 2025

This PR introduces protocols for SSE support and a reference implementation for ring.adapter.jetty.

@weavejester
Copy link
Member

SSE are built on top of HTTP, so you can implement them in Ring by just writing to the response OutputStream.

(defn sse-handler [_request]
  {:status 200
   :headers {"Content-Type" "text/event-stream"}
   :body (reify ring.core.protocols/StreamableResponseBody
           (write-body-to-stream [_ _ out _]
             (let [writer (clojure.java.io/writer out)]
               (.write writer "event: example\ndata: 1\n\n")
               (.flush writer)
               (Thread/sleep 1000)
               (.write writer "event: example\ndata: 2\n\n")
               (.flush writer))))

There's no need to add an additional protocol or add to the specification, as SSEs are already supported by virtue of them being HTTP. If you need asynchronous support, then you can use the 3-arity asynchronous Ring handlers.

If you want a nicer interface, then you can implement that via a middleware wrapper or a function that generates the response (as the datastar-clojure library does).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants