Skip to main content

Module client

Module client 

Source
Expand description

Clients to interact with Cloud Pub/Sub.

This module contains the primary entry points for the library, including clients for publishing and receiving messages, as well as managing topics, subscriptions, and schemas.

§Example: Publishing Messages

use google_cloud_pubsub::client::Publisher;
use google_cloud_pubsub::model::Message;

// Create a publisher that handles batching for a specific topic.
let publisher = Publisher::builder("projects/my-project/topics/my-topic").build().await?;

// Publish several messages.
// The client will automatically batch them in the background.
let mut futures = Vec::new();
for i in 0..10 {
    let msg = Message::new().set_data(format!("message {}", i));
    futures.push(publisher.publish(msg));
}

// The futures resolve to the server-assigned message IDs.
// You can await them to get the results. Messages will still be sent even
// if the futures are dropped.
for (i, future) in futures.into_iter().enumerate() {
    let message_id = future.await?;
    println!("Message {} sent with ID: {}", i, message_id);
}

§Example: Receiving Messages

use google_cloud_pubsub::client::Subscriber;

// Create a subscriber client.
let client = Subscriber::builder().build().await?;

// Start a message stream from a subscription.
let mut stream = client
    .subscribe("projects/my-project/subscriptions/my-subscription")
    .build();

// Receive messages from the stream.
while let Some((m, h)) = stream.next().await.transpose()? {
    println!("Received message m={m:?}");

    // Acknowledge the message.
    h.ack();
}

Structs§

BasePublisher
Creates Publisher instances.
Publisher
A Publisher client for the Cloud Pub/Sub API.
SchemaService
Implements a client for the Cloud Pub/Sub API.
Subscriber
A Subscriber client for the Cloud Pub/Sub API.
SubscriptionAdmin
Implements a client for the Cloud Pub/Sub API.
TopicAdmin
Implements a client for the Cloud Pub/Sub API.