I'm using Http4s to mount a websocket service that I can use to communicate between this backend service and a UI (piping status updates and completion % for a batch job).
I'm using the BlazeBuilder Websocket Example to set up the service.
The service works, but what I'm trying to do is emit socket messages from within a class instance. For example, I want to instantiate a worker, pass a reference for the socket connection, and be able to emit data into that connection. Unfortunately, I'm having a very hard time making this work! It's much simpler in Python and JS.
See the code below, which is mostly the example code I linked above. In the place I'm calling Stream.emit(...), how can I pass a reference to that "toClient" and still emit to it? If I pass the toClient instance into a class instance, it doesn't seem to work.
case GET -> Root / "ws" =>
val toClient: Stream[F, WebSocketFrame] = Stream.emit(Text("How can I do this from a class instance?"))
val fromClient: Sink[F, WebSocketFrame] = _.evalMap { (ws: WebSocketFrame) =>
ws match {
case Text(t, _) => F.delay(println(t))
case f => F.delay(println(s"Unknown type: $f"))
}
}
WebSocketBuilder[F].build(toClient, fromClient)