Smeltery
Get started

Anvil (broadcasting)

v0.1

Events

An event is a struct that serializes to its data:

rust
use serde::Serialize;
use smeltery::anvil::{Anvil, BroadcastEvent, SocketId};
use smeltery::prelude::*;

#[derive(Serialize, BroadcastEvent)]
#[broadcast(private = "orders.{order_id}")]
pub struct OrderShipped {
    pub order_id: i64,
    pub tracking: String,
    #[serde(skip)]
    pub internal_note: String,
}

async fn ship(anvil: Anvil, socket: Option<SocketId>) -> Result<&'static str> {
    // … mark order 7 shipped …
    anvil
        .send(&OrderShipped { order_id: 7, tracking: "1Z999".into(), internal_note: String::new() })
        .except(socket)
        .await?;
    Ok("shipped")
}
  • #[broadcast(public = "…")], #[broadcast(private = "…")] and #[broadcast(presence = "…")] name the channels (several are allowed); {field} is the field's Display value, and a field the struct does not have is a compile error.
  • The event name is App\Events\<TypeName> by default: laravel-echo's .listen("OrderShipped") listens for that name. #[broadcast(as = "order.shipped")] sets another; laravel-echo listens for it with a leading dot (.listen(".order.shipped")).
  • Names starting with pusher: or pusher_internal: are refused (the protocol reserves them). A {field} value goes into the channel name as it is, so a value with a . adds a segment: use fields such as ids. A channel named twice in one event is sent once.
  • The data is the struct's JSON (serde attributes apply; #[serde(skip)] keeps a field out). Data on a public channel is public.
  • Without a derive: impl BroadcastEvent for X { fn channels(&self) -> Vec<Channel> { … } } (the name defaults as above), or an event without a type: anvil.to(Channel::public("news")).event("posted").with(&json!({ "id": 3 })).await?.
  • send(...).await returns Delivered { local }, the sockets of this process the event was queued for. It fails when the data is larger than ANVIL_MAX_EVENT_SIZE, a channel name is invalid, the name is empty or longer than 200 bytes, or the app's PubSub driver fails (the sockets of this process got the event then; the other processes did not). A failed check sends nothing.
  • Other processes: the event also goes out on the app's PubSub (topic anvil); every serving process hands it to its own sockets. A job in a work process reaches sockets held by serve --no-agents when both use a shared PUBSUB_DRIVER.
  • Delivery is at most once, in order per sending process. A client that reconnects fetches the current state again.
  • An event sent inside a database transaction is delivered at once, before the commit; send after it.

Leaving the sender out. A client sends its socket id with its own requests in the X-Socket-ID header. SocketId reads it (Option<SocketId> is None without it; only <digits>.<digits> is accepted), and .except(socket) leaves that socket out of the event. A client can name another socket's id, which keeps one event from that socket and reveals nothing else.

↑↓ moveEnter openEsc close