[SalesForce] How to Subscribe to platform Events on Community

Trying to subscribe to a platform Event channel from community keep failing for me.

All is working fine from internal User on a Lightning Record Page but for some reason cannot subscribe to the Channel from Community.

Am I missing something ? anything needed to add here to make it work on community?

Using LWC but no luck using Aura as well.
Got the Platform Event Setup "TestEvent__e".

import { LightningElement,track, api} from 'lwc';
import { subscribe, onError } from 'lightning/empApi';

export default class TestComp extends LightningElement {
 channelName = '/event/TestEvent__e';
 subscription = {};

 handleSubscribe() {
    // Callback invoked whenever a new event message is received
    const messageCallback = (response) => {
    console.log('Platform Event received : ', JSON.stringify(response,null,' '));     
    };

    // Invoke subscribe method of empApi. Pass reference to messageCallback
    subscribe(this.channelName, -1, messageCallback).then(response => {
        // Response contains the subscription information on successful subscribe call
        console.log('Successfully subscribed to : ', JSON.stringify(response.channel));
        this.subscription = response; 
    });   
}
registerErrorListener() {
    // Invoke onError empApi method
    onError(error => {
        console.log('Received error from server: ', JSON.stringify(error));
        // Error contains the server-side error
    });
}}

HTML

<lightning-button variant="success" label="Subscribe" title="Subscribe"
    onclick={handleSubscribe} class="slds-m-left_x-small">
</lightning-button>

XML

`<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>47.0</apiVersion>
    <isExposed>true</isExposed>
    <masterLabel>My Test Component</masterLabel>
    <description>Listen to EmpAPI</description>
     <targets>
        <target>lightningCommunity__Page</target>
        <target>lightningCommunity__Default</target>
    </targets>
</LightningComponentBundle>`

Best Answer

Unfortunately, you can't do it.

Because Community Users don't have access to Push Topics (idea), related question. And Platform Event, Data Change Event are built on Push Topics.

I have a similar use case where I need to use functionality in both Community and Internal, so I had to use different implementations based on the running environment.

UPDATE: actually you can answer

Related Topic