View Source SpawnSdk.Flow.Broadcast (spawn_sdk v1.3.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:

defmodule Fleet.Actors.Driver do use SpawnSdk.Actor,

kind: :abstract,
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_state
  )
)
|> Value.reply!()

end end

defmodule Fleet.Actors.FleetControllersActor do use SpawnSdk.Actor,

kind: :unnamed,
channels: [
  {"fleet.controllers.topic", "update_position_receive"}
] # or just ["fleet.controllers.topic"] and it will forward to a action called receive

alias Fleet.Domain.Point

defact update_position_receive(%Point{} = position, _ctx) do

Logger.info(
  "Received Update Position Event. Position: [{inspect(position)}]"
)

Value.of()

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.

Summary

Types

@type channel() :: String.t()
@type payload() :: term() | nil
@type t() :: %SpawnSdk.Flow.Broadcast{channel: String.t(), payload: module()}

Functions

@spec to(channel(), payload()) :: t()