Search code examples
kotlinactorkotlinx.coroutines

Kotlin actor to actor communication


An actor can be sent a message with actor.send() or actor.offer() Once my actor gets the message from its channel I'd like to return a response. How do I do this? I don't see any obvious built in method.


Solution

  • You need to request the Actor’s channel, as documented:

    [The actor coroutine builder] Launches new coroutine that is receiving messages from its mailbox channel and returns a reference to the coroutine as an ActorJob. The resulting object can be used to send messages to this coroutine.

    Here’s a simple example.