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?

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.