Salesforce Pub/Sub Connector 1.0 Examples | MuleSoft Documentation
# Salesforce Pub/Sub Connector 1.0 Examples
The following examples show several Mule flows for Salesforce Pub/Sub Connector:
  * Publish an Event
  * Subscribe to a Channel


## Before You Begin
These examples require:
  * Java 8, 11, or 17
  * Anypoint Studio 7.5 or later
  * Mule runtime engine (Mule) 4.3.0 or later
  * DataWeave


## Configure a Connection
To secure connections, you must specify the connection field values. To do this:
  * Create a configuration file for a connection.
  * Configure the connection global elements.
  * Configure a global element for the properties file.


### Create a Configuration File for a Connection
Create a configuration file that includes properties for a 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 the connection, similar to the ones that follow, replacing the bracketed text (including the brackets) with the correct values for your configuration:
```
m4-config.username=<username within Salesforce Pub/Sub system>
m4-config.password=<password for the username within Salesforce Pub/Sub system>
m4-config.securityToken=<security token within Salesforce Pub/Sub system>
m4-config.url=<endpoint representing your development environment>
```

The properties used vary depending on the selected connection configuration.


For more information about creating a properties file, refer to Configuring Property Placeholders.
### Configure the Connection Global Elements
Configure global elements for connection:
  1. Create a new Mule project.
  2. In the **Mule Palette** view, click **Search in Exchange** and enter `Salesforce Pub/Sub`.
  3. Add **Salesforce Pub/Sub Connector** to the **Selected modules** section and click **Finish**.
  4. Click the **Global Elements** tab and click **Create**.
  5. Select **Connector Configuration > Salesforce Pub/Sub Config** and click **OK**.
  6. Enter the values to configure either Basic Authentication, OAuth v2.0, OAuth JWT, OAuth Username Password, or OAuth SAML.
  7. Click the **Test Connection** button to ensure there is connectivity with the Salesforce Pub/Sub API. A successful message should pop up.
  8. Click **OK**.
  9. Open the HTTPS Listener config in **Global Element Configuration**.
  10. Click the **TLS** tab and select **TLS Configuration > Edit inline**.
  11. Specify the **Key Store Configuration** with the generated keystore details to enable HTTPS on this configuration.
  12. 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**.


## Publish an Event
This example Mule flow publishes an event and uses the following operations:
  * **HTTP Listener** Accepts data from HTTP requests.
  * **Transform Message** Outputs the data in Java.
  * **Publish event** Publishes the given list of events to the specified event topic. Only high-volume platform events, including real-time event monitoring events and change data capture events, are allowed.
Enter the following values:
Name of the topic used for message publishing, for example, `/event/gRPCMunitTestDoNotDelete__e`  
---  


### 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 version="1.0" encoding="UTF-8"?>
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
	 xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:salesforce-pub-sub="http://www.mulesoft.org/schema/mule/salesforce-pub-sub" 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/salesforce-pub-sub http://www.mulesoft.org/schema/mule/salesforce-pub-sub/current/mule-salesforce-pub-sub.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/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
	<configuration-properties file="mule-app.properties"/>
	<salesforce-pub-sub:pubsub-config name="Salesforce_PubSub_Config" >
		<salesforce-pub-sub:basic-connection username="${m4-config.username}" password="${m4-config.password}" securityToken="${m4-config.securityToken}" url="${m4-config.url}"/>
	</salesforce-pub-sub:pubsub-config>
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="3805c5f1-0f01-476a-a619-a03f34dadfdd" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
<flow name="mule-salesforce-pubsub-connector-publish-event-demo" doc:id="9e2df8f5-1b47-4a76-ac62-941c989cd93b" >
 <http:listener doc:name="Listener" doc:id="6e06a214-3112-40ef-8720-f730121aa5e0" config-ref="HTTP_Listener_config" path="/publish-event"/>
 <ee:transform>
  <ee:message>
   <ee:set-payload><![CDATA[%dw 2.0
    output application/java
    ---
    [{
     CreatedDate: 12334455,
     CreatedById: "123",
     Desc__c: "Something"
    },{
     CreatedDate: 12334456,
     CreatedById: "1234",
     Desc__c: "Something2"
    }
    ]]]>
   </ee:set-payload>
  </ee:message>
 </ee:transform>
 <salesforce-pub-sub:publish-event config-ref="Salesforce_PubSub_Config" topic="/event/gRPCMunitTestDoNotDelete__e"/>
</flow>
</mule>
```

## Subscribe to a Channel
This example Mule flow subscribes to a channel and uses the following operations:
  * **Subscribe channel listener** Subscribes to a streaming channel. This source provides channel-related notification settings for new events that occur after you subscribe.
A channel must be published to Salesforce before a subscription to the channel is created.   
---  
Enter the following values:
**Channel name** |  Name of the streaming channel to subscribe to, for example, `/event/gRPCMunitTestDoNotDelete__e`  
---|---  
**Replay option** |  One of the given implementations, for example, `Earliest`. For more information on how to use `Replay id from object store` in **Replay option** , refer to Store the Replay ID in the Object Store.  
**Batch events size** |  Total number of events included in a server batch. Lower values indicate small memory footprint with more server calls, while bigger values indicate bigger memory footprint with fewer API calls. A new batch of events is requested by the connector after the flow consumes the existing events, for example, `1000`  
  * **Logger** Shows the HTTP response from the **Subscribe channel listener** source
## Subscribe to a Channel
This example Mule flow subscribes to a channel and uses the following operations:
  * **Subscribe channel listener** Subscribes to a streaming channel. This source provides channel-related notification settings for new events that occur after you subscribe.
A channel must be published to Salesforce before a subscription to the channel is created.   
---  
Enter the following values:
**Channel name** |  Name of the streaming channel to subscribe to, for example, `/event/gRPCMunitTestDoNotDelete__e`  
---|---  
**Replay option** |  One of the given implementations, for example, `Earliest`. For more information on how to use `Replay id from object store` in **Replay option** , refer to Store the Replay ID in the Object Store.  
**Batch events size** |  Total number of events included in a server batch. Lower values indicate small memory footprint with more server calls, while bigger values indicate bigger memory footprint with fewer API calls. A new batch of events is requested by the connector after the flow consumes the existing events, for example, `1000`  
  * **Logger** Shows the HTTP response from the **Subscribe channel listener** source


### 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 version="1.0" encoding="UTF-8"?>
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
	 xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:salesforce-pub-sub="http://www.mulesoft.org/schema/mule/salesforce-pub-sub" 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/salesforce-pub-sub http://www.mulesoft.org/schema/mule/salesforce-pub-sub/current/mule-salesforce-pub-sub.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/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
	<configuration-properties file="mule-app.properties"/>
	<salesforce-pub-sub:pubsub-config name="Salesforce_PubSub_Config" >
		<salesforce-pub-sub:basic-connection username="${m4-config.username}" password="${m4-config.password}" securityToken="${m4-config.securityToken}" url="${m4-config.url}"/>
	</salesforce-pub-sub:pubsub-config>
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="3805c5f1-0f01-476a-a619-a03f34dadfdd" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<flow name="mule-salesforce-pubsub-connector-subscribe-channel-demo" doc:id="4faf2bf8-e2af-43bc-bba8-d336761800dd" >
		<salesforce-pub-sub:subscribe-channel-listener channelName="/event/gRPCMunitTestDoNotDelete__e" config-ref="Salesforce_PubSub_Config" eventBatchSize="1000">
			<salesforce-pub-sub:replay-option >
				<salesforce-pub-sub:earliest />
			</salesforce-pub-sub:replay-option>
		</salesforce-pub-sub:subscribe-channel-listener>
		<logger level="INFO" doc:name="Logger" doc:id="21652e14-de84-4752-a9b9-871dc346befb" message="#[payload]"/>
	</flow>
</mule>
```

## See Also
  * Introduction to Anypoint Connectors
  * MuleSoft Help Center