Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement full duplex protocol using websocket? #3248

Open
andrey-ilinykh opened this issue Dec 11, 2024 · 0 comments
Open

How to implement full duplex protocol using websocket? #3248

andrey-ilinykh opened this issue Dec 11, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@andrey-ilinykh
Copy link

Describe the bug
Not sure this is a bug, but I can't find a good way to implement full duplex protocol using websocket. Examples I see are half duplex - request/response.
Let's say I want to stream some random data to a client (stock price for example). On the top of it I need regular request/response protocol.

val myStream : ZStream[Any, Throwable, String] = ??? //source of some random data
val socketApp: WebSocketApp[Any] =
    Handler.webSocket {
      channel =>
        channel.receiveAll {
          case ChannelEvent.Read(WebSocketFrame.Binary(bytes)) =>
             decode(bytes) match {
               case MyCustomCommand() =>
                  channel.send(Read(WebSocketFrame.binary(myResponse)))
               case Subscribe() =>
                 // I have to start streaming random data
                 myStream.run(ZSink.foreach(x => channel.send(Read.WebSocketFrame.binary(encode(x))))
               //after this message I never get MyCustomCommand(), which makes sense because myStream is never ending
             }
        }
    }

I can fork

  myStream.run(ZSink.foreach(x => channel.send(Read.WebSocketFrame.binary(encode(x)))).fork

It seems to be working, but I'm not sure if it is the right way to do. The resource leak is my concern.

@andrey-ilinykh andrey-ilinykh added the bug Something isn't working label Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant