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.
  1. 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.
  2. In Step 3: Configure Client Parameters, supply the configuration parameters in arguments.yaml. Also, make sure you supply this value:
    TOPIC: /event/Order_Channel__chn
  3. In a Terminal window, navigate to the top-level java folder.
  4. To run the Subscribe RPC example, enter:./run.sh genericpubsub.Subscribe.
  5. After subscribing to the filtered channel, publish an Order_NorthAmer__e event and an Order_EMEA__e event.
    1. Open another Terminal window.
    2. Navigate to your local pub-sub-api folder.
    3. In an IDE, such as Visual Studio Code, open java/src/main/java/utility/CommonContext.java.
    4. Modify the createEventMessage method that takes one parameter. Replace the method with this snippet. Replace the CreatedById value with your Salesforce user ID. See Find the Salesforce ID for a User or Profile.
      public GenericRecord createEventMessage(Schema schema) {
          // Update CreatedById with the appropriate User Id from your org.
          return new GenericRecordBuilder(schema)
                  .set("CreatedDate", System.currentTimeMillis())
                  .set("CreatedById", "<User_Id>")
                  .set("Order_Number__c", "1")
                  .set("City__c", "Los Angeles")
                  .set("Amount__c", 35.0).build();
      }
    5. For the configuration parameters in java/src/main/resources/arguments.yaml, supply this value.
      TOPIC: /event/Order_NorthAmer__e
    6. Build the client from the top-level java folder with this command: mvn clean install.
    7. To run the Publish RPC example from the java folder, enter ./run.sh genericpubsub.Publish.
    8. Repeat the previous steps to publish an Order_EMEA__e event. Both events have the same fields, so modify the field values in the createEventMessage method in CommonContext.java.
      .set("Order_Number__c", "2")
                  .set("City__c", "London")
                  .set("Amount__c", 20.0).build();
    9. For the configuration parameters in arguments.yaml, supply this value.
      TOPIC: Order_EMEA__e
    10. Build the client from the top-level java folder with this command: mvn clean install.
    11. To run the Publish RPC example from the java folder, enter ./run.sh genericpubsub.Publish.

    Because Order_Channel__chn includes 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.

    2024-03-08 16:58:09,730 [pool-3-thread-2] 
    java.lang.Class - Received event with payload: 
    {
      "CreatedDate": 1711497250504,
      "CreatedById": "0055f000005mc66AAA",
      "Order_Number__c": "1",
      "City__c": "Los Angeles",
      "Amount__c": 35.0
    } 
    with schema name: Order_NorthAmer__e
    
    2024-03-08 16:58:09,730 [pool-3-thread-1] 
    java.lang.Class - Received event with payload: 
    {
      "CreatedDate": 1711497484289,
      "CreatedById": "0055f000005mc66AAA",
      "Order_Number__c": "2",
      "City__c": "London",
      "Amount__c": 20.0
    } 
    with schema name: Order_EMEA__e
    If you use a Streaming API (CometD) to subscribe to the channel, the event messages contain the EventApiName as seen in this example.
    {
      "schema": "LofZQqy_2SpDbzzZptVpxQ",
      "payload": {
        "City__c": "Los Angeles",
        "CreatedById": "0055f000005mc66AAA",
        "Amount__c": 35.0,
        "CreatedDate": "2024-03-09T00:27:54.115Z",
        "Order_Number__c": "1"
      },
      "event": {
        "EventUuid": "7fffe7b9-106f-4708-bd8d-00a33aa6ab84",
        "replayId": 12319972,
        "EventApiName": "Order_NorthAmer__e"
      }
    }
    
    {
      "schema": "LofZQqy_2SpDbzzZptVpxQ",
      "payload": {
        "City__c": "London",
        "CreatedById": "0055f000005mc66AAA",
        "Amount__c": 20.0,
        "CreatedDate": "2024-03-09T00:27:54.087Z",
        "Order_Number__c": "2"
      },
      "event": {
        "EventUuid": "4a502e4c-8cc4-4a6e-818f-45f91efac9a4",
        "replayId": 12319973,
        "EventApiName": "Order_EMEA__e"
      }
    }