You need to sign in to do that
Don't have an account?
Prabhjot Singh 27
Issue in documentation in Emp API for LWC(Lightning Web Component))
While implementing emp API in LWC to subscribe the platform event I find out that there is small issue in sample code snipit given in documentation.
Issue is in handleSubscribe method in js below is the code given in documentation:
Issue is in handleSubscribe method in js below is the code given in documentation:
handleSubscribe() { // Callback invoked whenever a new event message is received const messageCallback = function(response) { console.log('New message received : ', JSON.stringify(response)); // Response contains the payload of the new message received }; // 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; this.toggleSubscribeButton(true); }); }Correction is required in messageCallback method which should be as below:
const messageCallback = (response) => { console.log('New message received : ', JSON.stringify(response)); this.payload = JSON.stringify(response); console.log('this.payload: ' + this.payload); // Response contains the payload of the new message received };We have to use arrow function as it does not have its own scope.
All Answers
Bellow is the code
When I click in a button that has onclick={handleSubscription} I only receive the console output "LOG".
One of the components is a Aura component that subscribes to a Platform Event 1. The Second component is a LWC that subscibes to a Platform Event 2. I removed the Aura from the page, then the button that subscribes to the Platform Event 2 worked wthout any changes.
https://github.com/salesforce/lwc/issues/1618
For now Im just not leaving both components on the same lightning page. I also did a test where I have two Aura components subscribing to two different Platform Events and they work perfectly like you said @Prabhjot Singh 27. The issue happens only when I have one Aura and one LWC.
Also, there is another weird behaviors happening. When I load a page which has an Aura component with empApi and then move to another lightning page that has a LWC using empApi, this page throws an exception. If I reload this lightning page the error is gone.
Scenario 1:
Place one Aura in a Lightning Page that subscribe to an Event A
Place one LWC in the same Lightning Page that subscribes to an Event B.
Result: The LWC subscribe will not work and it will also not thrown any exception if you put a .catch(error => { console.log(error); }) in the promisse.
Scenario 2:
Place one Aura in a Lightning Page A that subscribe to a Platform Event 1
Place one LWC in a Lightning Page B in the same Lightning APP that subscribe to a Platform Event 2
Go to Lightning Page A, wait the Aura to subscribe to the Platform Event 1
Click on the Lightning Page B and the error thrown will be the following
Reload the page with Ctrl + R and the error is gone and the LWC will subscribe to the Platform Event 2
Prabhjot Singh - You are a genious.. you helped complete my IOT project.. thank you
@Prabhjot - I was hoping your solution would solve the issue but I get the same error as @allan31
Salesforce is already aware of it and it is trying to understand.
To bad SF does not correct their documentation...