0

I have implemented the LMS(Lightning Message Service) concept in my project because I have to pass some parameter values from LWC component to Aura component. the problem is my LWC component is not able to publish the value and I don't know why, I'm following the steps that have been mentioned in the Salesforce documentation, and here is my code:

LWC Code:

import { LightningElement,wire } from 'lwc';

import { publish, MessageContext } from 'lightning/messageService';


import SAMPLEMC from '@salesforce/messageChannel/SampleMessageChannel__c';


export default class testComponent extends LightningElement {

    @wire(MessageContext)
    messageContext;

    connectedCallback() {
        document.addEventListener('scroll', function(e){
            

if(window.scrollY <= 15){
   
    publish(this.messageContext,SAMPLEMC,{
        headerShrinkFlag:true

    });
    
}else if(window.scrollY <= 100){
    publish(this.messageContext,SAMPLEMC,{
        headerShrinkFlag:false

    });
}
          
              }, true);
    
    
    }
    disconnectedCallback() {
        releaseMessageContext(this.context);
    }

Aura Code:

cmp:

 <lightning:messageChannel type="SampleMessageChannel__c" aura:id="sampleMessageChannel" onMessage="{!c.handleMessage}" />


controller:

     handleMessage : function(component, message) {
    
        console.log("-----------------from Aura-----------------");
         console.log(message.getParam("headerShrinkFlag"))
         console.log("-----------------from Aura-----------------");
    }

2
  • Do you get any errors in the browser console? If so, please add the details to your question using edit.
    – Phil W
    Nov 18, 2021 at 8:59
  • no nothing on the console Nov 18, 2021 at 12:17

1 Answer 1

0

the code is working properly

however to use LMS you should create messageChannels record

https://developer.salesforce.com/docs/atlas.en-us.api_meta.meta/api_meta/meta_lightningmessagechannel.htm

https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.use_message_channel_intro

Сode corrections are:

lwc

import SAMPLEMC from '@salesforce/messageChannel/Sample_Message_Channel__c';

...

let $this = this;
document.addEventListener('scroll', function (e) {
        if (window.scrollY <= 15) {
            publish($this.messageContext, SAMPLEMC, {
                headerShrinkFlag: true
            });

aura:

type="Sample_Message_Channel__c"

force-app/main/default/messageChannels/Sample_Message_Channel.messageChannel-meta.xml

<?xml version="1.0" encoding="UTF-8"?>
<LightningMessageChannel xmlns="http://soap.sforce.com/2006/04/metadata">
    <masterLabel>SampleMessageChannel</masterLabel>
    <isExposed>true</isExposed>
    <description>This is a sample Lightning Message Channel.</description>
    <lightningMessageFields>
        <fieldName>headerShrinkFlag</fieldName>
        <description>sample field</description>
    </lightningMessageFields>
</LightningMessageChannel>

Thanks

You must log in to answer this question.

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