Skip to main content

PubSubBroker

PubSubBroker connects a service to Google Cloud Pub/Sub; new only records the project, and the runtime connects it at startup.

PubSubBroker::new takes the project id that short topic and subscription names resolve in. It does no I/O, so the whole service fits the synchronous #[ruststream::app] builder. The settings that follow it are builder steps too: a local emulator, explicit credentials and another service endpoint. Without them the broker reaches pubsub.googleapis.com with Application Default Credentials.

At startup the runtime connects the broker and receives a ConnectedPubSubBroker. Every subscription and every publisher of the service opens against that connected form. At shutdown the runtime flushes what the service published and closes the connection.

#[subscriber("orders-workers")] reads the existing subscription orders-workers of the project. A name is a short id or a full resource name, projects/{project}/subscriptions/{id}. Every other setting of a subscription belongs to the GooglePubSub descriptor.

Example​

The service reads the subscription orders-workers of the project my-project. cargo run --example pubsub_broker --features testing -- run starts it against Pub/Sub.

/// Reads the subscription `orders-workers`.
#[subscriber("orders-workers")]
async fn place(order: &Order) -> HandlerOutcome {
tracing::info!(order.id, "order placed");
HandlerOutcome::ack()
}

/// Application Default Credentials, against `pubsub.googleapis.com`.
#[ruststream::app]
fn app() -> impl App {
RustStream::new(AppInfo::new("orders", "0.1.0")).with_broker(
PubSubBroker::new("my-project"),
|b| {
b.include(place);
},
)
}