---
schema_version: "1"
id: 1f2ce1ea6469687ca63d0a07be6b3edda6b04616b565eb51aba6a4924c18f4eb
title: Google Pub/Sub Connector 1.2 Examples
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/google-pubsub-connector/1.2/google-pubsub-connector-examples.md
canonical_url: https://docs.mulesoft.com/google-pubsub-connector/1.2/google-pubsub-connector-examples
source_site: docs.mulesoft.com
content_type: documentation
content_hash: c4698ba0f8e1d1adfce5a281f71565c1fb1a9c134fdb9b57799e63d267616788
description: MuleSoft Documentation Site
release: "1.2"
latest: false
---
---
product: Google Pub/Sub Connector
version: 1.2
---

# Google Pub/Sub Connector 1.2 Examples

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

These examples show you how to use Google Pub/Sub Connector to perform the following actions:

- [Create a snapshot](#create-snapshot)

- [Patch a snapshot](#patch-snapshot)

- [Get a snapshot](#get-snapshot)

- [Delete a snapshot](#delete-snapshot)

- [Publish a message](#publish-message)

## Configure a PubSub Connection

Because this connector uses PubSub to secure connections, you must specify the PubSub Connection
field values. To do this:

- [Create a configuration file for a PubSub Connection.](#create-config-file)

- [Configure the PubSub Connection global elements.](#configure-pubsub-global)

- [Configure a global element for the properties file.](#configure-global)

### Create a Configuration File for a PubSub Connection

Create a configuration file that includes properties for a PubSub Connection:

1.  Create a file named `mule-app.properties` in the `/src/main/resources/` folder.

2.  In the `mule-app.properties` file, create a set of properties for `PubSub Connection`, similar
    to the ones that follow, replacing the bracketed text (including the brackets) with the correct
    values for your configuration:

    config.private\_key\_id=<privatekeyid> config.private\_key=<privatekey>
    config.client\_id=<clientid> config.client\_email=<clientemail>
    config.default\_project\_id=<defaultprojectid>

    For more information about creating a properties file, refer to
    [Configuring Property Placeholders](https://docs.mulesoft.com/mule-runtime/latest/mule-app-properties-to-configure).

### Configure the PubSub Connection Global Elements

Configure global elements for the `PubSub Connection` file:

1.  Create a new Mule project.

2.  In the **Mule Palette** view, click **Search in Exchange** and enter `google pubsub`.

3.  Add **Google Pub/Sub Connector** to the **Selected modules** section and click **Finish**.

4.  Click the **Global Elements** tab and click **Create**.

5.  Select **Connector Configuration > Google PubSub Configuration** and click **OK**.

6.  Enter the following values:

    <table><colgroup><col> <col></colgroup><thead><tr><th>Field</th><th>Value</th></tr></thead><tbody><tr><td><p>Name</p></td><td><p><code>Google_Pub_Sub_Configuration</code></p></td></tr><tr><td><p>Private key id</p></td><td><p><code>${config.private_key_id}</code></p></td></tr><tr><td><p>Private key</p></td><td><p><code>${config.private_key}</code></p></td></tr><tr><td><p>Client id</p></td><td><p><code>${config.client_id}</code></p></td></tr><tr><td><p>Client email</p></td><td><p><code>${config.client_email}</code></p></td></tr><tr><td><p>Default Project Id</p></td><td><p><code>${config.default_project_id}</code></p></td></tr><tr><td><p>Default Subscription Name</p></td><td><p><code>orders-subscription</code></p></td></tr><tr><td><p>Default Topic Name</p></td><td><p><code>orders-topic</code></p></td></tr></tbody></table>

7.  Click **OK**.

### Configure a Global Element for the Properties File

Configure a global element for the `mule-app.properties` file so that Mule knows where to find it:

1.  Click the **Global Elements** tab and click **Create**.

2.  In the **Choose Global Type** dialog, select **Configuration properties** and click **OK**.

3.  In the **File** field, enter `mule.app.properties`.

4.  Click **OK**.

## Create a Snapshot

This Mule flow creates a snapshot from the requested subscription.

This example uses the following operations:

- **HTTP Listener**  
  Accepts data from HTTP requests

- **Create snapshot**  
  Creates a snapshot

- **Logger**  
  Shows the HTTP response from the **Create snapshot** operation

- **Transform Message**  
  Outputs the results of the **Create snapshot** operation in JSON format

![Studio Flow for the Create Snapshot Operation](/mulesoft/current/en-us/google-pubsub-connector/1.2/_images/create-snapshot-flow.png)

### XML for This Example

Paste this code into the Studio XML editor to quickly load the flow for this example into your Mule
app:

```xml
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
      xmlns:pubsub="http://www.mulesoft.org/schema/mule/pubsub"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
                          http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
                          http://www.mulesoft.org/schema/mule/pubsub http://www.mulesoft.org/schema/mule/pubsub/current/mule-pubsub.xsd
                          http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">

    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config">
        <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>

    <pubsub:config name="Google_Pub_Sub_Configuration" doc:name="Google Pub Sub Configuration" projectId="${config.default_project_id}">
        <pubsub:connection privateKeyId="${config.private_key_id}" privateKey="${config.private_key}" clientId="${config.client_id}" clientEmail="${config.client_email}" />
    </pubsub:config>

    <configuration-properties doc:name="Configuration properties" file="mule-app.properties" />

    <flow name="create-snapshot-flow">
        <http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/createSnapshot"/>
        <pubsub:create-snapshot
                doc:name="Create snapshot"
                config-ref="Google_Pub_Sub_Configuration"
                snapshotName="#[attributes.queryParams.snapshot]"
                subscriptionName="#[attributes.queryParams.sub]">
            <pubsub:labels ><![CDATA[#[output application/java
---
{
	"label1" : "test-snapshot-demo"
}]]]></pubsub:labels>
        </pubsub:create-snapshot>
        <logger level="INFO" doc:name="Logger" message="Created Snapshot with name: #[payload.name]"/>
        <ee:transform doc:name="Transform Message">
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
            </ee:message>
        </ee:transform>
    </flow>
</mule>
```

### Steps for Running This Example

1.  Verify that your connector is configured.

2.  Save the project.

3.  From a web browser, test the application by entering
    `[http://localhost:8081/](http://localhost:8081/)`. You should see the **Create Snapshot** form
    with pre-populated **Snapshot Name** and **Subscription Name** fields.

    ![Create Snapshot Form](/mulesoft/current/en-us/google-pubsub-connector/1.2/_images/create-snapshot.png)

4.  Click **Create Snapshot**. You should see an alert that the snapshot was successfully created.

## Patch a Snapshot

This Mule flow updates an existing snapshot.

This example uses the following operations:

- **HTTP Listener**  
  Accepts data from HTTP requests

- **Patch snapshot**  
  Updates a snapshot

- **Transform Message**  
  Outputs the results of the **Patch snapshot** operation in JSON format

- **Logger**  
  Shows the HTTP response from the **Patch snapshot** operation

![Studio Flow for the Patch Snapshot Operation](/mulesoft/current/en-us/google-pubsub-connector/1.2/_images/patch-snapshot-flow.png)

### XML for This Example

Paste this code into the Studio XML editor to quickly load the flow for this example into your Mule
app:

```xml
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
      xmlns:pubsub="http://www.mulesoft.org/schema/mule/pubsub"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
                          http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
                          http://www.mulesoft.org/schema/mule/pubsub http://www.mulesoft.org/schema/mule/pubsub/current/mule-pubsub.xsd
                          http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">

    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config">
        <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>

    <pubsub:config name="Google_Pub_Sub_Configuration" doc:name="Google Pub Sub Configuration" projectId="${config.default_project_id}">
        <pubsub:connection privateKeyId="${config.private_key_id}" privateKey="${config.private_key}" clientId="${config.client_id}" clientEmail="${config.client_email}" />
    </pubsub:config>

    <configuration-properties doc:name="Configuration properties" file="mule-app.properties" />

     <flow name="patch-snapshot-flow">
        <http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/patch-snapshot"/>
        <pubsub:patch-snapshot doc:name="Patch snapshot" config-ref="Google_Pub_Sub_Configuration" updateMask="labels">
            <pubsub:snapshot><![CDATA[#[%dw 2.0
output application/json
 ---
{
  "name": "projects/<<project-name>>/snapshots/<<snapshot-name>>",
    "topic": "projects/<<project-name>>/topic/<<topic-name>>"
    "expireTime": "2099-01-01T00:00:00Z",
    "labels": {
      "label1": "test-patch-snapshot"
    }
}]]]></pubsub:snapshot>
        </pubsub:patch-snapshot>
        <ee:transform doc:name="Transform Message">
            <ee:message>
                <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <logger level="INFO" doc:name="Logger" message="Updated Snapshot: #[payload]"/>
    </flow>
</mule>
```

## Get a Snapshot

This Mule flow retrieves an existing snapshot.

This example uses the following operations:

- **HTTP Listener**  
  Accepts data from HTTP requests

- **Get snapshot**  
  Retrieves a snapshot

- **Logger**  
  Shows the HTTP response from the **Get snapshot** operation

- **Transform Message**  
  Outputs the results of the **Get snapshot** operation in JSON format

![Studio Flow for the Get Snapshot Operation](/mulesoft/current/en-us/google-pubsub-connector/1.2/_images/get-snapshot-flow.png)

### XML for This Example

Paste this code into the Studio XML editor to quickly load the flow for this example into your Mule
app:

```xml
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
      xmlns:pubsub="http://www.mulesoft.org/schema/mule/pubsub"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
                          http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
                          http://www.mulesoft.org/schema/mule/pubsub http://www.mulesoft.org/schema/mule/pubsub/current/mule-pubsub.xsd
                          http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">

    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config">
        <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>

    <pubsub:config name="Google_Pub_Sub_Configuration" doc:name="Google Pub Sub Configuration" projectId="${config.default_project_id}">
        <pubsub:connection privateKeyId="${config.private_key_id}" privateKey="${config.private_key}" clientId="${config.client_id}" clientEmail="${config.client_email}" />
    </pubsub:config>

    <configuration-properties doc:name="Configuration properties" file="mule-app.properties" />

    <flow name="get-snapshot-flow">
        <http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/getSnapshot"/>
        <pubsub:get-snapshot doc:name="Get snapshot" config-ref="Google_Pub_Sub_Configuration" snapshotName="#[attributes.queryParams.getsnapshot]"/>
        <logger level="INFO" doc:name="Logger" message="Received Snapshot Topic: #[payload.topic]"/>
        <ee:transform doc:name="Transform Message">
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
            </ee:message>
        </ee:transform>
    </flow>
</mule>
```

### Steps for Running This Example

1.  Verify that your connector is configured.

2.  Save the project.

3.  From a web browser, test the application by entering
    `[http://localhost:8081/](http://localhost:8081/)`. You should see the **Get Snapshot** form
    with a pre-populated **Snapshot Name** field.

    ![Get Snapshot Form](/mulesoft/current/en-us/google-pubsub-connector/1.2/_images/get-snapshot.png)

4.  Click **Get Snapshot**. You should see an alert that the snapshot was successfully retrieved.

## Delete a Snapshot

This Mule flow deletes an existing snapshot.

This example uses the following operations:

- **HTTP Listener**  
  Accepts data from HTTP requests

- **Delete snapshot**  
  Deletes a snapshot

![Studio Flow for the Delete Snapshot Operation](/mulesoft/current/en-us/google-pubsub-connector/1.2/_images/delete-snapshot-flow.png)

### XML for This Example

Paste this code into the Studio XML editor to quickly load the flow for this example into your Mule
app:

```xml
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
      xmlns:pubsub="http://www.mulesoft.org/schema/mule/pubsub"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
                          http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
                          http://www.mulesoft.org/schema/mule/pubsub http://www.mulesoft.org/schema/mule/pubsub/current/mule-pubsub.xsd
                          http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">

    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config">
        <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>

    <pubsub:config name="Google_Pub_Sub_Configuration" doc:name="Google Pub Sub Configuration" projectId="${config.default_project_id}">
        <pubsub:connection privateKeyId="${config.private_key_id}" privateKey="${config.private_key}" clientId="${config.client_id}" clientEmail="${config.client_email}" />
    </pubsub:config>

    <configuration-properties doc:name="Configuration properties" file="mule-app.properties" />

    <flow name="delete-snapshot-flow">
        <http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/deleteSnapshot"/>
        <pubsub:delete-snapshot doc:name="Delete snapshot" config-ref="Google_Pub_Sub_Configuration" snapshotName="#[attributes.queryParams.deletesnapshot]"/>
    </flow>
</mule>
```

### Steps for Running This Example

1.  Verify that your connector is configured.

2.  Save the project.

3.  From a web browser, test the application by entering
    `[http://localhost:8081/](http://localhost:8081/)`. You should see the **Delete Snapshot** form
    with a pre-populated **Snapshot Name** field.

    ![Delete Snapshot Form](/mulesoft/current/en-us/google-pubsub-connector/1.2/_images/delete-snapshot.png)

4.  Click **Delete Snapshot**. You should see an alert that the snapshot was successfully deleted.

## Publish a Message

This Mule flow publishes a single message to a selected topic.

This example uses the following operations:

- **HTTP Listener**  
  Accepts data from HTTP requests

- **Publish Message**  
  Publishes a message

![Studio Flow for the Publish Message Operation](/mulesoft/current/en-us/google-pubsub-connector/1.2/_images/publish-message-flow.png)

### XML for This Example

Paste this code into the Studio XML editor to quickly load the flow for this example into your Mule
app:

```xml
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
      xmlns:pubsub="http://www.mulesoft.org/schema/mule/pubsub"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
                          http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
                          http://www.mulesoft.org/schema/mule/pubsub http://www.mulesoft.org/schema/mule/pubsub/current/mule-pubsub.xsd
                          http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">

    <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config">
        <http:listener-connection host="0.0.0.0" port="8081" />
    </http:listener-config>

    <pubsub:config name="Google_Pub_Sub_Configuration" doc:name="Google Pub Sub Configuration" projectId="${config.default_project_id}">
        <pubsub:connection privateKeyId="${config.private_key_id}" privateKey="${config.private_key}" clientId="${config.client_id}" clientEmail="${config.client_email}" />
    </pubsub:config>

    <configuration-properties doc:name="Configuration properties" file="mule-app.properties" />

    <flow name="publish-message-flow">
        <http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/message"/>
        <pubsub:publish-message
                doc:name="Publish Message"
                config-ref="Google_Pub_Sub_Configuration"
                topicName="#[attributes.queryParams.topicname]"
                orderingKey="#[attributes.queryParams.orderkey]">
            <pubsub:message ><![CDATA[#[attributes.queryParams.message]]]></pubsub:message>
        </pubsub:publish-message>
    </flow>
</mule>
```

### Steps for Running This Example

1.  Verify that your connector is configured.

2.  Save the project.

3.  From a web browser, test the application by entering
    `[http://localhost:8081/](http://localhost:8081/)`. You should see the **Publish Message** form
    with pre-populated **Topic Name**, **Ordering Key**, and **Message** fields.

    ![Publish Message form](/mulesoft/current/en-us/google-pubsub-connector/1.2/_images/publish-message.png)

4.  Click **Publish Message**. You should see an alert that the snapshot was successfully published.

## See Also

- [Introduction to Anypoint Connectors](https://docs.mulesoft.com/connectors/introduction/introduction-to-anypoint-connectors)

- [Salesforce Help](https://help.salesforce.com)
