2
votes

I'm trying out some stuff with Akka Streams for one of my project where I already use Rx Scala. I was tempted to see how Akka Streams would be a fit to replace the Rx Scala library that we have. One thing what I do not see possible with Akka Streams is the possibility to have one Source and many Sinks. Say, in this example taken straight out from Akka Streams documentation:

val source = Source(1 to 10)
val sink = Sink.fold[Int, Int](0)(_ + _)

// connect the Source to the Sink, obtaining a RunnableGraph
val runnable: RunnableGraph[Future[Int]] = source.toMat(sink)(Keep.right) // how could I materialize to a Seq of Sinks?

// materialize the flow and get the value of the FoldSink
val sum: Future[Int] = runnable.run()

When using an Rx library, I have both the Source (Observable) and Sink (Observer) totally decoupled which gives me the flexibility to map 1 Source (Observable)and have n Sinks (Observers). How can I achieve this with the Akka Streams? Any pointers would be helpful!

1

1 Answers

2
votes

This is available with Graphs, specifically Broadcast:

Broadcast[T] – (1 input, N outputs) given an input element emits to each output

Some sample code from the documentation:

val in = Source(1 to 10)
val out = Sink.ignore

val bcast = builder.add(Broadcast[Int](2))
val merge = builder.add(Merge[Int](2))

val f1, f2, f3, f4 = Flow[Int].map(_ + 10)

in ~> f1 ~> bcast ~> f2 ~> merge ~> f3 ~> out
            bcast ~> f4 ~> merge
ClosedShape