Skip to main content

relay

Attribute Macro relay 

Source
#[relay]
Expand description

Registers a channel handler for the relay system.

Channel handlers receive RelayEvent events when clients subscribe, send messages, or disconnect from matching topics.

The pattern supports exact matches and prefix matches (trailing *):

  • "chat:lobby" โ€” matches only the exact topic "chat:lobby"
  • "room:*" โ€” matches any topic starting with "room:"

The first parameter must be RelayEvent. Remaining parameters are extracted via FromRequestParts with synthetic request parts (same extractors as HTTP handlers, minus body extractors).

ยงExample

โ“˜
use rapina::prelude::*;
use rapina::relay::{Relay, RelayEvent};

#[relay("room:*")]
async fn room(event: RelayEvent, relay: Relay) -> Result<()> {
    match &event {
        RelayEvent::Join { topic, conn_id } => {
            relay.track(topic, *conn_id, serde_json::json!({}));
        }
        RelayEvent::Message { topic, event: ev, payload, .. } => {
            relay.push(topic, ev, payload).await?;
        }
        RelayEvent::Leave { topic, conn_id } => {
            relay.untrack(topic, *conn_id);
        }
    }
    Ok(())
}