Typed interface to call the Google Cloud Pub/Sub gRPC API to publish messages and manage topics and subscriptions.
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;
| Name | Description |
|---|---|
| ListTopics | Publisher service: lists the topics that exist in a project. |
| CreateTopic | Publisher service: creates a new topic. |
| DeleteTopic | Publisher service: deletes an existing topic. |
| Publish | Publisher service: publishes one or more messages to a topic. |
| CreateSubscription | Subscriber service: creates a subscription on a topic. |
| DeleteSubscription | Subscriber service: deletes an existing subscription. |
| Pull | Subscriber service: pulls messages from a subscription. |
| Acknowledge | Subscriber service: acknowledges messages received from a subscription. |
| StreamingPull | Subscriber service: bidirectional streaming delivery of messages with flow control. |
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.