View Source SpawnSdk.Flow.Broadcast (spawn_sdk v0.5.1)
Actors can also send messages to a group of actors at once as an action callback. This we call Broadcast.
example-using-elixir-sdk
Example using Elixir SDK:
defmodule Fleet.Actors.Driver do use SpawnSdk.Actor,
kind: :abstract,
# Set ´driver´ channel for all actors of the same type (Fleet.Actors.Driver)
channel: "drivers",
state_type: Fleet.Domain.Driver
alias Fleet.Domain.{
Driver,
OfferRequest,
OfferResponse,
Point
}
require Logger
@brain_actor_channel "fleet-controllers"
defact update_position(%Point{} = position, %Context{state: %Driver{id: name} = driver} = ctx) do
driver_state = %Driver{driver | position: position}
%Value{}
|> Value.of(driver_state, driver_state)
|> Value.broadcast(
Broadcast.to(
@brain_actor_channel,
"driver_position",
driver_state
)
)
|> Value.reply!()
end end
In the case above, every time an Actor "driver" executes the update_position action it will send a message to all the actors participating in the channel called "fleet-controllers".
Broadcasts can also be performed outside the Spawn Actor system, using the transport mechanism based on Phoenix.PubSub in memory or Phoenix.PubSub over Nats Broker.