0

I want to stream data from Zstream with some repeat time. I have my main function which returns ZIO:

def processData(request: MyRequest): Task[Seq[SomePayload]]

I also call this method for every element in my list requests.

Now I would like to take this output and stream jsons I have in Seq[SomePayload]. I created a stream with endpoint:

  val streamingServerEndpoint: ZServerEndpoint[Any, ZioStreams] = streamingEndpoint.zServerLogic { _ =>
    val stream  =
      ZStream.fromZIO(ZIO.collectAll(requests.collect(service.processData(_))).repeat(Schedule.minuteOfHour(30))).map(_.toByte)

    ZIO.succeed((100L, stream))
  }

Endpoint:

  val streamingEndpoint: PublicEndpoint[Unit, Unit, (Long, Stream[Throwable, Byte]), ZioStreams] =
    endpoint.get
      .in("receive")
      .out(header[Long](HeaderNames.ContentLength))
      .out(streamTextBody(ZioStreams)(CodecFormat.TextPlain(), Some(StandardCharsets.UTF_8)))

But when I run this code, I can make a request to /receive endpoint but nothing is streamed in output. In logs I see processData method works and it returns data, but nothing on endpoint. How should I change this code to stream jsons (or jsons as bytes) on API? I want also to run processData for all requests, every hour (minut of 30 every hour).

Gaël J
  • 11,274
  • 4
  • 17
  • 32
Developus
  • 1,400
  • 2
  • 14
  • 50
  • Not sure if related but Content-Length should be computed by the server or actually left empty for infinite streaming. Also your scheduling of 30 minutes might be a typo? – Gaël J Mar 15 '23 at 21:07
  • I want to repeat my `processData` every hour and stream body to output – Developus Mar 15 '23 at 21:09
  • Can you also show output of `curl -v` maybe? – Gaël J Mar 15 '23 at 21:29
  • Note that of you don't send any data for long period (several minutes), clients will likely close the HTTP connection. – Gaël J Mar 15 '23 at 21:30
  • It's nothing. Postman shows "sending request" and return no response. In app logs I see output from `processData ` method, so I think it is returned, but not on endpoint. – Developus Mar 15 '23 at 21:30
  • 1
    I'd also remove the content-length. This might confuse the server. Which interpreter are you using? – adamw Mar 16 '23 at 08:10
  • `curl -v` would give interesting details like responde headers. Please show us that. – Gaël J Mar 16 '23 at 12:31

0 Answers0