Salesforce introduced the Lightning Message Service (LMS) to promote effortless communication and seamless data exchange among Lightning components.
In this article, we will understand what Lightning Message Service is, its key features and advantages, and provide guidance on its effective implementation within the Salesforce platform.
What is Lightning Message Service?
Lightning Message Service (LMS) serves as a powerful communication framework introduced by Salesforce. The main goal of LMS is to enable effective communication between multiple lightning web components.
Using LMS, you can enable conversations between Visualforce, Aura, and Lightning Web Components (LWC) on a Lightning page.
Below is the Lightning Message Service Flow diagram showcasing the communication between different components with the help of Lightning Message Channels.
Also Read – Understanding Events in Salesforce Lightning Web Components
Key Concepts of Lightning Message Services
Publisher
The task of a publisher component involves sending messages to a specified message channel.
These messages can either carry data or not, and subscribers possess the capability to react and respond to these messages as needed.
When you’re creating a publisher component, it’s important to import the publish() function from the @salesforce/messageChannel module.
This function enables you to send messages through the Lightning Message Channel.
Subscriber
A subscriber component actively monitors a designated message channel. Whenever a message is published on this channel, all subscribed components that are listening can respond to that message.
Importantly, subscribers don’t require knowledge about the publisher’s specifics, which helps maintain a flexible and loosely coupled system.
Lightning Message Channel
Lightning web components can publish messages or data to a Lightning message channel.
Simultaneously, any lightning component or Visualforce page subscribed to that specific Lightning message channel will receive the data or message on their respective components.
In order to harness the capabilities of the Lightning Message Service, it is necessary to establish a Lightning Message Channel.
Lightning message channel can be referenced with the suffix __c.
Please keep in mind that for Lightning Message Service to function properly, both components must be present on the same record page.
Create a Lightning Message Channel
To create a Lightning message channel, use the LightningMessageChannel metadata type.
Create a folder named ‘messageChannels’ at force-app/main/default location.
[
After the folder is established, the next step involves creating the initial Lightning message channel XML file, which should have the “.messageChannel-meta.xml” suffix.
The masterLabel tag in the “.messageChannel-meta.xml” file holds the name of the messageChannel which in our example is “contactMessageChannel”
To deploy a LightningMessageChannel into your org, create an SFDX project. Include the XML definition in the force-app/main/default/messageChannels/ directory. The LightningMessageChannel file name follows the format messageChannelName.messageChannel-meta.xml
The “isExposed” tag serves the purpose of making the component available for use.
The “lightningMessageFields” tag can be used to pass specific information defining the field for example recordId.
You can use the “description” tag to add a description about your lightning message channel explaining the goal behind creating this message channel
Once the “.messageChannel-meta.xml” is ready, deploy it to your Salesforce org.
LWC Publisher Component Example
Now we will create a new Lightning Web Component (LWC) named publisherLWC
It will publish the data to the Lightning message channel.
Always remember to add __c suffix in the end while importing the message channel.
LWC Subscriber Component Example
To listen and stop listening to messages on a message channel in your Lightning web component, import the message channel from @salesforce/messageChannel module and make use of the subscribe() and unsubscribe() functions offered by the Lightning message service.
Benefits of implementing LMS
- LMS allows components to be loosely connected, which simplifies maintenance and scalability of the components as changes made in one component don’t force modifications in others.
- With dynamic components in place, lightning message service makes the user experience better.
Conclusion
The Lightning Message Service in Salesforce is a useful tool. It helps developers make applications that are interactive, quick to respond, and user-friendly.
This tool achieves this by enabling real-time communication between different parts of the application called Lightning components.
Overall, the Lightning Message Service enhances the user experience, simplifies the development process, and encourages the building of applications that can easily grow and adapt to changing needs.
Take your Salesforce expertise to new heights with saasguru’s Online Salesforce Bootcamps. Dive deep into Salesforce, gaining hands-on application experience and achieving recognized certifications. Engage in real-world projects, from requirement analysis to solution designing and showcasing your work.
Plus, refine your soft skills, focusing on personal branding, and mastering successful interviews. Sign up with saasguru today!