Google Cloud Pub/Sub

Typed interface to call the Google Cloud Pub/Sub gRPC API to publish messages and manage topics and subscriptions.

Introduction

Google Cloud Pub/Sub is a fully managed messaging service for sending and receiving messages between independent applications. The gRPC API is exposed through the google.pubsub.v1.Publisher service (topics and publishing) and the google.pubsub.v1.Subscriber service (subscriptions and message delivery), reached at pubsub.googleapis.com:443 over TLS.

Requests and responses are built with typed classes such as TsgcGRPCPubSubListTopicsRequest / TsgcGRPCPubSubListTopicsResponse, TsgcGRPCPubSubTopic, TsgcGRPCPubSubPublishRequest / TsgcGRPCPubSubPublishResponse, TsgcGRPCPubSubSubscription, TsgcGRPCPubSubPullRequest / TsgcGRPCPubSubPullResponse and TsgcGRPCPubSubAcknowledgeRequest, so you do not need to encode Protocol Buffers by hand.

The example below authenticates with a service-account JWT, wires a TsgcGRPCClient over a TsgcHTTP2Client to the Pub/Sub host, sets the authorization Bearer metadata and calls ListTopics for a project:


    oHTTP2 := TsgcHTTP2Client.Create(nil);
    oHTTP2.Host := 'pubsub.googleapis.com';
    oHTTP2.Port := 443;
    oHTTP2.TLS := True;

    oGRPC := TsgcGRPCClient.Create(nil);
    oGRPC.Client := oHTTP2;

    // service-account JWT authentication
    oGRPC.GoogleCloudOptions.JWT.KeyFile := 'service-account.json';
    oGRPC.GoogleCloudOptions.JWT.API_Endpoint := 'https://pubsub.googleapis.com/';
    oGRPC.DefaultMetadata.AddValue('authorization', 'Bearer ' + oGRPC.GoogleCloudOptions.JWT.Token);

    // build the typed request and call the method
    oRequest := TsgcGRPCPubSubListTopicsRequest.Create;
    try
      oRequest.Project := 'projects/my-project-id';
      oResponse := oGRPC.Call('google.pubsub.v1.Publisher', 'ListTopics', oRequest.ToBytes);
      ShowMessage(oResponse.DataString);
    finally
      oRequest.Free;
    end;

Methods

NameDescription
ListTopicsPublisher service: lists the topics that exist in a project.
CreateTopicPublisher service: creates a new topic.
DeleteTopicPublisher service: deletes an existing topic.
PublishPublisher service: publishes one or more messages to a topic.
CreateSubscriptionSubscriber service: creates a subscription on a topic.
DeleteSubscriptionSubscriber service: deletes an existing subscription.
PullSubscriber service: pulls messages from a subscription.
AcknowledgeSubscriber service: acknowledges messages received from a subscription.
StreamingPullSubscriber service: bidirectional streaming delivery of messages with flow control.

Demo

A working sample is available in the demo folder Demos/21.GRPC/10.PubSub, which shows how to authenticate, list and create topics, publish messages and pull from a subscription.

See Also