Subscribe to the Channel
After creating a custom channel and its members, subscribe to the channel using a Pub/Sub API Java client, and receive event messages.
In this example, you subscribe to the Order_Channel__chn channel you created for two custom platform events. Only Pub/Sub API and CometD clients support custom channels. Other subscribers, such as Apex triggers, flows, and processes, don’t support custom channels.
-
To set up the Pub/Sub API Java client, follow the steps in Java Quick Start for Publishing and Subscribing to Events in the Pub/Sub API Developer Guide.
-
In Step 3: Configure Client Parameters, supply the configuration parameters in arguments.yaml. Also, make sure you supply this value:
-
In a Terminal window, navigate to the top-level
javafolder. -
To run the Subscribe RPC example, enter:
./run.sh genericpubsub.Subscribe. -
After subscribing to the filtered channel, publish an Order_NorthAmer__e event and an Order_EMEA__e event.
-
Open another Terminal window.
-
Navigate to your local
pub-sub-apifolder. -
In an IDE, such as Visual Studio Code, open
java/src/main/java/utility/CommonContext.java. -
Modify the
createEventMessagemethod that takes one parameter. Replace the method with this snippet. Replace theCreatedByIdvalue with your Salesforce user ID. See Find the Salesforce ID for a User or Profile. -
For the configuration parameters in
java/src/main/resources/arguments.yaml, supply this value. -
Build the client from the top-level java folder with this command:
mvn clean install. -
To run the Publish RPC example from the java folder, enter
./run.sh genericpubsub.Publish. -
Repeat the previous steps to publish an Order_EMEA__e event. Both events have the same fields, so modify the field values in the
createEventMessagemethod inCommonContext.java. -
For the configuration parameters in arguments.yaml, supply this value.
-
Build the client from the top-level java folder with this command:
mvn clean install. -
To run the Publish RPC example from the java folder, enter
./run.sh genericpubsub.Publish.
Because
Order_Channel__chnincludes both the Order_NorthAmer__e and Order_EMEA__e event types, you receive the event messages of both events in the terminal where your subscriber client is running. This example shows the two received event messages after subscribing to the filtered channel,/event/Order_Channel__chn. The Pub/Sub API client output includes the event name, which is obtained from the schema on the received event.If you use a Streaming API (CometD) to subscribe to the channel, the event messages contain the
EventApiNameas seen in this example. -