0

I am new to akka-stream and I am trying to use the Framing.lengthField with websocket https://github.com/akka/akka/blob/master/akka-stream/src/main/scala/akka/stream/scaladsl/Framing.scala

Here is what I have:

path("websocket") {
    handleWebSocketMessages(websocketFlow)
} 

def websocketFlow(): Flow[Message, Message, Any] =
Flow.fromGraph(
  GraphDSL.create(Source.actorRef[TestMessage](bufferSize = 10, OverflowStrategy.fail)) {
    implicit builder =>
      source =>

        val binaryMessageDecoder = Framing.lengthField(4, 0, 1024 * 32, ByteOrder.BIG_ENDIAN)

        val byteStringToMessage = Flow[ByteString].map { bytes =>
          TestMessage.parseFrom(bytes.toArray[Byte])
        }

        val fromWebsocket = builder.add(
          Flow[Message].map {
            case bm: BinaryMessage =>
              bm.dataStream
                .via(binaryMessageDecoder)
                .via(byteStringToMessage)
          }
        )

        val backToWebsocket = builder.add(
          Flow[TestMessage].map {
            case TestMessage(text) =>
              TextMessage(text)
          }
        )

        val actorSink = Sink.actorRef[TestMessage](testActor, PoisonPill)

        import GraphDSL.Implicits._

        fromWebsocket ~> actorSink // This line does not compile

        source ~> backToWebsocket

        FlowShape(fromWebsocket.in, backToWebsocket.out)
  }
)

I do not understand the issue here. How can I make this work?

ydemartino
  • 242
  • 5
  • 10

1 Answers1

1

Your fromWebsocket flow returns streams, not messages, and your actorSink takes messages.

If you want it to compile you have to make sure it returns messages, for example you can change map to flatMapConcat. This will make sure that the messages returned from the streams are concatenated in a new stream of messages.

lpiepiora
  • 13,659
  • 1
  • 35
  • 47