0

Requirement: I have a custom subtab (Parent) on my console. It has a web link, upon clicking it opens a new subtab (Child). This Child subtab needs some data from the Parent subtab, so I am implementing a 2-way LMS approach to get the data: Clicking on the web link will publish an LMS channel with keyId (connectedCallback on the LWC), which then subscribed by the Parent subtab's component which in turn Publishes another LMS (DataChannel) with necessary data on it. I need to subscribe to this newly published DataChannel on the Child subtab's component.

Ask: How can I access this DataChannel on the Child component? Is it via renderedCallback()? Or, directly within connectedCallback() right after the initial LMS channel publish?

Child LWC:

connectedCallback() {
  //some code
  this.publishFirstLMSChannel(); //First LMS publishing
  //more code
}

Parent Aura:

CMP:

<lightning:messageChannel type="firstLMSfromChildLWC__c" scope="APPLICATION"
    onMessage="{!c.handleFirstLMS}"/> 

<lightning:messageChannel type="secondLMSDataChannel__c" scope="APPLICATION"
    aura:id ="secondLMSDataChannel"/> 

JS:

handleFirstLMS: function(component,message){
    //get Parent's data and constructor 'dataMessage'
    component.find("secondLMSDataChannel").publish(dataMessage);
   }

I was able to verify the LMS: secondLMSDataChannel and can confirm the availability of the data in it. All I need is to access it in the Child LWC.

1 Answer 1

2

You can subscribe as soon as connectedCallback, but you should not publish until the initial life cycle is done. You could easily do that in connectedCallback, though:

async connectedCallback() { // async converts this method to a Promise.
  this.subscribeToChannel(); // You can subscribe here.
  await Promise.resolve(); // Wait one render cycle.
  this.publishFirstLMSChannel();
}
4
  • Do I need to have this first: 'this.publishFirstLMSChannel();' and later 'this.subscribeToDataChannel();'? Commented Mar 29, 2022 at 18:58
  • @AustinEvans If you don't subscribe first, you won't get the return message, because the second component will publish to the channel before you're ready for it.
    – sfdcfox
    Commented Mar 29, 2022 at 19:07
  • It worked now. But, can you please add more details? I would like to understand the concept well. My understanding goes: I have to publish first and wait for the parent LMS channel to publish back the data, then I can subscribe to it. Commented Mar 29, 2022 at 19:51
  • 2
    @AustinEvans That's incorrect. Think about all the subscriptions you have in your life, perhaps magazines, email lists, services like Netflix, etc. You must subscribe to the service before you get any service. Similarly, you must subscribe to a message channel before you can receive messages on that channel. Any messages sent while you're not subscribed will be missed.
    – sfdcfox
    Commented Mar 29, 2022 at 20:03

You must log in to answer this question.

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