fiadliel / fs2-grpc-example

Example implementation for fs2-grpc library
MIT License
7 stars 8 forks source link

Streaming examples? #2

Open simplesteph opened 5 years ago

simplesteph commented 5 years ago

Would be great to get client streaming, server streaming and bi-di streaming examples if possible? Including how to properly handle cancellation!

SemanticBeeng commented 5 years ago

In this example the server prints both messages but client only prints the first.

https://github.com/SemanticBeeng/fs2-grpc-example/blob/7e8731ff04811f3606f6801eebee3efdba6f9f0e/client/src/main/scala/Main.scala#L27-L28

@fiadliel please advise if

  1. there is a way to write the client to get both server replies using the generated stub.

  2. Will it work using java_runtime.client.Fs2ClientCall and the generated server?

  3. Is there a way (with the generated client) to get incremental replies for same call? Server writes some replies, client gets them, server continues with some more replies, clients gets them too, etc. In other words the lazy evaluation stream semantics is respected (as opposed to a list).

Note: used a version of the sbt-java-gen plugin I custom built with special versions of some dependencies but did not change anything else (0.3.0 will work just as well).

phiSgr commented 3 years ago

In the for-comprehension:

  _ <- runProgramStream(helloStub)
} yield StreamApp.ExitCode.Success

You are running Stream#flatMap, and yielded the exit code on first reply.


Your server implementation is also confusing, if not buggy.

_ ← sayHello(req, clientHeaders)
_ ← IO(println(s"Pausing a little ..."))
_ ← IO(println(s"Responding to lady $req again"))
_ ← timer.sleep(1 second)
r ← sayHello(req, clientHeaders)

All the first sayHello does is to calculate a response, which you discarded. If you want multiple reply for one incoming message, use flatMap and construct your multiple response stream instead of using evalMap.