tapir icon indicating copy to clipboard operation
tapir copied to clipboard

SSE with a project having shared routes for jvm and scalajs

Open larochef opened this issue 5 months ago • 3 comments

Hello, I have a project using scalajs for the front and tapir with netty sync for the backend.

I'm currently wanting add some SSE endpoints and so had a look at the corresponding example at https://github.com/softwaremill/tapir/blob/master/examples/src/main/scala/sttp/tapir/examples/sse/sseNettySyncServer.scala

This example shows that we need, in the route definition, some classes coming from the server part, describing the streaming. However, this doesn't seem to make any sense for the scalajs client, which shouldn't know about netty or ox.

how would you recommend sharing the route definitions between back and front for SSE? Also, do you have some example code using a SSE client with tapir?

larochef avatar Jul 24 '25 07:07 larochef

You're right, streaming is what ties an endpoint description to a particular implementation (here it's Ox, but it also might be FS2/cats-effect/http4s, or ZIO, or Akka, or Pekko). So the question boils down to which stack you are using on the backend & frontend.

I think the only one that allows sharing streaming endpoint descriptions between server & JS client is cats-effect-based, using http4s. Maybe ZIO as well, through http4s+zio integration.

Otherwise, you'll need separate endpoint descriptions for both server & client, when it comes to streaming.

adamw avatar Jul 25 '25 08:07 adamw

Thx for the answer, this leaves me though with another question: how to handle sse with the FetchBackend? When I look at the code, it feels like streaming is disabled, how would it work? Since I use laminar, I'm considering writing some backend using signals instead if futures, extending the AbstractFetchBackend. So I could add streaming support to it if it's not too complex, if it's needed for SSE. Would you have any hints on what should be done and what I should beware of?

larochef avatar Jul 25 '25 08:07 larochef

I don't think you'll be able to handle SSE with a FetchBackend, as there's no support for any kind of streaming there. I think the only streaming backends for JS that are available are Monix (which I think at this point might be considered a legacy library) and ZIO (using ZioServerSentEvents.parse).

The same should be possible using FS2, but we don't have a FetchFs2Backend - should be doable I think, just nobody every got to doing it. Of course, PRs welcome :)

adamw avatar Jul 25 '25 08:07 adamw