Newer Version Available

This content describes an older version of this product. View Latest

Subscribe to the Channel

After creating the custom channel and its members, subscribe to the channel using a CometD client, and receive event messages. Only CometD clients support custom channels. Other subscribers, such as Apex triggers, flows, and processes, don’t support custom channels.
  1. To set up EMP Connector, follow the steps in Example: Subscribe to Events Using a CometD Client (EMP Connector). For the channel argument, provide the custom channel that you created: /event/Order_Channel__chn. The channel name is format /event/ChannelName__chn.

    To receive the EventApiName field, ensure that the client is subscribed with the Streaming API endpoint of API version 55.0 or later. See API Version in the Streaming API Endpoint in the EMP Connector GitHub repo.

    Note

  2. Now that you’re subscribed to the custom channel, publish event messages of both events, Order_NorthAmer__e and Order_EMEA__e, in the Developer Console using Apex.
    1. In Salesforce Classic, select your name | Developer Console.
    2. In Lightning Experience, click the quick access menu, and select Developer Console.
    3. Select Debug | Open Execute Anonymous Window.
    4. In the new window, replace the contents with this Apex code snippet, and click Execute.
      1// List to hold event objects to be published. 
      2List<SObject> eventList = new List<SObject>(); 
      3// Create event objects. 
      4Order_NorthAmer__e event1 = new Order_NorthAmer__e(
      5    Order_Number__c='1', City__c='Los Angeles', Amount__c=35); 
      6Order_EMEA__e event2 = new Order_EMEA__e(
      7    Order_Number__c='2', City__c='London', Amount__c=20); 
      8// Add event objects to the list. 
      9eventList.add(event1); 
      10eventList.add(event2); 
      11// Call method to publish events. 
      12List<Database.SaveResult> results = EventBus.publish(eventList); 
      13// Inspect publishing result for each event 
      14for (Database.SaveResult sr : results) { 
      15    if (sr.isSuccess()) { 
      16        System.debug('Successfully published event.'); 
      17    } 
      18    else { 
      19        for(Database.Error err : sr.getErrors()) { 
      20                 System.debug('Error returned: ' + err.getStatusCode() + 
      21                              ' - ' +  err.getMessage()); 
      22        } 
      23    } 
      24}

      Notice that the eventList variable is declared with the SObject type and not the event type (for example, Order_NorthAmer__e). Using SObject enables this list variable to hold any event type, for example, Order_NorthAmer__e and Order_EMEA__e.

      Note

    Because Order_Channel__chn includes both the Order_NorthAmer__e and Order_EMEA__e event types, you receive the event messages of both events. This example shows the two received event messages after subscribing to the filtered channel, /event/Order_Channel__chn.
    1{
    2  "schema": "LofZQqy_2SpDbzzZptVpxQ",
    3  "payload": {
    4    "City__c": "Los Angeles",
    5    "CreatedById": "005RM000002Qu16YAC",
    6    "Amount__c": 35,
    7    "CreatedDate": "2022-03-29T13:45:19.141Z",
    8    "Order_Number__c": "1"
    9  },
    10  "event": {
    11    "EventApiName": "Order_NorthAmer__e",
    12    "EventUuid": "51de9c1f-3de0-48f8-b107-74fb1b377340",
    13    "replayId": 10305
    14  }
    15}
    16
    17{
    18  "schema": "e8jMOnID4xDThlaPBMx5gg",
    19  "payload": {
    20    "City__c": "London",
    21    "CreatedById": "005RM000002Qu16YAC",
    22    "Amount__c": 20,
    23    "CreatedDate": "2022-03-29T13:45:19.230Z",
    24    "Order_Number__c": "2"
    25  },
    26  "event": {
    27    "EventApiName": "Order_EMEA__e",
    28    "EventUuid": "218544ad-0472-4315-970f-8825a2802de6",
    29    "replayId": 10306
    30  }
    31}