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);
}
);
}
},