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:
- You define a notification struct (e.g.
WelcomeEmail). - You impl
Notification<User>for it, returning a [NotificationDispatch] populated for each channel you want to use. - You call
notify(&user, ¬ification, &ctx).await?once. The dispatcher sends to mail / database / log / broadcast based on which fields theNotificationDispatchset.
§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 dispatch | Channel | Backend used |
|---|---|---|
email: Some(Email) | ctx.mailer() | |
database: Some(Value) | database | INSERT into ctx.database_table() |
log: Some(String) | log | tracing::info! |
broadcast: Some(Value) | broadcast | ctx.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§
- Notification
Context - Per-app dispatch context — holds the channel backends.
- Notification
Dispatch - One notification, possibly delivered across several channels.
- Notification
Result - Per-channel outcome of one
notifycall.
Enums§
Traits§
- Notifiable
- Marker for things that can receive notifications. Implement on your
User,Operator, etc. Most apps only neednotification_id— the email goes into theEmailbuilder by theNotificationitself. - Notification
- A notification that can be sent to one or more receivers of type
N.
Functions§
- notify
- Send
notificationtorecipientacross 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§
- Broadcast
Fn - Broadcast callback — invoked once per
notify()call when the dispatch has abroadcastpayload. Wire a WebSocket / SSE / pub-sub here.