View Source SpawnSdk.Flow.Broadcast (spawn_sdk v1.0.0-rc16)

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,
# 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.

Summary

Types

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

Functions

@spec to(channel(), payload()) :: t()
Link to this function

to(channel, action, payload)

View Source
@spec to(channel(), action(), payload()) :: t()