Websocket, ActorFlow.actorRef vs Flow.fromSinkAndSourceMat with SourceQueue

The Play for Scala docs for Websocket suggest that actors are usually the most straightforward way to do it. However, for some students who I’ve taught Scala (including actors but only more briefly), I’m tempted to suggest using a SourceQueue and Flow.fromSinkAndSourceMat – that way they don’t need to remember / feel confident with the concept of an actor but just have an asynchronous queue they can offer messages out on, and Sink.foreach to describe how to handle the incoming messages.

(Flow.fromSinkAndSourceMat because you need to get a reference to the materialised SourceQueue so you can retain it and push to it later)

I just quickly wanted to post and sanity check whether there’s any obvious grave problem with this approach – whether it’d lead students up a garden path because I’m forgetting something, lead them into a terrible habit, or I’m just going domestically blind and have forgotten something simpler?


This sounds like sensible advice to me! As briefly mentioned in https://doc.akka.io/docs/akka/current/stream/stream-integrations.html#source-actorref Source.queue is slightly more powerful since adding things to the queue returns a value describing whether this was successful.

For turning your source and sink into a Flow, depending on your use case you might consider using the ‘Coupled’ version of fromSinkAndSource: https://doc.akka.io/docs/akka/current/stream/operators/Flow/fromSinkAndSourceCoupled.html#flow-fromsinkandsourcecoupled