Google Pub/Sub Inbound Endpoint Example¶ Introduction¶ Google Cloud Pub/Sub is a globally distributed, asynchronous messaging service designed to decouple services that produce events from services that process events. The Google Pub/Sub Inbound Endpoint provides a robust and production-grade implementation of a Google Cloud Pub/Sub subscriber using the Streaming Pull API, built on top of Google's high-level Java client library. The Streaming Pull API is ideal for low-latency, high-throughput message consumption, as it maintains a persistent connection to efficiently stream messages from the server to the client. Note The WSO2 MI Google Pub/Sub Inbound Endpoint also supports subscription creation. To create or update a subscription, the service account must have the roles/pubsub.editor role. However, if you only intend to consume messages from an existing subscription, the roles/pubsub.subscriber role is sufficient. What you’ll build¶ By following this tutorial, you will gain hands-on experience in: Set up Google Cloud Pub/Sub resources. Configure the Inbound Endpoint in WSO2 Integrator: MI using the Visual Studio Code extension. Run and test the integration to receive real-time messages. Step 01: Set up Google Cloud Pub/Sub resources¶ Prerequisites¶ Create a Google Cloud Account if you already don't have one. Create/Select a Google Cloud Project. Enable the Pub/Sub API Create required resources¶ Create a Topic Create a Subscription Set Up Authentication Step 02: Configure the Inbound Endpoint in WSO2 Integrator: MI using the Visual Studio Code extension¶ Follow these steps to configure the inbound endpoint in WSO2 Integrator: MI using the Visual Studio Code extension: Create a new project using the WSO2 Integrator: MI VS Code extension. In the Add Artifact interface, under Create an Integration, click Event Integration. This will open the list of event integrations available in WSO2 Integrator: MI. Select Google Pub/Sub Inbound Endpoint. Fill the form with the values for creating the inbound endpoint. Add a Log Mediator to the sequence to log the incoming messages. Add a Drop Mediator to the sequence to drop the messages after logging. You can use any mediation to mediate the consumed records. Note You can access the consumed record details like pub/sub message id, record published time, ordering-key(if available) and attributes through below message context properties. Each message attribute can be accessed with attribute key. <property name="messageID" expression="${properties.synapse.MessageID}"/> <property name="MessagePublishTime" expression="${properties.synapse.MessagePublishTime}"/> <property name="MessageOrderingKey" expression="${properties.synapse.MessageOrderingKey}"/> <property name="MessageAttribute1" expression="${properties.synapse.attributeName}"/> Step 03: Run and test the integration to receive real-time messages¶ To generate an access token for Publisher, refer to Setting up the Google PubSub Environment. Publish a message to the Google Pub/Sub topic. You can use the below direct Rest API or WSO2 Google Pub/Sub Connector. curl --location 'https://pubsub.googleapis.com/v1/projects/<project-id>/topics/<topic-id>:publish' \ --header 'Content-Type: application/json' \ --header 'Authorization: Bearer <ACCESS-TOKEN>' \ --data '{ "messages": [ { "data": "ewogICJub3RpZmljYXRpb24iOiB7CiAgICAidGl0bGUiOiAiTmV3IERhdGEgUmVjZWl2ZWQiLAogICAgImRlc2NyaXB0aW9uIjogIk9yZGVyIHByb2Nlc3NpbmcgZGV0YWlscy4iLAogICAgImRldGFpbHMiOiB7CiAgICAgICJNZXNzYWdlIENvbnRlbnQiOiAiT3JkZXIgcHJvY2Vzc2VkIiwKICAgICAgIk1ldHJpYyBWYWx1ZSI6IDMuMTQsCiAgICAgICJQcm9jZXNzaW5nIFN0YXR1cyI6ICJTdWNjZXNzZnVsIgogICAgfSwKICAgICJ0aW1lc3RhbXAiOiAiMjAyNS0wNy0wMVQxMDo0NTowMFoiCiAgfQp9", "attributes": { "key1": "value9", "key2": "value2", "attributeName": "user123"}} ]} To deploy and run the project, refer to the Build and Run guide or simply click the Run button in the Visual Studio Code extension. Once you have published the message to your Google Pub/Sub Topic, you can check the logs in the WSO2 Integrator: MI server to see the incoming messages.