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.