1

Every other time I attempt to use the lightning:empApi component to subscribe to a Push Topic channel the EMP API component reports error '403::Unknown Client'

After the EMP API component reports the error no event messages are received for a couple minutes. After a couple minutes the component starts receiving messages.

Here is the faulty code. The subscription occurs in the postInitializeCallback.

/**
 * Gets EMP API cmp ready for event subscriptions. After successful initialization subscribes to error notifications
 * and executes the given callback.
 *
 * If already initialized then just executes given callback.
 *
 * @param cmp
 * @param postInitializeCallback
 */
initializeEmpApi: function(cmp, postInitializeCallback) {
    console.log('initializing EMP API');
    if (this.empApiInitialized) {
        console.log('EMP API already initialized');
        postInitializeCallback(cmp);
    } else {
        // Get the empApi cmp
        var empApi = cmp.find('scannerEmpApi');

        if (!empApi.isEmpEnabled()) {
            alert('cmp must be hosted in the Salesforce One app in order to listen to platform events');
            return;
        }
        empApi.setDebugFlag(true);

        var onErrorCallback = function(error) {
            // NOTE: Salesforce throws 403::Unknown Client on subscribe but the subscription will start working after a few minutes
            console.error('EMP API message: ' + JSON.stringify(error));
            // if (error.error === '403::Unknown client') {
            //  var helperRef = this;
            //  window.setTimeout(function() {
            //      helperRef.updateAccessControlFeedSubscription(cmp);
            //  }, 1000);
            // }
        }.bind(this);
        empApi.onError(onErrorCallback);

        // Initialize the push topic that we'll subscribe to
        var helperRef = this;
        helperRef.createControllerRequest(cmp, 'initializePushTopic', {},
            function(jsonBody) {
                console.log('push topics initialized');
                helperRef.empApiInitialized = true;
                cmp.set('v.pushTopicName', jsonBody.regularPushTopicName);
                cmp.set('v.errorPushTopicName', jsonBody.errorPushTopicName);
                // helperRef.subscribeToScanningErrors(cmp);
                postInitializeCallback(cmp);
            }
        );
    }
},

1 Answer 1

1

The problem in helper method posted is that it uses the isEmpEnabled function incorrectly.

The isEmpEnabled function returns a promise not a boolean. If this is fixed so that isEmpEnabled is called correctly then waiting for a successful response and subscribing USUALLY does not result in a 403 error, but it still happens occasionally on the first load of the page.

As far as I can tell it is actually required to call isEmpEnabled prior to requesting the component subscribe to any channels. If the isEmpEnabled is not called any subscription call is likely to result in the 403::Unknown client error. (note that the official usage example does not include usage of isEmpEnabled)

The corrected code is below.

/**
 * Gets EMP API cmp ready for event subscriptions. After successful initialization subscribes to error notifications
 * and executes the given callback.
 *
 * If already initialized then just executes given callback.
 *
 * @param cmp
 * @param postInitializeCallback
 */
initializeEmpApi: function(cmp, postInitializeCallback) {
    console.log('initializing EMP API');
    if (this.empApiInitialized) {
        console.log('EMP API already initialized');
        postInitializeCallback(cmp);
    } else {
        // Get the empApi cmp
        var empApi = cmp.find('scannerEmpApi');

        var helperRef = this;
        empApi.isEmpEnabled().then(function(enabled) {
            if (!enabled) {
                alert('cmp must be hosted in the Salesforce One app in order to listen to platform events');
                return;
            }
            console.log('EMP API is enabled');
            empApi.setDebugFlag(true);

            var onErrorCallback = function(error) {
                console.error('EMP API message: ' + JSON.stringify(error));
            }.bind(this);
            empApi.onError(onErrorCallback);

            // Initialize the push topic that we'll subscribe to
            helperRef.createControllerRequest(cmp, 'initializePushTopic', {},
                function(jsonBody) {
                    console.log('push topics initialized');
                    helperRef.empApiInitialized = true;
                    cmp.set('v.pushTopicName', jsonBody.regularPushTopicName);
                    cmp.set('v.errorPushTopicName', jsonBody.errorPushTopicName);
                    // helperRef.subscribeToScanningErrors(cmp);
                    postInitializeCallback(cmp);
                }
            );
        });

    }
},

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .