Skip to main content

subscription

Attribute Macro subscription 

Source
#[subscription]
Expand description

Creates a subscription handler for specific events.

This macro transforms an async function into a handler struct that implements the subscription::Handler<E> trait for processing events in real-time subscriptions.

Unlike projection handlers, subscription handlers receive a context with access to the executor and can perform side effects like database updates, notifications, or external API calls.

§Function Signature

The function must have this signature:

async fn handler_name<E: Executor>(
    context: &Context<'_, E>,
    event: Event<EventType>,
) -> anyhow::Result<()>

§Generated Code

For a function on_money_deposited, the macro generates:

  • OnMoneyDepositedHandler struct
  • on_money_deposited() constructor function
  • subscription::Handler<E> trait implementation

§Example

use evento::{Executor, metadata::Event, subscription::Context};

#[evento::subscription]
async fn on_money_deposited<E: Executor>(
    context: &Context<'_, E>,
    event: Event<MoneyDeposited>,
) -> anyhow::Result<()> {
    // Access shared data from context
    let config: Data<AppConfig> = context.extract();

    // Perform side effects
    send_notification(&event.data).await?;

    Ok(())
}

// Register with subscription
let subscription = SubscriptionBuilder::<Sqlite>::new("deposit-notifier")
    .handler(on_money_deposited())
    .routing_key("accounts")
    .start(&executor)
    .await?;