Fork me on GitHub

I’m using the HTTP client to connect to a server that sends back server-sent events. Is there a way to receive a stream of these for consumption, rather than a deferred with the batched result?

Arnaud Geiser07:01:10

Hello Henrik! Sorry for the delay, can you share the code with us so we can have a look?


Hi @UFKCFTVB8! Sure, thank you! It’s not much to look at:

(aleph-http/post endpoint
  {:body    (json/write-str (assoc default-parameters :stream true))
   :headers {"Content-Type"  "application/json"
             "Authorization" (str "Bearer " (api-key))}})
:stream true tells the endpoint to stream back responses rather than send a single chunk, which is something I’d like to make use of as I can then stream them to the user. Being that it returns a deferred, I get the responses batched at the end of the request, however. So it’d be great to be able to consume the response stream as it happens. I’ve mucked around with
(aleph-http/post endpoint
  {:pool    (aleph-http/connection-pool {:connection-options {:raw-stream? true}})
   :body    (json/write-str (assoc default-parameters :stream true))
   :headers {"Content-Type"  "application/json"
             "Authorization" (str "Bearer " (api-key))}})
But I don’t really know what I’m doing here.

Arnaud Geiser08:01:40

Thanks! I don't have time to have a look now, I will do it this evening (CET timezone).

🙏 2
Arnaud Geiser22:01:13

Here is a really dumb Aleph SSE server/client. [1] What http/get returns is a deferred that returns an InputStream, not the aggregated data. As you can see, the data is printed as it comes and not in an aggregated way. [1] :

Arnaud Geiser22:01:53

To be honest, I'm not aware of any "good" client side SSE library for Clojure. That's unfortunate. But depending what you are interested in from your server, it might be just applying a filtering on data: which might be good enough....


Thanks @UFKCFTVB8, I appreciate the answer nevertheless. I thought there might be a solution for this native to Aleph, since stream constructs are native to it.

Matthew Davidson (kingmob)07:02:26

@U06B8J0AJ Yeah, SSE is pretty old, and not well-supported outside of a browser. Aleph's minimal-level stream support is the basic InputStream, and that's the best you'll get for SSE. I'm sure you know this, but server-to-server SSE is pretty unusual. I get that you might be constrained if the other server is outside your control, but if they support WebSockets, that will be 100x easier.


@U10EC98F5 I’m talking to OpenAI, which offers this. Honestly, I need to scrutinize the API docs a bit further, but so far the only alternative for getting streamed responses I’ve found is SSE.