1

I'm currently working with Akka Streams (in Java) for a personal project and I'm having a hard time understanding how to send element to a Source.

The idea is to use a WebSocket to push content into the user's web browser. I've managed to use Akka Streams to create a request-response system, following the Akka HTTP documentation, but this is not what I want to do.

Looking into the Akka Streams documentation, I saw that there is Source.queue and Source.actorRef. But I don't understand how to put an element into the Source. Source.queue and Source.actorRef return a Source, which doesn't have the method offer (for Source.queue) or tell (for Source.actorRef).

My question is: how do I get the ActorRef for the Source created by Source.actorRef or the SourceQueueWithComplete for a Source created with Source.queue, to be able to send elements to my Source?

I searched the various Akka documentation but found no method to do that. And the majority of the code I found on the Internet is written in Scala, which doesn't seem to have the same problem.

Jeffrey Chung
  • 19,319
  • 8
  • 34
  • 54
Urires
  • 25
  • 6
  • Possible duplicate of [How to create a Source that can receive elements later via a method call?](https://stackoverflow.com/questions/30964824/how-to-create-a-source-that-can-receive-elements-later-via-a-method-call) – Ramón J Romero y Vigil Nov 16 '17 at 13:19

1 Answers1

2

The actor and queue from Source.actorRef and Source.queue, respectively, are the materialized values of those sources, meaning that they can be obtained only if the stream is running. For example:

final ActorRef actor =
  Source.actorRef(Integer.MAX_VALUE, OverflowStrategy.fail())
        .to(Sink.foreach(m -> System.out.println(m)))
        .run(materializer);

actor.tell("do something", ActorRef.noSender());

It's no different in Scala:

implicit val materializer = ActorMaterializer()

val actor =
  Source.actorRef(Int.MaxValue, OverflowStrategy.fail)
        .to(Sink.foreach(println))
        .run()

actor ! "do something"
Jeffrey Chung
  • 19,319
  • 8
  • 34
  • 54
  • As an addition to this answer, the relevant documentation on akka.io can be found here, which may not be completely obvious to find: https://doc.akka.io/docs/akka/2.5.3/scala/stream/stream-integrations.html – Antonio de Perio Mar 20 '18 at 23:36