Search code examples
scalaakka

Scala Akka | How to use the allButSelf property inside an Cluster?


Based on the Akka documentation for Cluster, would I like to Publish a Broad message to all nodes in the Cluster except myself. Currently it's always sending the Broad message to me as well.

val mediator = DistributedPubSub(context.system).mediator
mediator ! Subscribe("content", self)       // subscribe to the topic named "content"
mediator ! Publish("content", "msg")        // sends the msg out Broad to all nodes including myself

How exactly can I set the documentation property "allButSelf"?

https://doc.akka.io/docs/akka/current/distributed-pub-sub.html


Solution

  • You want to do

    mediator ! DistributedPubSubMediator.Put(testActor)
    mediator ! DistributedPubSubMediator.SendToAll(path, msg, allButSelf=false) // it is false by default
    

    See the example here https://github.com/akka/akka/blob/0e4d41ad33dbeb00b598cb75b4d29899371bdc8c/akka-cluster-tools/src/test/scala/akka/cluster/pubsub/DistributedPubSubMediatorRouterSpec.scala#L56