Restioson/xtra

Help: notify_all not clear

overflowz opened this issue · 7 comments

Hey there! First of all, wanted to say thanks for this great library, tried many actor frameworks so far, but sticking with xtra due to its simplicity, speed, and easy handling of async/await :-)

I have a question about notify_all method, which is not clear to me. It says to notify the message to all of the actors running with the same address, but I wasn't able to figure out how to register two actors at the same address.

My goal is the following: I have some actor, which accepts messages, but also broadcasts (via notify_all) messages (like events). The only way I found to do this is to call clone_channel and pass it to the actor itself so the actor can call it later (vector of MessageChannel).

So there are two questions:

  1. What exactly is the use case of notify_all?
  2. How can I model xtra such it will notify all the actors that have handlers for the Message for example?

Thank you!

Thanks!

There are a lot of nice API changes coming in the next release that should make various use cases simpler. In particular, latest master already has a broadcast function on Address to send the same message to all actors.

You can run multiple actors on one Context by using Context::attach instead of Context::run. Note that that might also change until next release.

Thanks for the info! I'll be waiting for the next release then :-)

Keep it up!

How can I model xtra such it will notify all the actors that have handlers for the Message for example?

If the recipients of your messages are different actors then the broadcast / notify_all function doesn't help you unfortunately. What you need is some sort of "fan-out" pattern where one actor holds a list of message channels and sending one message to that actor will send it to all.

You could also use AddressSink and use https://docs.rs/futures/latest/futures/sink/trait.SinkExt.html#method.fanout

The relevant example is message_stealing.rs. It is probably worth adding some pointers somewhere around run so that Context::attach isn't missed!

Thank you for the replies, will definitely have a better look :)

Just as an FYI: We are hanging out in https://matrix.to/#/#xtra-community:matrix.org if you want to follow along more closely with the current development.

would've been joined if it were a discord, but I really don't use matrix (nor heard of it before). also, I don't think I'll be helpful that much, because I'm fairly new to rust:)