Skip to main content

Module notifications

Module notifications 

Source
Expand description

Multi-channel notifications — fan one notification out to mail / database / log / broadcast channels. See notifications::notify. Multi-channel notifications — fan one notification out to mail / database / log / broadcast channels.

Laravel’s Illuminate\Notifications is the inspiration. The shape:

  1. You define a notification struct (e.g. WelcomeEmail).
  2. You impl Notification<User> for it, returning a [NotificationDispatch] populated for each channel you want to use.
  3. You call notify(&user, &notification, &ctx).await? once. The dispatcher sends to mail / database / log / broadcast based on which fields the NotificationDispatch set.

§Quick start

use rustango::notifications::{
    notify, Notifiable, Notification, NotificationContext, NotificationDispatch,
};
use rustango::email::Email;
use serde_json::json;

pub struct WelcomeEmail { pub display_name: String }

impl Notification<User> for WelcomeEmail {
    fn build(&self, user: &User) -> NotificationDispatch {
        NotificationDispatch {
            email: Some(
                Email::new()
                    .to(&user.email)
                    .from("noreply@app.example.com")
                    .subject("Welcome!")
                    .body(&format!("Hi {}, thanks for signing up.", self.display_name)),
            ),
            database: Some(json!({
                "type": "user.welcome",
                "display_name": self.display_name,
            })),
            log: Some(format!("welcomed user {}", user.username)),
            broadcast: None,
        }
    }
}

impl Notifiable for User {
    fn notification_id(&self) -> Option<i64> { Some(self.id) }
}

// At call site:
let ctx = NotificationContext::new()
    .with_mailer(mailer)
    .with_database(pool.clone(), "user_notifications");
let result = notify(&user, &WelcomeEmail { display_name: "Alice".into() }, &ctx).await?;
println!("delivered to {} channels", result.delivered_count());

§Channels

Field on dispatchChannelBackend used
email: Some(Email)mailctx.mailer()
database: Some(Value)databaseINSERT into ctx.database_table()
log: Some(String)logtracing::info!
broadcast: Some(Value)broadcastctx.broadcast() callback

Each channel is independent — failing to send via mail does NOT abort database / log delivery. The returned [NotificationResult] reports per-channel outcomes.

Structs§

NotificationContext
Per-app dispatch context — holds the channel backends.
NotificationDispatch
One notification, possibly delivered across several channels.
NotificationResult
Per-channel outcome of one notify call.

Enums§

ChannelOutcome
NotificationError

Traits§

Notifiable
Marker for things that can receive notifications. Implement on your User, Operator, etc. Most apps only need notification_id — the email goes into the Email builder by the Notification itself.
Notification
A notification that can be sent to one or more receivers of type N.

Functions§

notify
Send notification to recipient across every configured channel.
notify_many
Send the same notification to many recipients sequentially. Convenient when the notification doesn’t vary per-user. Returns one result per recipient.
should_send_throttled
Track whether a notification has been sent recently — useful for rate-limiting “X new comments” emails or similar burst-prone events.

Type Aliases§

BroadcastFn
Broadcast callback — invoked once per notify() call when the dispatch has a broadcast payload. Wire a WebSocket / SSE / pub-sub here.