Lightning Message Service (LMS) is the
Salesforce technology of its kind that facilitates communication between
Visualforce, Lightning Web Components, and Aura Components on a Salesforce
Lightning Page.
With the launch of LMS, Salesforce developers obtained a simple and seamless API for publishing messages throughout the Lightning Experience platform and subscribed to messages that originated from anywhere within Lightning Experience.
·
In LWC we can
access Lightning Message Channel with the scoped module
@salesforce/messageChannel.
·
In Visualforce,
we can use global variable $MessageChannel.
·
In Aura, use
lightning:messageChannel in your component
Create
Lightning Message Channel
Currently we can create Lightning Message channel
with Metadata API. You can create the same with the help of VsCode.
You need to create one DX project then you need to place your message channel definition with the suffix .messageChannel-meta.xml in the force-app/main/default/messageChannels directory. like below folder structure.
Sample Message-meta.xml
<?xml version="1.0" encoding="UTF-8"?>
<LightningMessageChannel xmlns="https://soap.sforce.com/2006/04/metadata">
<masterLabel>Counter</masterLabel>
<isExposed>True</isExposed>
<lightningMessageFields>
<fieldName>operator</fieldName>
</lightningMessageFields>
<lightningMessageFields>
<fieldName>constant</fieldName>
</lightningMessageFields>
</LightningMessageChannel>
Here’s how to import a
Lightning message channel that a component can use to communicate via the
Lightning Message Service.
// Syntax
import channelName from
'@salesforce/messageChannel/channelReference';
// Syntax for resources in a
managed package
import channelName from
'@salesforce/messageChannel/namespace__channelReference';
// Example
import recordSelected from
'@salesforce/messageChannel/Record_Selected__c';
channelName—An imported symbol that identifies the message
channel.
channelReference—The API name of the message channel.
namespace—If the message channel is in a managed package, this
value is the namespace of the managed package. If the message channel is not in
a managed package, do not include a namespace.
Restrictions in
LMS:
Here are several restrictions
to the Lightning Message Service.
>Under the following circumstances, you are unable to utilise LMS:
· Salesforce Mobile
· Communities
· AppExchange
Note:
LMS cannot be used to
communicate with VF page contained in an iframe in Aura/LWC.
Examples of data that you can
pass in a message include strings, numbers, booleans, and objects. A message
cannot contain functions and symbols.
The lightning:messageChannel component is only available in Lightning Experience.