Lightning message Service to communicate betweeen lightning web component , aura lightning component and visualforce page. It is a new release in LWC from spring 20. Events in lightning web component. child to parent. parent to child. pubsub.
2. What is the LMS ?
• First Salesforce technology designed to enable communication
between Visualforce and Lightning Components anywhere on a
Lightning Page.
• Introduced in Winter ’20 and available in Summer ’20 release.
• It is a front-end service that works in client-side user interfaces.
• based on a new type of metadata: Lightning Message Channels.
3. What is Lightning Message Channel?
• Provides access to the Lightning Message Service API.
• Let you publish and subscribe to messages across the DOM.
• It works only in Lightning Experience.
4. How to create Lightning Message Channel?
• To create LMS you need to create folder under force-app/main/default and
folder name will be “messageChannels”.
• <MessageChannelName>.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>recordData</fieldName>
<description>The current data representing the record that changed</description>
</lightningMessageFields>
</LightningMessageChannel>
5. Methods provide by LMS
• Publish (messageContext, Lmchannel, message)
• Subscribe (messageContext, Lmchannel, callback, subscriberOptions)
• Unsubscribe (subscribeObject)
6. Import LMS in components
• LWC
• import { MessageContext, publish } from 'lightning/messageService’;
• import { MessageContext, subscribe, unsubscribe} from
'lightning/messageService’;
• import SAMPLEMC from
"@salesforce/messageChannel/SampleMessageChannel__c";
• Aura
• <lightning:messageChannel type="SampleMessageChannel__c"
aura:id="sampleMessageChannel"/>
• <lightning:messageChannel type="SampleMessageChannel__c"
• onMessage="{!c.handleMessage}"/>
• VF
• var SAMPLEMC = "{!$MessageChannel.SampleMessageChannel__c}";