A platform event is publishing through an apex trigger and I am trying to subscribe to it using a lightning component, but the lightning component is failing to subscribe every time. It is subscribing randomly and sometimes it is giving the error below:

event {"advice"{"interval":0,"reconnect":"handshake"},"channel":"/meta/subscribe","id":"6","error":"403::Unknown client","successful":false}

Here is the subscription code from the lightning component.

 subscribeEvent:function(component, event, helper) {

    const empApi = component.find('empApi');
    const channel = component.get('v.channel');
    console.log('Event Received after subscribe: ');
    var recordid = component.get("v.entityId");

    var sub;
    var replayId = -1;
    const callback = function (message) {
        console.log('Event Received after subscribe: ' + JSON.stringify(message));
        console.log('Event Received after subscribe recordid: ' + recordid);

        if(recordid == message.data.payload.Reference_Id__c){
            if(message.data.payload.Status__c == 'Success'){
                console.log('Event Received paymentPhase receipt');

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

enter image description here


Although not a direct solution, I have also had similar issues with the 403::Unknown Client error. What I noticed with the seemingly inconsistent subscriptions was that the error would only occur when the subscription tried to connect before the page was finished loading. I found that by stalling the subscription for a few seconds, the page loaded and the code worked as expected.

window.setTimeout($A.getCallback(function() {
    //subcription here
}), 3000); //Waits for 3 seconds

This prevents the handshake from mostly failing, prior to the connection being established. I will also point out that this is not a fully working solution, as at times the connection still fails (regardless of time), but is fixed with a refresh of the page once or twice. I also added alerts to pop up indicating successful and failed connection attempts that I could share if necessary.

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.