Search code examples
akkaakka-streamakka.net

Push data to stream


Is it possible to create a Source to which I'm going to be able to push data "manually" (or I can do it somehow to a "regular" Source)?

Something like:

var source = Source.Empty<int>();
source.Push(10); //is something like this possible?

My use case would be creating a source to which I'm able to push data whenever my API endpoint is called.


Solution

  • Yes, it's possible. Check out Source.Queue:

    Source.Queue can be used for emitting elements to a stream from an actor (or from anything running outside the stream). The elements will be buffered until the stream can process them. You can Offer elements to the queue and they will be emitted to the stream if there is demand from downstream, otherwise they will be buffered until request for demand is received.

    Another option is Source.ActorRef:

    Messages sent to the actor that is materialized by Source.ActorRef will be emitted to the stream if there is demand from downstream, otherwise they will be buffered until request for demand is received.

    Unlike Source.Queue, Source.ActorRef does not support backpressure.