Should actor B change the response message between C and A?
If not, actor B should use B forward "msg" instead of B ! "msg" B ! "msg" . This will save the sender information. When C uses the response, the response is automatically sent to without passing through B:
A sends message to B (b ! "msg")
B forward the message to C (c forward "msg")
C replies to A (self.reply ! "msg3")
If so, get the sender of ActorRef and pass it by the link of the sender of the message.
For example, using a simple tuple:
A sends message to B (b ! "msg1") B sends message to C, with the sender reference (c ! ("msg2",self.sender) ) C replies to B adding the reference it received (self.reply ! ("msg3",ref) ) B replies to A ( ref ! "msg4" )
I used Tuple here, but if you have a long chain, go List[ActorRef] . As you move forward, you add senders refs. And when you go back, you respond to the head of the list and pass the tail of the list by answer.
Edit: Considering Victor's comment.
source share