Consume a stream without blocking

Hello !

I use reqwest create to consume an SSE http response. SSE send "event" over http response without closing it. Like a unidirectional websocket, but as more simple.

To read this SSE response, I use the Response.bytes_stream method which give me a Stream.

Then I use next to get new response lines :

while let Some(thing) = stream.next().await {
  // [...]

But stream.next().await is blocking until new lines are sent. I need to do some things if no message received since 60 seconds. How can I do read the stream with a timeout or without blocking ?

Thanks !

You can use tools like tokio::select! or tokio::time::timeout to do this.

I will try this !

alice, you seriously deserve a medal for all the help you provide on this forum !

1 Like