Skip to main content

Actions

The ballerinax/gcloud.pubsub package exposes the following clients:

ClientPurpose
PublisherPublishes messages to a Google Cloud Pub/Sub topic.

For event-driven integration, see the Trigger Reference.


Publisher​

Publishes messages to a Google Cloud Pub/Sub topic.

Configuration​

FieldTypeDefaultDescription
batchConfigBatchConfig()Message batching configuration (max delay, message count, byte limit).
compressionrecord {| int threshold = 240; |}()Compression configuration. Messages exceeding the threshold (in bytes, default 240) are compressed.
authGcpCredentialConfig()GCP service account credentials. Contains a path field pointing to the JSON key file. If omitted, Application Default Credentials are used.
enableMessageOrderingbooleanfalseEnable message ordering using ordering keys.
retryConfigRetryConfig()Retry configuration with exponential backoff for publish failures.

Initializing the client​

import ballerinax/gcloud.pubsub;

configurable string project = ?;
configurable string topic = ?;
configurable string gcpCredentialsFilePath = ?;

pubsub:Publisher publisher = check new (project, topic,
auth = {path: gcpCredentialsFilePath}
);

Operations​

Messaging​

publish

Publishes a message to the configured Pub/Sub topic. The message data can be any anydata value: byte[] is sent as-is, string and xml values are sent as UTF-8 text, and all other values are serialized as JSON.

Parameters:

NameTypeRequiredDescription
messageMessageYesThe message record containing data, optional attributes, and optional orderingKey.

Returns: string|Error

Sample code:

string messageId = check publisher->publish({
data: "Hello from Ballerina!",
attributes: {"source": "ballerina-app", "env": "production"}
});

Sample response:

"1234567890123456"
close

Gracefully shuts down the publisher, flushing any pending batched messages before closing.

Parameters:

NameTypeRequiredDescription
timeoutdecimalNoMaximum time in seconds to wait for graceful shutdown. Defaults to 10.0.

Returns: Error?

Sample code:

check publisher->close();