lwc icon indicating copy to clipboard operation
lwc copied to clipboard

LWC EmpApi does not work when there is an Aura component using EmpApi

Open AllanOricil opened this issue 4 years ago • 33 comments

Description

LWC can't subscribe to a Platform Event if there is an Aura component that is subscribing to a Platform event in the same Lightning Page.

Steps to Reproduce

Place an Aura component that subscribes to a Platform Event 1 and a LWC that subscribes to a Platform Event 2 in a Lightning Page. The Aura component subscribed without any problem. The LWC did not subscribe and also did not display any error message or exception. Once I removed the Aura component from the Lightning Page, saved it, and reloaded it, the LWC subscribed to the Platform Event. Bear in mind I did not change anything in the LWC. I just removed the Aura component from the Lightning Page.

I followed the instructions to build the LWC component from here: https://developer.salesforce.com/docs/component-library/bundle/lightning-emp-api/documentation

And I made the correction to my component that is described here: https://developer.salesforce.com/forums/ForumsMain?id=9062I000000Xm9eQAC

Again, the LWC code is working! but only if I remove the Aura component from the Lightning Page.

Expected Results

LWC should subscribe to the Platform Event

Actual Results

LWC does not subscribe to the Platform Event and also does not show an errror message or exception

AllanOricil avatar Nov 16 '19 13:11 AllanOricil

To solve my issue for now I just put my LWC into another Lightning Page. It works but an odd behavior happens.

When I load a page which has an Aura component that subscribes to a Platform Event using empApi and then move to another lightning page that has a LWC using empApi, this page throws an exception. If I reload this last lightning page the error is gone.

This is the exception that I have: image

AllanOricil avatar Nov 16 '19 16:11 AllanOricil

This was once happening with me and the reason for this was that upon the navigation the sequence of lifecycle events were not coordinated in the navigation logic. I used state to capture current and cleared the previous states altogether. If your component has state before the navigation I recommend to set it to the conducive state for the next component(page).

The other thing which might be at play here is that you are going from an Aura component to LWC and if any of the event is set to bubble true then that might also be causing the call back within smartstore.customer bucket (Parent assumed hereas its ) that is probably due to a handled event down the container hierarchy not stopped explicitly by event.stopPropagation()

On Sat, Nov 16, 2019 at 10:18 PM AllanOricil [email protected] wrote:

To solve my issue for now I just put my LWC into another Lightning Page. It works but an odd behavior happens.

When I load a page which has an Aura component that subscribes to a Platform Event using empApi and then move to another lightning page that has a LWC using empApi, this page throws an exception. If I reload this last lightning page the error is gone.

This is the exception that I have: [image: image] https://user-images.githubusercontent.com/55927613/68996355-bdbfd500-0890-11ea-987b-dbd775f4faf3.png

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/salesforce/lwc/issues/1618?email_source=notifications&email_token=ABRIU6FWRCHPDOJDRCQO3STQUAP4BA5CNFSM4JOFDGJKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEEHVQVQ#issuecomment-554653782, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABRIU6CFKF2NYURL6P5OXJ3QUAP4BANCNFSM4JOFDGJA .

-- Regards Mitesh Sharma

miteshmsharma avatar Nov 17 '19 04:11 miteshmsharma

The components don't not communicate with each other! They are completly separated and both subscribe to different platform events. And both work when they are in separated Lightning Pages. But if I switch from a Lightning page that has an Aura to a Lightning page that has the LWC it throws that above exception. Then if I reload the Lightning Page with the LWC the error is just gone...

My workaround was to transform my Aura to a LWC and everything worked perfectly. So my conclusion is, don't mix Aura and LWC components in a Lightning Page when they are both subscribing to Platform Events. This seems to be a bug.

AllanOricil avatar Nov 17 '19 10:11 AllanOricil

We're having this exact same issue in console. Our steps to reproduce:

  1. Open a console app
  2. Open a console main tab that has a non-namespaced Aura component that subscribes to the empApi when it initializes
  3. Open a separate console main tab that has a namespaced LWC component that subscribes to the empApi when a button is clicked
  4. Click the button in the namespaced LWC component that subscribes to the empApi
  5. Receive the following exception:
Method Promise.prototype.then called on incompatible receiver [object Object]

cc @cdelmoral @adamsheets89 @yippie

joeferraro avatar Apr 06 '20 18:04 joeferraro

By looking at the error message, this seems like a problem with proxies. Promise.prototype.then expects a branded promise object, but it is receiving a proxy of it most likely, which is not branded. This could be locker service doing the wrong thing.

caridy avatar Apr 06 '20 21:04 caridy

I have a suspicion this may have something to do with namespaces (at least in relation to the steps to reproduce I laid out above). I am unable to reproduce this issue when the Aura component subscribing is using the same namespace as the LWC component.

joeferraro avatar Apr 08 '20 14:04 joeferraro

cc @seksenov @manuel-jasso to see if he can re-route this issue to the proper queue, this is definitely not LWC.

caridy avatar Apr 08 '20 16:04 caridy

@seksenov @caridy Tracking on W-7429747

manuel-jasso avatar Apr 09 '20 14:04 manuel-jasso

Any updates on when this could be fixed? In my use case, I could reproduce this in Summer pre-release org as well.

My environment is set to use an already existing Background Utility Item Aura Component. And I'm building a new LWC component which needs to subscribe to a Change Data Event, now, both are conflicting, and LWC emp-api component is throwing the same error as mentioned above:

Method Promise.prototype.then called on incompatible receiver

vc4u avatar May 30 '20 21:05 vc4u

When I discovered this issue I wasn't using namespaces.

AllanOricil avatar May 30 '20 22:05 AllanOricil

seeing exactly same issue even when it is only component on page. @AllanOricil Thanks for any help!

arneper avatar May 31 '20 18:05 arneper

The Lightning Locker team is tracking and investigating the issue. When we have an update to share on potential resolutions and timeline we will do so.

seksenov avatar Jun 01 '20 16:06 seksenov

@seksenov thanks! It is appreciated

arneper avatar Jun 01 '20 17:06 arneper

Hi @AllanOricil, I am from Locker team, I would like to reproduce this issue, is there an environment you can point me to and steps to reproduce? Ideally, I would like to do that on my local, do you have a repo with code that I can clone and run? I understand this may be part of a large app, I'm just trying to see if we can narrow it down. Also looks like others like @joeferraro may have a similar/simpler way to reproduce?

manuel-jasso avatar Jul 09 '20 20:07 manuel-jasso

Hi @AllanOricil, I am from Locker team, I would like to reproduce this issue, is there an environment you can point me to and steps to reproduce? Ideally, I would like to do that on my local, do you have a repo with code that I can clone and run? I understand this may be part of a large app, I'm just trying to see if we can narrow it down. Also looks like others like @joeferraro may have a similar/simpler way to reproduce?

HI @manuel-jasso. I did it a long time ago. I will try to find the environment. But you can follow these steps to get the error.

  • Create Platform Event 1 and Platform Event 2.
  • Create a Lightning Page.
  • Add an Aura Component that subscribes to Platform Event 1 to this page.
  • Add a LWC that subscribes to Platform Event 2 to this page. Don't forget to apply the fix that was posted here to your LWC.
  • Check that the Aura Component subscribed and the LWC did not. Also, the LWC didn't display any error message.

To understand that this was a problem caused by mixing LWC and Aura components to the same lightning page, and not my code, I placed both in separate Lightning Pages, and both worked perfectly fine. But then an unexpected behavior started to happen.

To reproduce this one do the following:

  • Go to your Lightning Page 1, which has the Aura component that subscribes to the Platform Event 1.
  • Then "navigate to" the Lightning Page 2, that has a LWC that subscribes to the Platform Event 2.
  • The error message will appear.
  • Reload the Lightning Page 2 and check that the error is gone.

AllanOricil avatar Jul 09 '20 21:07 AllanOricil

Thanks @AllanOricil, kudos to @p-wu-214 (also from Locker) who was able to repro internally, so we can debug...

manuel-jasso avatar Jul 09 '20 21:07 manuel-jasso

Thanks @AllanOricil, kudos to @p-wu-214 (also from Locker) who was able to repro internally, so we can debug...

@manuel-jasso no problem. I hope it is not too hard to fix it. But you guys can do it! Good luck. Hope to hear the issue was solved soon.

AllanOricil avatar Jul 10 '20 03:07 AllanOricil

@AllanOricil Hey! An update: We debugged the problem, which was an unwrapped promise, and have a fix. Once the fix is in, we will repro the error you are running into and make sure it is working. Will keep you updated when it is tested in the environment and we backport it to an earlier release. Thank you!

arcadeteddy avatar Jul 21 '20 22:07 arcadeteddy

@AllanOricil Hey! An update: We debugged the problem, which was an unwrapped promise, and have a fix. Once the fix is in, we will repro the error you are running into and make sure it is working. Will keep you updated when it is tested in the environment and we backport it to an earlier release. Thank you!

@arcadeteddy nice! well done guys.

AllanOricil avatar Jul 21 '20 23:07 AllanOricil

Hey there, any updates?

gyadav4 avatar Aug 04 '20 07:08 gyadav4

@gyadav4 Hey, so the fixes are in! We decided on not back porting it because it touches a critical part of the filtering mechanism.

arcadeteddy avatar Aug 05 '20 16:08 arcadeteddy

@gyadav4 Hey, so the fixes are in! We decided on not back porting it because it touches a critical part of the filtering mechanism.

What do you mean "not back porting"? Does it mean we can't work with aura and lwc on the same page when using the emp api?

AllanOricil avatar Aug 05 '20 20:08 AllanOricil

@AllanOricil it means that we will not fix it in current production code, but in the upcoming release only, which might take a while to hit production.

caridy avatar Aug 05 '20 20:08 caridy

@AllanOricil it means that we will not fix it in current production code, but in the upcoming release only, which might take a while to hit production.

Thanks for clarifying it @caridy I understood it wrong. Sorry for that.

AllanOricil avatar Aug 06 '20 00:08 AllanOricil

Can this issue also occur when a custom LWC component uses emp-api and and an Aura component uses emp-api where the Aura component is in a managed package?

iarecuezero avatar Aug 31 '20 19:08 iarecuezero

Can this issue also occur when a custom LWC component uses emp-api and and an Aura component uses emp-api where the Aura component is in a managed package?

@iarecuezero This can happen if you place both components on the same page.

AllanOricil avatar Sep 03 '20 08:09 AllanOricil

@caridy Hi there. Seems that we still have the issue. Could you confirm that it has not been fixed in the Winter release? Or am I wrong?

fredgaume avatar Oct 29 '20 08:10 fredgaume

Can this issue also occur when a custom LWC component uses emp-api and and an Aura component uses emp-api where the Aura component is in a managed package?

Looks like I have the same problem :(

pazhukh avatar Apr 13 '21 07:04 pazhukh

Can this issue also occur when a custom LWC component uses emp-api and and an Aura component uses emp-api where the Aura component is in a managed package?

Looks like I have the same problem :(

If your Aura component is simple, migrate it to LWC. Just don't mix both "frameworks" and you will never have this problem

AllanOricil avatar Apr 13 '21 11:04 AllanOricil

Just added a wrapping Aura component to implement lightning:isUrlAddressable for navigation purposes and no longer receiving a platform event in an LWC in the same managed package namespace located in a different page that was previously working. No subscription in the Aura component.

@AllanOricil, I think there may be no work-around for this case?

KeithClarke avatar Dec 24 '21 11:12 KeithClarke