Server-sent events(SSE) is a powerful Web technology that allows to push data from the server directly to the browser.
Since Ring 1.6.0 thanks to the support for async handlers is possible to use SSE from your Clojure Ring/Compojure app.
A walking skeleton could be as simple as a background thread producing data pushed into a core.async channel that is pushed to the browser via SSE.
To enable the async handler we have to pass the new :async? flag to the adapter:
Two core building blocks are then needed to use SSE with Ring: the newly introduced StreamableResponseBody protocol and a new middleware handler arity.
We’re going to extend a core.async channel with StreamableResponseBody:
Everything that is pushed into the channel will be now pushed down to the browser.
The route handler is also returning the asynchronous handler(with 3 parameters):
Where stream-msg and stream-response are helper functions to create the correct SSE response headers.
The client side Javascript code is straightforward: