aleph

henrik 2023-01-26T13:24:19.179389Z

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?

henrik 2023-01-30T14:26:49.465469Z

Thanks @arnaudgeiser, 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 2023-02-08T07:17:26.587759Z

@henrik 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.

henrik 2023-02-08T08:00:51.469909Z

@kingmob 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.

henrik 2023-01-27T08:03:19.683109Z

Hi @arnaudgeiser! 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 Geiser 2023-01-27T08:32:40.487049Z

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

🙏 1
Arnaud Geiser 2023-01-27T22:47:13.930579Z

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] : https://gist.github.com/arnaudgeiser/aec1efea5440160e61320e2aa9069b79

Arnaud Geiser 2023-01-27T22:50:53.254459Z

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....

Arnaud Geiser 2023-01-27T07:52:10.726929Z

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