6

I am using platform event and subscribing them in a lightning component. When i use my component as standalone lightning app or use it on lightning record page, the components subscribe well to the event.

// Client-side function that invokes the subscribe method on the empApi component.
subscribe: function (component, event, helper) {
    // Get the empApi component.
    console.log('calling to subscribe');
    debugger;
    var empApi = component.find('empApi');
    // Get the channel from the attribute.
    var channel = component.get('v.channel');
    // Subscription option to get only new events.
    var replayId = -1;
    // Callback function to be passed in the subscribe call.
    // After an event is received, this callback prints the event
    // payload to the console. A helper method displays the message
    // in the console app.
    var subscribeCallback = function (message) {
        console.log('Event Received : ' + JSON.stringify(message));
        helper.onReceiveNotification(component, message);
    };
    // Subscribe to the channel and save the returned subscription object.
    empApi.subscribe(channel, replayId, subscribeCallback).then($A.getCallback(function (newSubscription) {
        console.log('Subscribed to channel ' + channel);
        component.set('v.subscription', newSubscription);
    }));

    /*.catch(
        function(error) {
            console.error('cant subscribe to event ', JSON.stringify(error));
        }
    );*/
},

In the subscribe method - there is a console log - Subscribed to channel.

empApi.subscribe(channel, replayId, subscribeCallback).then($A.getCallback(function (newSubscription) {
    console.log('Subscribed to channel ' + channel);
    component.set('v.subscription', newSubscription);
}));

It works fine for above mentioned scenarios and gets called well from Init.

If i put the component on the community page, the component stops subscribing and 'Subscribed to channel' does not show.

  • Is there an alternative? A way to subscribe to PE on a community page? – Julian May 26 '19 at 23:31
  • Nope! haven't found it yet! If i see console logs, i see that the component will subscribe to events in community but will not get any notifications! Don't know if things have changed! – Anshul Agrawal May 27 '19 at 9:02
8

Per documentation, lightning:empApi is not supported in Communities. If you refer to the Experience section in the documentation, the only supported experiences are LEX and Mobile.

Refer screenshot from the documentation:

enter image description here

| improve this answer | |
  • 1
    As of Winter'21 (21/10/2020) per the updated documentation, developer.salesforce.com/docs/component-library/bundle/… the Salesforce Mobile App is removed and only Lightning Experience is supported – Swetha Oct 21 at 12:48

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.