Skip to main content

Crate larust_reverb

Crate larust_reverb 

Source
Expand description

A generic WebSocket pub/sub broadcasting server - Larust’s port of laravel/reverb. Sibling to, not a replacement for, larust_live::push (@live(channel) ... @endlive): that mechanism pushes pre-rendered HTML fragments to a fixed [data-live-channel] DOM element and has no notion of a named event; this one pushes arbitrary JSON payloads, tagged with an event name, to whatever JS code on the page chooses to subscribe (LarustReverb.channel(name).listen(eventName, callback), not a DOM patch). Deliberately a separate route namespace (/__larust_reverb/*, not /__larust_push/*) and a separate channel registry - an app that pointed both mechanisms at the same channel name would have @live’s client try to DOM-patch a JSON payload, or a Reverb listener receive raw HTML instead of {event, data}, so the two contracts stay physically separate rather than trying to share one wire format.

The channel/broadcast/WebSocket-upgrade plumbing below intentionally mirrors larust_live::push‘s own - the same process-wide, lazily created channel-registry shape, the same connection-limiting semaphore, the same bounded/validated channel names - duplicated rather than shared, since it’s a small, well-understood mechanism and the two crates’ payload contracts (opaque HTML vs. a JSON envelope) are different enough that forcing a shared abstraction across them would cost more clarity than it’d save.

§Public vs. private channels

A channel name starting with private- requires authorization (see authorize); any other name is public - anyone who can reach the route can subscribe, no different from @live’s own channels today. This is Pusher/Laravel’s own real naming convention, not invented here.

§Deliberately out of scope

  • No presence channels - membership tracking, join/leave events, a here()/joining()/leaving() client API. A real, separate feature; not attempted in this version.
  • No Pusher-wire-protocol compatibility. This isn’t a drop-in replacement for pusher-js or Laravel Echo - authorize’s callback runs directly at WebSocket-upgrade time (the browser already carries the session cookie to this same-origin request), not via a separate POST .../auth round trip the way real Pusher/Reverb need to (their split exists because Pusher itself is a third-party service with no access to the app’s own session - this server is the app, so that round trip has no purpose here).
  • No per-channel-pattern authorization registry. Laravel lets an app register a separate callback per channel-name pattern (Broadcast::channel('orders.{id}', fn ($user, $id) => ...)). This crate has one global callback instead - it receives the full channel name and does its own matching inside - avoiding a route-pattern-matching mini-DSL for what’s usually a handful of if/match arms in practice.

§Example

ⓘ
// At app boot, next to `larust_support::wire::components()...publish()`:
larust_support::reverb::authorize(|session, channel| async move {
    let Ok(Some(user)) = larust_support::auth::user::<User>(&session).await else {
        return false;
    };
    // e.g. "private-orders.42" -> only order 42's own owner may listen.
    channel == format!("private-orders.{}", user.id)
});

// Route registration - shorthand for the pair of `.get(...)` calls this
// crate's own `ReverbPlugin` bundles:
// .plugin(larust_support::reverb::ReverbPlugin)

// Wherever an order actually ships:
larust_support::reverb::broadcast_event(
    &format!("private-orders.{}", order.user_id),
    "OrderShipped",
    &order,
)?;

Structs§

ReverbPlugin
The two routes a Reverb-broadcasting app needs, bundled for larust_http::Router::plugin - sugar for the .get/.get pair this crate’s own doc comment example shows an app writing by hand today. Gated behind the reverb Cargo feature one layer up, in larust_support::reverb - this crate itself has no feature flags of its own, since it only ever compiles when that optional dependency is pulled in.

Functions§

authorize
Registers the callback deciding whether a private--prefixed channel may be subscribed to. Called with the current request’s Session and the full channel name (prefix included) at WebSocket-upgrade time, before the connection is accepted - never called at all for a channel that isn’t private--prefixed (public, unauthenticated by design).
broadcast_event
Broadcasts event_name/payload - JSON-serialized as {"event": event_name, "data": payload} - to every client currently subscribed to channel. Call this from wherever server state actually changes (an event listener, a controller action), not from inside the request rendering the page itself.
runtime_js
GET /__larust_reverb/runtime.js - the vendored client runtime, served from the installed crate itself rather than copied into public/js/, same reasoning as larust_live::push::runtime_js.
socket
GET /__larust_reverb/{channel} - upgrades to a WebSocket and streams every broadcast_event call’s JSON envelope through, verbatim, for the lifetime of the connection. Pure server → client push: no client → server message is ever acted on (an inbound message is only read to detect the browser tab closing the connection).