What is the purpose of the Adapted Response?

Hi all

I am trying to understand the purpose of the Adapted Response from the doc.

I can not see the purpose of it, could someone provide a simple example. Preferably a project, that I can run.


It’s for when actor A which accepts messages of type A.Command wants to do a request-response with actor B.

The request will look like this B.Request(replyTo: ActorRef[B.Response]).

In actor A, a response adapter provides a way to given a function B.Response => A.Command get an ActorRef[B.Response] to put in the request that B can respond to. The transformed response will then end up as a normal message to A.

I hope this together with the code sample in the docs you linked to help understand why it is needed!


@johanandren Thanks a lot for your answer.

Why not just only

B.Request(replyTo: ActorRef[A.Command]) ?


That would mean that actor B would have to know that it will interact with actor A, it would also limit it to only interact with actor A and would need additional messages for a new actor C to be able to communicate with it.

You could compare it to protocols in general, where for example a mail server in the best scenario does not know of each of the clients mail applications that can connect but has a clearly defined set of requests and responses it will use.

In a few cases it could make sense to share messages (if the two actors are already tightly coupled and can rather be seen as one component, where no other actor from the “outside” will interact with B for example) but in general you will want the loose coupling of and self-containment that you get from having all messages that an actor accepts and responds with “owned” by the actor itself.

1 Like