Definition of Akka HTTP client-side websocket streams

Viewed 696

I've successfully used Akka Streams in the past, however, I'm currently having a hard time to understand why client-side Websocket Streams in Akka-HTTP are defined and work as it is shown in the documentation.

Since a WebSocket connection allows full-duplex communication, I'd expected that such a connection is represented by two separate streams in Akka HTTP, one for incoming traffic, one for outgoing traffic. And indeed, the documentation states the following:

A WebSocket consists of two streams of messages [...]

It further states that incoming messages are represented by a Sink and outgoing messages by a Source. And that's my first point of confusion - when using two separate streams, you'd expect having to deal with two sources and two sinks in total instead of just one of each kind. Currently, my guess is that the source of the incoming stream as well as the sink of the outgoing stream are not really of much use to the developer and therefore are just "hidden".

However, it really gets confusing when wiring everything together (see the docs linked above).

The part in question when using singleWebSocketRequest:

val flow: Flow[Message, Message, Future[Done]] =
      Flow.fromSinkAndSourceMat(printSink, helloSource)(Keep.left)

Or the same part when using webSocketClientFlow:

val (upgradeResponse, closed) =
      outgoing
        .viaMat(webSocketFlow)(Keep.right)
        .toMat(incoming)(Keep.both)
        .run()

This contradicts my current understanding of the workflow of streams.

  • Why would I want to combine the Source for outgoing messages and the Sink for incoming messages? The code above looks like I'm sending messages to myself and not to a server.
  • Furthermore, what are the semantics of Flow[Message, Message, ...]? Transforming outgoing messages to incoming messages doesn't seem to make sense.
  • Shouldn't there be two streams instead of one?

Any help in improving my understanding is appreciated, thanks.

edit:

I have no problems using the Source and Sink and sending data over the WebSocket, I just want to understand why the wiring of the stages is done like this.

1 Answers
Related