Mail, Broadcasting and Inertia
Part 2 · Laravel Rust. A beginner tour of three Laravel Rust features: typed email with Mailable, realtime updates over WebSockets, and Inertia for server-driven React pages.
Your café site can already take orders and run work in the background. Three more features turn it from a form into a real product: email (a receipt in the customer’s inbox), broadcasting (an order page that updates itself when the barista finishes the drink), and Inertia (a smooth, app-like frontend without building a separate JSON API).
This lesson is an overview. Each part shows the key idea with a small example adapted from the official guides, and links to the full documentation.
Note
Laravel Rust is in beta (announced December 1, 2026). Details may change, so treat the docs as the source of truth: Mail (opens in new tab), Broadcasting (opens in new tab), WebSockets (opens in new tab) and Inertia (opens in new tab).
Part 1: Mail
A mailable is a struct
An email is just a struct whose fields fill in a template. Derive Mailable and you are most of the way there:
use laravel::mail::Mailable;
use crate::models::Order;
#[derive(Mailable)]#[mail(subject = "Your Rusty Bean receipt")]pub struct OrderReceipt { pub order: Order, pub total: String,}Mailables live in a src/mail/ module. As the structure guide says, declare it in src/lib.rs (pub mod mail;) and list each file in src/mail/mod.rs (mod order_receipt; pub use order_receipt::OrderReceipt;), or Rust will skip the file.
#[derive(Mailable)]connects the struct to an email view. By the guide’s naming rule, the view is named after the struct in kebab case, soOrderReceiptrendersresources/views/mail/order-receipt.html.- Each field is available in the template by name (for example
{{ order.id }}), so each field must implementSerialize. - Laravel also generates a matching plain-text version of the email automatically, so you do not maintain two templates.
You may use {field} placeholders in a subject, such as subject = "Order #{order_id} is ready".
Sending it
use laravel::mail;
mail::to(&order.user) .queue(OrderReceipt { order, total }) .await?;(order.user is the customer who placed the order, loaded with .with(Order::user). Passing a &User works once you implement From<&User> for Address, which the capstone shows. mail::to also accepts a plain &str or String email address.) The guide gives you three delivery methods:
| Method | What it does |
|---|---|
queue |
Pushes the rendered email onto your queue. Workers deliver it and retry on failure. Best for most handlers. |
send |
Sends right now and waits for the provider. Good inside a job. |
send_background |
Sends in a background task, with no retries and no queue worker. |
Laravel renders and validates the email immediately, so a broken template or invalid address fails at your call site, before anything reaches the mail provider.
Providers and previews
Your .env file chooses the mailer with MAIL_MAILER. The default is log, which writes each email to your application log instead of sending it, so a new app sends nothing until you choose a provider. The guide lists first-party Resend and Amazon SES transports, plus a failover mailer that tries several in order. SMTP is not bundled.
To design an email, return the mailable straight from a development route and view it in the browser:
pub async fn preview(order: Order) -> laravel::Result<OrderReceipt> { Ok(OrderReceipt { order, total: String::from("$4.50") })}(String::from turns the text "$4.50" into an owned String, which the struct field needs. You will also see .into() do the same job. It is Rust’s general “convert this into whatever type is expected” method, covered in Traits and Generics.)
Tip
Keep money as integer cents everywhere in your code (as you did in the Rust lessons), and format it into a string like "$4.50" only at the edge, such as when you build the email.
Part 2: Broadcasting
Push, don’t poll
Normally the browser asks the server a question and gets one answer. For “your latte is ready” you would need to ask over and over (polling). Broadcasting flips this around. The browser keeps a connection open, called a WebSocket, and the server pushes news down it the moment it happens. That is what realtime means.
Laravel ships a local WebSocket server called Reverb, written in Rust and speaking the Pusher protocol. Per the announcement, you enable the reverb Cargo feature and run laravel reverb:start to develop locally, then switch environment variables to managed Reverb on Laravel Cloud when you deploy.
Broadcasting an event
You can broadcast any event by adding an attribute:
use laravel::event::Event;
#[derive(Event)]#[event(broadcast(private = "orders.{order_id}"))]pub struct OrderReady { pub order_id: i64, pub drink: String, pub(crate) barista_note: String,}New apps already ship a src/events/ folder, so put the file there and list it in src/events/mod.rs (mod order_ready; pub use order_ready::OrderReady;). Then dispatch it like any event: OrderReady { .. }.dispatch().await?. Here is what happens:
- The event goes out on a channel named
private-orders.42(for order 42). A channel is a named stream that browsers subscribe to. privatemeans only authorized users may listen. There are alsopublicchannels (anyone) andpresencechannels (which also show who is subscribed).- The payload contains the event’s
pubfields.pub(crate)and private fields, likebarista_note, are left off the wire.
Warning
Every subscriber to a channel gets the same payload. Only include data that everyone allowed on that channel may see.
Authorizing a private channel
Channels are registered in src/routes/channels.rs. Each pairs a pattern with a function that says yes or no:
use laravel::broadcast::Channels;use laravel::prelude::*;
use crate::models::{Order, User};
pub fn routes(channel: &mut Channels) { channel.private("orders.{order}", order);}
async fn order(order: Order, user: Authenticated<User>) -> bool { order.user_id == user.id}The {order} in the pattern is bound to an Order model, the same route model binding you learned for handlers. The function returns true only for the person who owns that order.
Listening in the browser
The guide uses Laravel Echo’s React hook:
import { useEcho } from '@laravel/echo-react';
type Order = { id: number; status: string };
export function OrderStatus({ order }: { order: Order }) { useEcho(`orders.${order.id}`, 'OrderReady', (event) => { console.log(`${event.drink} is ready!`); });
// ...}Echo authorizes the private channel for you and leaves it when the component goes away. Broadcasts travel through your queue, so locally you need your app, a queue worker and laravel reverb:start all running.
Part 3: Inertia
Building a single-page app (SPA) normally means two projects: a JSON API on the server and a separate frontend that fetches from it. Inertia lets you skip the API. Your handler picks a React (or Vue or Svelte) page and hands it the data directly:
use laravel::inertia::{self, Page, props};
use crate::models::MenuItem;
pub async fn index() -> laravel::Result<Page> { let items = MenuItem::all().await?; Ok(inertia::render("Menu/Index", props! { items }))}type Item = { id: number; name: string; price_cents: number };
export default function Index({ items }: { items: Item[] }) { return ( <ul> {items.map((item) => ( <li key={item.id}> {item.name}: ${(item.price_cents / 100).toFixed(2)} </li> ))} </ul> );}inertia::render("Menu/Index", ...)names a page component. Page files live inresources/js/pages, so this isMenu/Index.tsx.props! { items }sends the local variableitemsas a prop nameditems.- The React component receives
itemsas an ordinary prop.
New Laravel Rust apps come set up with Inertia, React, TypeScript, Tailwind and Vite. The guide also covers deferred props (load slow data after the first render), polling, flash messages and server-side rendering.
Live props: Inertia + broadcasting
The two features combine. A live prop refreshes itself whenever a matching broadcast event arrives:
order => inertia::lazy(async move || Order::find(order_id).await) .live(inertia::on(&OrderUpdated { order_id })?),When OrderUpdated is broadcast, Inertia reloads just that prop. The page updates without custom code. (This needs a small client setup described in the Inertia guide.)
Note
If you prefer plain server-rendered HTML pages (with Jinja-style templates and layouts), Laravel Rust supports views too. Inertia is the default choice for interactive apps.
Try it yourself
- Write a
Mailablestruct calledPickupReadywith adrink: Stringfield and the subject “Your drink is ready”. - Which channel type would you use for each: (a) a public “Today’s special” banner, (b) one customer’s order status?
Show solution
use laravel::mail::Mailable;
#[derive(Mailable)]#[mail(subject = "Your drink is ready")]pub struct PickupReady { pub drink: String,}Its template would live at resources/views/mail/pickup-ready.html.
- (a) A
publicchannel, because anyone may see the banner. (b) Aprivatechannel, authorized so only the order’s owner can subscribe.
Recap
- A
Mailablestruct’s fields fill an email template, and Laravel makes the plain-text version for you. Preferqueuefor delivery. - Broadcasting pushes events to browsers over WebSockets instead of making the page poll. Reverb is the bundled local server.
#[event(broadcast(...))]publishes an event to channels. Authorize private channels insrc/routes/channels.rs.- Inertia lets handlers render React pages with props, no separate API required.
- Live props connect Inertia pages to broadcast events.
What’s next
How do you know all this works? In Testing Your App you will write tests that fake mail, queues and more.
0 of 35 lessons complete