Subscribe to the Channel and Receive the Filtered Event Stream

After configuring the filter, subscribe to the channel and receive the event messages that match the filter expression. Only Pub/Sub API and CometD clients support stream filtering. Because Apex triggers, flows, and processes don’t support custom channels, you can’t use them to subscribe to filtered event streams.

Before You Begin 

Before subscribing to the channel, follow the steps in the previous sections to create the MyChannel__chn channel, and configure a filter expression for Order_Event__e with Tooling API or Metadata API.

Note

In this example, we use a Pub/Sub API Java client sample. See Java Quick Start for Publishing and Subscribing to Events in the Pub/Sub API Developer Guide. If you prefer to use a tool in the Salesforce UI, you can use the empApi Lightning component or the Streaming Monitor app from AppExchange.

  1. Set up the Pub/Sub API Java client and subscribe to the channel.

    1. 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, for the TOPIC argument, provide the custom channel that you created: /event/MyChannel__chn. The channel name format is /event/ChannelName__chn.

    3. From the java folder, run: ./run.sh genericpubsub.Subscribe.

  2. Now that you’re subscribed to the custom channel, publish event messages.

    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 createEventMessages method. Replace the code with this snippet. Replace the CreatedById value with your Salesforce user ID. See Find the Salesforce ID for a User or Profile.

      1public List<GenericRecord> createEventMessages(Schema schema, final int numEvents)
      2{
      3
      4    String[] orderNumbers = {"99","100","101","102","103"};
      5    String[] cities = {"Los Angeles", "New York", "San Francisco", "San Jose", 
      6                       "Boston"};
      7    Double[] amounts = {35.0, 20.0, 2.0, 123.0, 180.0};
      8
      9    // Update CreatedById with the appropriate User Id from your org.
      10    List<GenericRecord> events = new ArrayList<>();
      11    for (int i=0; i<numEvents; i++) {
      12        events.add(new GenericRecordBuilder(schema)
      13                .set("CreatedDate", System.currentTimeMillis())
      14                .set("CreatedById", "<User_Id>")
      15                .set("Order_Number__c", orderNumbers[i % 5])
      16                .set("City__c", cities[i % 5])
      17                .set("Amount__c", amounts[i % 5]).build());
      18    }
      19
      20    return events;
      21}
    5. For the configuration parameters in java/src/main/resources/arguments.yaml, supply these values.

      • TOPIC: /event/Order_Event__e
      • NUMBER_OF_EVENTS_TO_PUBLISH: 5
      • SINGLE_PUBLISH_REQUEST: true
    6. Build the client from the top-level java folder with this command: mvn clean install.

    7. To run the PublishStream RPC example and publish five event messages from the java folder, enter ./run.sh genericpubsub.PublishStream.

      As a refresher, here’s the filter expression that we set in the previous section.

      1(City__c LIKE 'S%' OR City__c='New York') AND Amount__c>10.50

      From the event messages published, only the second and fourth event messages match the filter criteria given in the previous example and are delivered to the client. The other event messages only partially match the criteria, so they aren’t delivered to the client.

      This example shows the two event messages received after subscribing to the filtered channel, /event/MyChannel__chn.

      12024-03-26 17:04:38,347 [pool-3-thread-1] 
      2java.lang.Class - Received event with payload: 
      3{
      4  "CreatedDate": 1711497876867,
      5  "CreatedById": "0055f000005mc66AAA",
      6  "Order_Number__c": "100",
      7  "City__c": "New York",
      8  "Amount__c": 20.0
      9}
      10with schema name: Order_Event__e
      11
      122024-03-26 17:04:38,347 [pool-3-thread-2] 
      13java.lang.Class - Received event with payload: 
      14{
      15  "CreatedDate": 1711497876867,
      16  "CreatedById": "0055f000005mc66AAA",
      17  "Order_Number__c": "102",
      18  "City__c": "San Jose",
      19  "Amount__c": 123.0
      20}
      21with schema name: Order_Event__e