3

I'm trying to subscribe to multiple channels with CometD. I'm using the CometD static resource and syntax as per the Trailhead project: https://trailhead.salesforce.com/projects/workshop-platform-events/steps/platform-event-subscribe

When I update the subscription code in the helper to the following, the first channel subscription is not returning any events - looks like it's being overwritten by the second one.

    var newSubscription = cometd.subscribe('/event/Notification__e',
      function(platformEvent) {
        console.log('Platform event received: '+ JSON.stringify(platformEvent));
        //helper.onReceiveNotification(component, platformEvent);
    });
    var newSubscription2 = cometd.subscribe('/event/AnotherPlatformEvent__e',
      function (message) {
        console.log("Second event was heard!!");
    });
    // Save subscription for later
    var subscriptions = component.get('v.cometdSubscriptions');
    subscriptions.push(newSubscription);
    subscriptions.push(newSubscription2);
    component.set('v.cometdSubscriptions', subscriptions);

CometD is supposed to support Bulk Subscriptions as per https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/using_streaming_api_bulk.htm - I tried adding my own method into the cometd.js static resource to create the Bayeux JSON message in this format, but got lost down the JS rabbit hole.

Has anyone tried to subscribe to multiple PushTopics / Platform Events in the same Lightning Component? How did you manage it?

2
  • 3
    In Winter '19, the new lightning:empApi should take care of this for you.
    – sfdcfox
    Commented Aug 29, 2018 at 13:19
  • @sfdcfox Thanks! My sandbox is updating to Winter '19 in the next two weeks so I'll try it out :) Commented Aug 31, 2018 at 10:51

1 Answer 1

4

As @sfdcfox mentioned in the comments, the new lightning:empApi component can be used to achieve multiple streaming connections in a single component or browser window. I got this working with multiple lightining:empApi tags in a single component, using unique aura:Ids to access their subscribe methods through the JS controller and helper.

You can access the specification for this component on the current Winter '19 preview sandboxes through the following link: https://[YOUR-DOMAIN].lightning.force.com/auradocs/reference.app#reference?descriptor=lightning:empApi&defType=component

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .